|
马上注册,结交更多好友,享用更多功能^_^
您需要 登录 才可以下载或查看,没有账号?立即注册
x
这道问题是输入学生信息成绩,按照平均分比较后排序
我的思路是用单链表,插入一个结构体,根据平均分找到比平均分大的第一个数的结构体,然后插入单链表
- #include <stdio.h>
- #include <stdlib.h>
- struct Sorce
- {
- int chinese;
- int math;
- int english;
- };
- struct Student
- {
- int num;
- char name[20];
- struct Sorce*sorce;
- float average;
- struct Student*next;
- };
- void getInput(struct Student*student);
- void getInput(struct Student*student)
- {
- printf("请输入学号");
- scanf("%d",&student->num);
- printf("请输入名字");
- scanf("%s",student->name);
- printf("请输入语文数学英语成绩");
- scanf("%d %d %d",&student->sorce->chinese,&student->sorce->math,&student->sorce->english);
- student->average=(student->sorce->chinese+student->sorce->math+student->sorce->english)/3;
- }
- void addStudent(struct Student**list);
- void addStudent(struct Student**list)
- {
- struct Student*previous;
- struct Student*current;
- struct Student*new;
- current=*list;
- previous=NULL;
- new=(struct Student*)malloc(sizeof(struct Student));
- if(new==NULL)
- {
- printf("内存分配失败\n");
- exit(1);
- }
- getInput(new);
- while(current!=NULL && (current->average)<(new->average))
- {
- previous=current;
- current=current->next;
- }
- new->next=current;
- if(previous==NULL)
- {
- *list=new;
- }
- else
- {
- previous->next=new;
- }
- }
- void printStu(struct Student*list);
- void printStu(struct Student*list)
- {
- struct Student*current;
- current=list;
- while(current!=NULL)
- {
- printf("%d %s %d %d %d\n",current->num,current->name,current->sorce->chinese,current->sorce->math,current->sorce->english);
- current=current->next;
- }
- putchar('\n');
- free(current);
- }
- int main()
- {
- struct Student*list=NULL;
- int ch;
- while(1)
- {
- printf("是否录入学生信息(Y/N)");
- do
- {
- ch=getchar();
- }
- while(ch!='Y' && ch!='N');
- if(ch=='Y')
- {
- addStudent(&list);
- }
- else
- {
- break;
- }
- }
- printStu(list);
- return 0;
- }
复制代码
本帖最后由 jackz007 于 2021-10-29 11:56 编辑
- struct Student
- {
- int num;
- char name[20];
- struct Sorce*sorce ;
- float average;
- struct Student*next;
- };
复制代码
改为:
- struct Student
- {
- int num;
- char name[20];
- struct Sorce sorce ;
- float average;
- struct Student*next;
- };
复制代码
如果不改,结构体的 sorce 成员只是一个指针,当定义该结构体变量时,此变量中并不包括结构体 struct Sorce 的存储空间,sorce 成员只是一个指向 NULL 的指针,相关的成员变量(Chinese、math、english)将没有空间保存,在访问这些变量的时候,将会导致异常退出。
还有就是函数 void addStudent(struct Student**list) 中多处使用 new 作为变量名,这在我使用的 gcc 10.2.0 编译器会直接报错。
代码问题主要集中在以上两点。
- #include <stdio.h>
- #include <stdlib.h>
- struct Sorce
- {
- int chinese ;
- int math ;
- int english ;
- } ;
- struct Student
- {
- int num ;
- char name[20] ;
- struct Sorce sorce ; // 【修改】
- float average ;
- struct Student*next ;
- } ;
- void getInput(struct Student*student);
- void getInput(struct Student*student)
- {
- printf("请输入学号");
- scanf("%d",&student->num);
- printf("请输入名字");
- scanf("%s",student->name);
- printf("请输入语文数学英语成绩");
- scanf("%d %d %d",&student->sorce.chinese,&student->sorce.math,&student->sorce.english); // 【修改】
- student->average=(student->sorce.chinese+student->sorce.math+student->sorce.english)/3; // 【修改】
- }
- void addStudent(struct Student**list);
- void addStudent(struct Student**list)
- {
- struct Student*previous;
- struct Student*current;
- struct Student*p; // 【修改】
- current=*list;
- previous=NULL;
- p=(struct Student*)malloc(sizeof(struct Student)); // 【修改】
- if(p==NULL) // 【修改】
- {
- printf("内存分配失败\n");
- exit(1);
- }
- getInput(p); // 【修改】
- while(current!=NULL && (current->average)<(p->average)) // 【修改】
- {
- previous=current;
- current=current->next;
- }
- p->next=current; // 【修改】
- if(previous==NULL)
- {
- *list=p; // 【修改】
- }
- else
- {
- previous->next=p; // 【修改】
- }
- }
- void printStu(struct Student*list);
- void printStu(struct Student*list)
- {
- struct Student*current;
- current=list;
- while(current!=NULL)
- {
- printf("%d %s %d %d %d\n",current->num,current->name,current->sorce.chinese,current->sorce.math,current->sorce.english); // 【修改】
- current=current->next;
- }
- putchar('\n');
- free(current);
- }
- int main()
- {
- struct Student*list=NULL;
- int ch;
- while(1)
- {
- printf("是否录入学生信息(Y/N)");
- do
- {
- ch=getchar();
- }
- while(ch!='Y' && ch!='N');
- if(ch=='Y')
- {
- addStudent(&list);
- }
- else
- {
- break;
- }
- }
- printStu(list);
- return 0;
- }
复制代码
|
|