C语言中的学生成绩管理系统的保存数据
录入的文件夹是空的,调试的时候看到head是空的,但传头指针的成绩表没有问题#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;
};
struct Line *head=NULL;
void saveStudent()
{
FILE *fp;
struct Line *student=NULL;
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()
{
char input;
printf("请输入学生信息(姓名或者学号):");
scanf("%s",input);
struct Line *student;
student=head;
while(student!=NULL)
{
if(!strcmp(student->student.name,input)||student->student.num==input)
{
break;
}
student=student->next;
}
return student;
}*/
void printStudent(struct Line *student)
{
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",student->student.c_mark);
printf("三科平均分为:%d",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%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 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;
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();
printStudent(temp);
break*/;
case 4:;
case 5:;
case 6:saveStudent();break;
case 0:;
}
}
return 0;
} {:5_109:} 本帖最后由 jhq999 于 2021-11-1 19:56 编辑
int main()
{
char ch;
//struct Line *head=NULL;//////////////////////////////////////////////重复声明,或者你把前面全局变量head删除,然后saveStudent()函数添加参数 Line *head
char sh;
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();
printStudent(temp);
break*/;
case 4:;
case 5:;
case 6:saveStudent();break;
case 0:;
}
}
return 0;
} {:5_109:} 好像是重复声明 不知道自己回过这帖子没有 再回一次试试看 重复声明 {:10_279:} {:10_277:} {:10_256:} {:5_95:} {:10_307:}
页:
[1]