学生成绩管理系统保存信息
这个只要输入两个学生信息之后,再次打开就会打不开,而且文件变得很大,搞不懂是怎么回事。保存在(35~53)打开文件在(431~483)
#include<stdio.h>
#include<string.h>
#include<stdlib.h>
#include<math.h>
struct Student
{
char name;
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;
};
int fmax(int a,int b)
{
return a>b?a:b;
}
int fmin(int a,int b)
{
return a>b?b:a;
}
void saveStudent(struct Line *head)
{
FILE *fp;
struct Line *student;
fp=fopen("score.txt","wb+");
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 saveStudent2(struct Line *head)
{
FILE *fp;
struct Line *student;
fp=fopen("score2.txt","wb+");
if(fp==NULL)
{
printf("打开文件失败!!!\n");
return;
}
student=head;
rewind(fp);
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||student->student.num==' ')
{
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||student->student.English_mark==' ')
{
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||student->student.Math_mark==' ')
{
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||student->student.c_mark==' ')
{
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;
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;
char ch;
int a;
printf("=====================================\n");
printf("1.按照名字查找需要删除的学生\n");
printf("2.按照学号查找需要删除的学生\n");
printf("3.返回菜单\n");
printf("请输入你的选择:");
scanf("%d",&ch);
switch(ch)
{
case 1:printf("请输入该学生的名字:");
scanf("%s",input);
for(previous = NULL , current = * head ; current && strcmp(current -> student . name , input) ;previous = current , current = current -> next) ;
if(current)
{
if(current == * head)
{
* head = current -> next ;
} else
{
previous -> next = current -> next ;
}
} else
{
printf("查无此人 ε=ε=ε=ε=ε=ε=┌(; ̄◇ ̄)┘\n") ;
}
break;
case 2:printf("请输入该学生的学号:");
scanf("%d",&a);
for(previous = NULL , current = * head ; current && !(current->student.num==a) ;previous = current , current = current -> next) ;
if(current)
{
if(current == * head)
{
* head = current -> next ;
}
else
{
previous -> next = current -> next ;
}
}
else
{
printf("查无此人 ε=ε=ε=ε=ε=ε=┌(; ̄◇ ̄)┘\n") ;
}
break;
case 3:return;
}
}
void changeStudent(struct Line **head)
{
struct Line *temp;
struct Line *student;
int ch;
temp=searchStudent(*head);
if(temp==NULL)
{
printStudent(temp);
return;
}
printf("===============================\n");
printf("1.改变学生姓名\n");
printf("2.改变学生的性别\n");
printf("3.改变学生学号\n");
printf("4.改变学生高数成绩\n");
printf("5.改变学生英语成绩\n");
printf("6.改变学生C语言程序设计成绩\n");
printf("0.返回主菜单\n");
printf("===============================\n");
printf("请输入你的选择:");
scanf("%d",&ch);
switch(ch)
{
case 1: printf("请输入你要改变后的名字:");
scanf("%s",temp->student.name);
break;
case 2:printf("请输入你要改变后的性别:");
scanf("%c",temp->student.sex);
break;
case 3:printf("请输入你要改变后的学号:");
scanf("%d",&temp->student.num);
break;
case 4:printf("请输入你要改变后的高数成绩:");
scanf("%d",&temp->student.Math_mark);
temp->student.avermark=(temp->student.c_mark+temp->student.English_mark+temp->student.Math_mark)/3;
temp->student.sum=temp->student.c_mark+temp->student.English_mark+temp->student.Math_mark;
break;
case 5:printf("请输入你要改变后的英语成绩:");
scanf("%d",&temp->student.English_mark);
temp->student.avermark=(temp->student.c_mark+temp->student.English_mark+temp->student.Math_mark)/3;
temp->student.sum=temp->student.c_mark+temp->student.English_mark+temp->student.Math_mark;
break;
case 6:printf("请输入你要改变后的C语言程序设计成绩:");
scanf("%d",&temp->student.c_mark);
temp->student.avermark=(temp->student.c_mark+temp->student.English_mark+temp->student.Math_mark)/3;
temp->student.sum=temp->student.c_mark+temp->student.English_mark+temp->student.Math_mark;
break;
case 0:return;
}
}
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 Openfile(struct Line **head)
{
FILE *fp;
struct Line *temp,*previous,*current;
struct Line *student;
student=(struct Line*)malloc(sizeof(struct Line));
if(student==NULL)
{
printf("内存分配失败!!!");
return;
}
fp=fopen("score.txt","rb+");
if(fp==NULL)
{
printf("打开文件失败!!!\n");
return;
}
while(!feof(fp))
{
fread(student,sizeof(struct Line ),1,fp);
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;
}
}
}
fclose(fp);
}
void Openfile2(struct Line **head)
{
FILE *fp;
struct Line *temp,*previous,*current;
struct Line *student;
student=(struct Line*)malloc(sizeof(struct Line));
if(student==NULL)
{
printf("内存分配失败!!!");
return;
}
fp=fopen("score2.txt","rb");
if(fp==NULL)
{
printf("打开文件失败!!!\n");
return;
}
while(!feof(fp))
{
fread(student,sizeof(struct Line ),1,fp);
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;
}
}
}
fclose(fp);
}
void draw(int i,char ch)
{
int j;
if(i==0)
{
printf("%c:NULL\n",ch);
return;
}
printf("%c:",ch);
for(j=0;j<i;j++)
{
printf("*");
}
printf("\n");
}
void getStudent_mark(struct Line *head)
{
struct Line *student;
int math,math_max=-1,math_min=101,math_A=0,math_B=0,math_C=0,math_D=0,math_E=0,english,english_min=101,english_max=-1,english_A=0,english_B=0,english_C=0,english_D=0,english_E=0,c,c_min=101,c_max=-1,c_A=0,c_B=0,c_C=0,c_D=0,c_E=0,avermark,avermark_max=-1,avermark_min=101,avermark_A=0,avermark_B=0,avermark_C=0,avermark_D=0,avermark_E=0,count=0,sum_max=-1,sum_min=101,sum_mark;
student=head;
int ch;
int a;
while(student!=NULL)
{
math=student->student.Math_mark;
english=student->student.English_mark;
c=student->student.c_mark;
avermark=student->student.avermark;
sum_mark=student->student.sum;
math_max=fmax(math,math_max);
math_min=fmin(math,math_min);
english_max=fmax(english,english_max);
english_min=fmin(english,english_min);
c_max=fmax(c,c_max);
c_min=fmin(c,c_min);
avermark_max=fmax(avermark,avermark_max);
avermark_min=fmin(avermark,avermark_min);
sum_max=fmax(sum_mark,sum_max);
sum_min=fmin(sum_mark,sum_min);
switch(math/10)
{
case 10:math_A++;break;
case 9:math_A++;break;
case 8:math_B++;break;
case 7:math_C++;break;
case 6:math_D++;break;
default:math_E++;break;
}
switch(english/10)
{
case 10:english_A++;break;
case 9:english_A++;break;
case 8:english_B++;break;
case 7:english_C++;break;
case 6:english_D++;break;
default:english_E++;break;
}
switch(c/10)
{
case 10:c_A++;break;
case 9:c_A++;break;
case 8:c_B++;break;
case 7:c_C++;break;
case 6:c_D++;break;
default:c_E++;break;
}
switch(avermark/10)
{
case 10:avermark_A++;break;
case 9:avermark_A++;break;
case 8:avermark_B++;break;
case 7:avermark_C++;break;
case 6:avermark_D++;break;
default:avermark_E++;break;
}
count++;
student=student->next;
}
printf("1.查询高数成绩的分布\n");
printf("2.查询英语成绩的分布\n");
printf("3.查询C语言程序设计成绩分布\n");
printf("4.查询平均分成绩分布\n");
printf("5.返回主菜单\n");
scanf("%d",&ch);
while(1)
{
switch(ch)
{
case 1:printf("该次高数考试共有%d个人参加,其中最高分为%d,最低分为%d\n",count,math_max,math_min);
draw(math_A,'A');
draw(math_B,'B');
draw(math_C,'C');
draw(math_D,'D');
draw(math_E,'E');
printf("是否想知道其他的成绩分布\n");
printf("1.否\n");
printf("2.C语言程序设计成绩分布\n");
printf("3.英语成绩分布\n");
printf("4.平均分成绩分布\n");
printf("请输入你的决定:");
scanf("%d",&a);
switch(a)
{
case 1:return;
case 2:ch=3;break;
case 3:ch=2;break;
case 4:ch=4;break;
}
case 2:printf("该次英语考试共有%d个人参加,其中最高分为%d,最低分为%d\n",count,english_max,english_min);
draw(english_A,'A');
draw(english_B,'B');
draw(english_C,'C');
draw(english_D,'D');
draw(english_E,'E');
printf("是否想知道其他的成绩分布\n");
printf("1.否\n");
printf("2.C语言程序设计成绩分布\n");
printf("3.高数成绩分布\n");
printf("4.平均分成绩分布\n");
printf("请输入你的决定:");
scanf("%d",&a);
switch(a)
{
case 1:return;
case 2:ch=3;break;
case 3:ch=1;break;
case 4:ch=4;break;
}
case 3:printf("该次C语言程序与设计考试共有%d个人参加,其中最高分为%d,最低分为%d\n",count,c_max,c_min);
draw(c_A,'A');
draw(c_B,'B');
draw(c_C,'C');
draw(c_D,'D');
draw(c_E,'E');
printf("是否想知道其他的成绩分布\n");
printf("1.否\n");
printf("2.高数成绩分布\n");
printf("3.英语成绩分布\n");
printf("4.平均分成绩分布\n");
printf("请输入你的决定:");
scanf("%d",&a);
switch(a)
{
case 1:return;
case 2:ch=1;break;
case 3:ch=2;break;
case 4:ch=4;break;
}
case 4:printf("该次考试共有%d个人参加,其中平均分最高为%d,最低分为%d\n",count,avermark_max,avermark_min);
draw(avermark_A,'A');
draw(avermark_B,'B');
draw(avermark_C,'C');
draw(avermark_D,'D');
draw(avermark_E,'E');
printf("是否想知道其他的成绩分布\n");
printf("1.否\n");
printf("2.C语言程序设计成绩分布\n");
printf("3.英语成绩分布\n");
printf("4.高数成绩分布\n");
printf("请输入你的决定:");
scanf("%d",&a);
switch(a)
{
case 1:return;
case 2:ch=3;break;
case 3:ch=2;break;
case 4:ch=1;break;
}
case 5:return;
}
}
}
void menu()
{
printf("************************\n");
printf("1.添加学生信息\n");
printf("2.查看成绩表\n");
printf("3.查询成绩信息\n");
printf("4.删除学生信息\n");
printf("5.修改学生信息\n");
printf("6.保存学生信息\n");
printf("7.调用学生信息\n");
printf("8.获取学生成绩分布信息\n");
printf("9.操作指南\n");
printf("0.退出程序\n");
printf("*************************\n");
}
void menu2()
{
printf("************************\n");
printf("1.查看1班的成绩\n");
printf("2.查看2班的成绩\n");
printf("3.退出该程序\n");
printf("************************\n");
}
int main()
{
char ch;
struct Line *head=NULL;
char sh;
char a;
char identity;
int n,b;
struct Line *student,*temp;
char Teacher1[]="teacher1";
char Teacher2[]="teacher2";
char Student[]="student";
char Master[]="master";
char *input;
heart();
printf("欢迎使用学生成绩管理系统 \n");
printf("welcome to student manage system\n");
printf("====power by swaggy boi!!!====\n");
system("pause");
printf("请输入你的身份:");
scanf("%s", identity);
if(!strcmp(identity,Teacher1))
{
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 7: Openfile(&head);
saveStudent(head);
break;
case 8:getStudent_mark(head);break;
case 9:printf("1.本程序可以帮助班主任统计学生的成绩\n");
printf("2.可以快速排序学生的成绩\n");
printf("3.如果想获取之前编写的信息可以使用“调用学生信息”\n");
printf("4.使用“获取学生成绩分布”可以使学生的成绩分布一目了然\n");
printf("5.A成绩表示成绩在90~100,B成绩表示成绩在80~89,C成绩表示成绩在70~79,D成绩表示成绩在60~69,E成绩表示成绩在60以下\n");
printf("===希望大家用的开心!===\n");
break;
case 0:remove("score.txt");
saveStudent(head);
exit(1);
}
}
}
else if(!strcmp(identity,Teacher2))
{
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:saveStudent2(head);break;
case 7:Openfile2(&head);
saveStudent2(head);
break;
case 8:getStudent_mark(head);break;
case 9:printf("1.本程序可以帮助班主任统计学生的成绩\n");
printf("2.可以快速排序学生的成绩\n");
printf("3.如果想获取之前编写的信息可以使用“调用学生信息”\n");
printf("4.使用“获取学生成绩分布”可以使学生的成绩分布一目了然\n");
printf("5.A成绩表示成绩在90~100,B成绩表示成绩在80~89,C成绩表示成绩在70~79,D成绩表示成绩在60~69,E成绩表示成绩在60以下\n");
printf("===希望大家用的开心!===\n");
break;
case 0:saveStudent2(head);
exit(1);
}
}
}
else if(!strcmp(identity,Student))
{
printf("请输入你的名字:");
scanf("%s",a);
do
{
printf("请输入你的班级:");
scanf("%d",&n);
}while(n>2||n<0);
printf("请稍等.....\n");
if(n==1)
{
Openfile(&head);
student=head;
if(!strcmp(student->student.name,a))
{
printStudent(student);
saveStudent(head);
exit(1);
}
while(student!=NULL&&!strcmp(student->student.name,a))
{
student=student->next;
}
if(student!=NULL)
{
printStudent(student);
saveStudent(head);
exit(1);
}
else
{
saveStudent(head);
}
}
else if(n==2)
{
Openfile2(&head);
student=head;
if(!strcmp(student->student.name,a))
{
printStudent(student);
saveStudent2(head);
exit(1);
}
while(student!=NULL&&!strcmp(student->student.name,a))
{
student=student->next;
}
if(student!=NULL)
{
printStudent(student);
saveStudent2(head);
exit(1);
}
else
{
saveStudent2(head);
}
}
printf("老师还未录入你的成绩,再等等吧!\n");
}
else if(!strcmp(identity,Master))
{
while(1)
{
menu2();
scanf("%d",&b);
switch(b)
{
case 1: Openfile(&head);
getStudent_mark(head);
saveStudent(head);
break;
case 2: Openfile2(&head);
getStudent_mark(head);
saveStudent2(head);
break;
case 3: return 0;
}
}
}
return 0;
} {:10_277:} {:10_254:} {:10_256:} {:10_279:} {:10_279:} {:10_279:} 本帖最后由 jackz007 于 2021-11-4 12:28 编辑
试试这个代码:
void Openfile(struct Line ** head)
{
FILE * fp ;
const char fn[] = "student.dat" ;
struct Line * student , * previous ;
size_t k , m ;
if(fp = fopen(fn , "rb")) {
fseek(fp , 0 , SEEK_END) ;
m = ftell(fp) / sizeof(struct Line) ;
fseek(fp , 0 , SEEK_SET) ;
for(previous = NULL , k = 0 ; k < m ; k ++) {
if(student = (struct Line *) malloc(sizeof(struct Line))) {
fread(student , sizeof(struct Line ) , 1 , fp) ;
student -> next = NULL ;
if(! previous) * head = student ;
else previous -> next = student ;
previous = student ;
} else {
fprintf(stderr , "内存分配失败!\n") ;
break ;
}
}
fclose(fp) ;
} else {
fprintf(stderr , "文件:[%s] 不存在\n" , fn) ;
}
}
void Savefile(struct Line * head)
{
FILE * fp
const char fn[] = "student.dat" ;
struct Line * student ;
if(fp = fopen(fn , "wb")) {
for(student = head ; student ; student = student -> next) {
fwrite(student , sizeof(struct Line) , 1 , fp) ;
}
fclose(fp) ;
} else {
fprintf(stderr , "文件:[%s] 创建失败\n" , fn) ;
}
} jackz007 发表于 2021-11-4 10:12
试试这个代码:
这个在每次调用一次就会在打印成绩表的时候多打印一行 superswagy2002 发表于 2021-11-4 10:27
这个在每次调用一次就会在打印成绩表的时候多打印一行
8 楼的代码已经修改,请再次测试。 {:10_256:} {:10_256:} {:10_256:} 进来学习
{:10_254:} jackz007 发表于 2021-11-4 11:12
8 楼的代码已经修改,请再次测试。
感谢大佬 {:10_261:} {:10_256:}{:10_256:}
页:
[1]