杨小九 发表于 2022-1-1 11:41:27

如何输入学生信息后按成绩排序

#include <stdio.h>
struct Student
{
        int num;
        char name;
        float score;
};        struct Student stu;
int main()
{
        int i,j,k;struct Student temp;
        for(i=0;i<10;i++)
        {scanf("%d,%s,%f",&stu.num,&stu.name,&stu.score);}
        for(i=0;i<9;i++)
        {k=i;
                for(j=i+1;j<10;j++)
                        if(stu.score>stu.score)
                                k=j;
                temp=stu;stu=stu;stu=temp;
        }
        for(i=0;i<10;i++)
                printf("%6d %8s %6.2f\n",stu.num,stu.name,stu.score);
        return 0;
}
求问这个代码怎么行不通

jackz007 发表于 2022-1-1 12:08:31

#include <stdio.h>

struct Student
{
      int num       ;
      char name ;
      float score   ;
} stu             ;

int main(void)
{
      int i , j , k ; struct Student temp                                                            ;
      for(i = 0 ; i < 10 ; i ++) scanf("%d%s%f" , & stu . num , stu . name , & stu . score) ;
      for(i = 0 ; i < 9 ; i ++) {
                for(k = i , j = i + 1 ; j < 10 ; j ++) {
                        if(stu . score > stu . score) {
                              k = j         ;
                              temp = stu   ;
                              stu = stu ;
                              stu = temp   ;
                        }
                }
      }
      for(i = 0 ; i < 10 ; i ++) printf("%6d %8s %6.2f\n",stu.num , stu . name , stu . score) ;
}
      编译、运行实况:
D:\00.Excise\C>g++ -o x x.c

D:\00.Excise\C>x
1 zyz 1
2 gl 2
3 zsc 3
4 abc 4
5 bcd 5
6 cde 6
7 def 7
8 efg 8
9 fgh 9
10 ghi 10
    10      ghi10.00
   9      fgh   9.00
   8      efg   8.00
   7      def   7.00
   6      cde   6.00
   5      bcd   5.00
   4      abc   4.00
   3      zsc   3.00
   2       gl   2.00
   1      zyz   1.00

D:\00.Excise\C>

傻眼貓咪 发表于 2022-1-1 12:09:42

本帖最后由 傻眼貓咪 于 2022-1-1 12:11 编辑

{scanf("%d,%s,%f",&stu.num, stu.name,&stu.score);} // 输入字符串时,试试去掉 &
页: [1]
查看完整版本: 如何输入学生信息后按成绩排序