|
15鱼币
我这个改变头节点的办法,使得运行完改变了头节点后,再运行成绩表导致系统崩溃,有没有什么好的办法解决。(删除在第258-308)
- #include<stdio.h>
- #include<string.h>
- #include<stdlib.h>
- #include<math.h>
- struct Student
- {
- char name[40];
- int num;
- int English_mark;
- int Math_mark;
- int c_mark;
- int avermark;
- char sex;
- int sum;
- };
- struct Line
- {
- struct Student student;
- struct Line *next;
- };
- void saveStudent(struct Line *head)
- {
- FILE *fp;
- struct Line *student;
- fp=fopen("score.txt","w+");
- if(fp==NULL)
- {
- printf("打开文件失败!!!\n");
- return;
- }
- student=head;
- while(student!=NULL)
- {
- fwrite(&student->student,sizeof(struct Student),1,fp);
- student=student->next;
- }
- fclose(fp);
- printf("数据保存成功!!!\n");
- }
- void getInput(struct Line *student,struct Line *head)
- {
- int count=0;
- struct Line *temp;
- temp=head;
- printf("该学生的名字为:");
- scanf("%s",student->student.name);
- getchar();
- while(student->student.sex!='M'&&student->student.sex!='F'&&student->student.sex!='f'&&student->student.sex!='m')
- {
- printf("该学生的性别为(M/F):");
- scanf("%c",&student->student.sex);
- getchar();
- }
- printf("该学生的学号为:");
- scanf("%d",&student->student.num);
- getchar();
- while(1)
- {
- count=0;
- while(student->student.num<0||student->student.num>999)
- {
- if(count>0)
- {
- printf("该学生的学号为:");
- scanf("%d",&student->student.num);
- count++;
- }
- else if(count==0)
- {
- count++;
- }
- }
- while((temp!=NULL)&&(temp->student.num!=student->student.num))
- {
- temp=temp->next;
- }
- if(temp==NULL)
- {
- break;
- }
- else
- {
- printf("该学生的学号重复,请重新输入!\n");
- printf("该学生的学号为:");
- scanf("%d",&student->student.num);
- }
- }
- printf("该学生的英语成绩为:");
- scanf("%d",&student->student.English_mark);
- getchar();
- count=0;
- while(student->student.English_mark<0||student->student.English_mark>100)
- {
- if(count==0)
- {
- count++;
- }
- else
- {
- printf("该学生的英语成绩为:");
- scanf("%d",&student->student.English_mark);
- getchar();
- }
- }
- printf("该学生的高数成绩为:");
- scanf("%d",&student->student.Math_mark);
- getchar();
- count=0;
- while(student->student.Math_mark<0||student->student.Math_mark>100)
- {
- if(count==0)
- {
- count++;
- }
- else
- {
- printf("该学生的高数成绩为:");
- scanf("%d",&student->student.Math_mark);
- getchar();
- }
- }
- printf("该学生C语言程序设计的成绩为:");
- scanf("%d",&student->student.c_mark);
- getchar();
- count=0;
- while(student->student.c_mark<0||student->student.c_mark>100)
- {
- if(count==0)
- {
- count++;
- }
- else
- {
- printf("该学生的C语言程序设计成绩为:");
- scanf("%d",&student->student.c_mark);
- getchar();
- }
- }
- student->student.avermark=(student->student.c_mark+student->student.English_mark+student->student.Math_mark)/3;
- student->student.sum=student->student.c_mark+student->student.English_mark+student->student.Math_mark;
- }
- void addStudent(struct Line **head)
- {
- struct Line *student,*temp,*previous,*current;
- student=(struct Line*)malloc(sizeof(struct Line));
- if(student==NULL)
- {
- printf("内存分配失败!!");
- exit(1);
- }
- getInput(student,*head);
-
- if(*head==NULL)
- {
- *head=student;
- student->next=NULL;
- }
- else
- {
- current=*head;
- previous=NULL;
- while(current!=NULL&¤t->student.avermark>student->student.avermark)
- {
- previous=current;
- current=current->next;
- }
- if(current==NULL)
- {
- previous->next=student;
- student->next=NULL;
- }
- else if(current!=NULL&¤t!=*head)
- {
- student->next=current;
- previous->next=student;
- }
- else if(current==*head)
- {
- student->next=*head;
- *head=student;
- }
-
- }
- }
- struct Line *searchStudent(struct Line *head)
- {
- char input[20];
- int a;
- int ch;
- struct Line *student;
- student=head;
- printf("*************************\n");
- printf("1.按照姓名查询\n");
- printf("2.按照学号查询\n");
- printf("3.返回菜单\n");
- scanf("%d",&ch);
- switch(ch)
- {
- case 1:printf("请输入该学生的姓名:");
- scanf("%s",input);
- break;
- case 2:printf("请输入该学生的学号:");
- scanf("%d",&a);
- break;
- case 3:return 0;
- }
- while(student!=NULL)
- {
- if(!strcmp(student->student.name,input)||student->student.num==a)
- {
- break;
- }
- student=student->next;
- }
- return student;
- }
- void printStudent(struct Line *student)
- {
- if(student==NULL)
- {
- printf("很抱歉查无此人 ε=ε=ε=ε=ε=ε=┌(; ̄◇ ̄)┘\n");
- return;
- }
- printf("姓名:%s\n",student->student.name);
- printf("学号:%d\n",student->student.num);
- printf("高数成绩:%d\n",student->student.Math_mark);
- printf("英语成绩:%d\n",student->student.English_mark);
- printf("c语言程序设计成绩:%d\n",student->student.c_mark);
- printf("三科平均分为:%d\n",student->student.avermark);
- }
- void printMark(struct Line *head)
- {
- struct Line *student;
- int count=1;
- printf("====================================\n");
- printf("\t学生成绩表\t\n");
- printf("\t姓名\t性别\t学号\t英语成绩\t高数成绩\tC语言程序设计成绩\t总分\t平均成绩\t排名\t\n");
- student=head;
- while(student!=NULL)
- {
- printf("\t%s\t%c\t%d\t%d\t\t%d\t%d\t%d\t%d\t%d\t\n",student->student.name,student->student.sex,student->student.num,student->student.English_mark,student->student.Math_mark,student->student.c_mark,student->student.sum,student->student.avermark,count);
- student=student->next;
- count++;
- }
- printf("====================================\n");
- system("pause");
- }
- void delStudent(struct Line *head)
- {
- struct Line *previous,*current,*p1;
- previous=NULL;
- current=head;
- char input[20];
- char ch;
- printf("=====================================\n");
- printf("1.按照名字查找需要删除的学生\n");
- printf("2.按照学号查找需要删除的学生\n");
- printf("3.返回菜单\n");
- printf("请输入你的选择:");
- scanf("%d",&ch);
- switch(ch)
- {
- case 1:printf("请输入该学生的名字:");
- scanf("%s",input);
- if(!strcmp(head->student.name,input))
- {
- p1=head;
- head=head->next;
- free(p1);
- return;
- }
- if(!strcmp(current->student.name,input)||current!=NULL)
- {
- previous=current;
- current=current->next;
- }
- if(current==NULL)
- {
- printf("查无此人 ε=ε=ε=ε=ε=ε=┌(; ̄◇ ̄)┘\n");
- return;
- }
- else
- {
- if(previous==NULL)
- {
- head=current->next;
- }
- else
- {
- previous->next=current->next;
- }
- free(current);
- }
-
-
- }
-
- }
- void heart()
- {printf(" **** ****\n");
- printf(" ********* *********\n");
- printf("************* *************\n");
- int i,j;
- for(i=0;i<3;i++)
- {
- for(j=0;j<29;j++)
- {
- printf("*");
- }
- printf("\n");
- }
- for(i=0;i<7;i++)
- {
- for(j=0;j<2*(i+1)-1;j++)
- {
- printf(" ");
- }
- for(j=0;j<27-i*4;j++)
- {
- printf("*");
- }
- printf("\n");
- }
- for(i=0;i<14;i++)
- {
- printf(" ");
- }
- printf("*\n") ;
- }
- void menu()
- {
- printf("************************\n");
- printf("1.添加学生信息\n");
- printf("2.查看成绩表\n");
- printf("3.查询成绩信息\n");
- printf("4.删除学生信息\n");
- printf("5.修改学生信息\n");
- printf("6.保存学生信息\n");
- printf("0.退出程序\n");
- printf("*************************\n");
- }
- int main()
- {
- char ch;
- struct Line *head=NULL;
- char sh[20];
- struct Line *student,*temp;
- char *input;
- heart();
- printf("欢迎使用学生成绩管理系统 \n");
- printf("welcome to student manage system\n");
- printf("====power by swaggy boi!!!====\n");
- while(1)
- {
- menu();
- printf("选哪个程序xd:");
- scanf("%d",&ch);
- getchar();
- switch(ch)
- {
- case 1: addStudent(&head);
- char ch;
-
- while(1)
- {
- printf("学生信息录入成功!!!\n");
- printf("是否继续输入(Y/N):");
- ch=getchar();
- getchar();
- if(ch=='Y'||ch=='y')
- {
- addStudent(&head);
- }
- else if(ch=='N'||ch=='n')
- {
- break ;
- }
- }
- break;
- case 2:printMark(head);break;
- case 3:temp=searchStudent(head);
- if(temp==NULL)
- {
- printStudent(temp);
-
- }
- if(temp==0)
- {
- continue;
- }
- printStudent(temp);
- system("pause");
- break;
- case 4:delStudent(head);break;
- case 5:/*changeStudent(head)*/;
- case 6:saveStudent(head);break;
- case 0:exit(1);
- }
- }
- return 0;
- }
复制代码
本帖最后由 jhq999 于 2021-11-2 17:03 编辑
- void delStudent(struct Line **head)//因为形参改变,实参不会跟着改变,所以要换成指针的指针,可惜c没有引用,其实引用最简单
复制代码
|
|