写了好几天终于写完了,分享一下,如果看到不足的地方请帮忙指出来,谢谢。
学生管理系统.rar
(2.47 KB)
方便大家看,我再把代码也贴上来。#include <stdio.h>
#include <string.h>
#include <stdlib.h>
//如果找到任何不足的地方,希望大家提出来,我虚心求教。多谢!
typedef struct Student
{
int Number;
char Name[32];
float Math;
float Chinese;
float English;
struct Student* last;
struct Student* next;
}student;
int InfoNumb=0;
struct Student headNode={0};
struct Student* pStu=&headNode;
struct Student* pEnd=&headNode;
int PrInterface();
int AddInfo();
int DelInfo();
int CorInfo(student* correct);//修改信息
int ReadInfo();
int WriteInfo();
int FreeMem();
int DisplayStu(student* stu);
student* SearchInfo();
student* SchByNum(int num);
student* SchByName(char name[32]);
int main(int argc, char *argv[])
{
int choice;
ReadInfo();
char ch='y';
while(ch!='n')
{
student* pass=NULL;
PrInterface();
scanf("%d",&choice);
switch(choice)
{
case 1:AddInfo();
break;
case 2:pass=SearchInfo();
DelInfo(pass);
pass=NULL;
break;
case 3:pass=SearchInfo();
CorInfo(pass);
break;
case 4:begin:pass=SearchInfo();
operation:
printf("\n请选择操作选项:\n");
printf("\n1.删除该生信息\n");
printf("\n2.修改该生信息\n");
printf("\n3.继续查找\n");
printf("\n4.退出查找\n");
printf("\n5.退出学生成绩系统\n");
printf("\n选择:");
int op;
scanf("%d",&op);
switch(op)
{
case 1:DelInfo(pass);break;
case 2:CorInfo(pass);break;
case 3:goto begin;
case 4:goto begin;
case 5:goto here;
default:printf("\n对不起,输入有误,请重新输入。\n");
goto operation;
}
break;
case 5:
goto here;
default:
printf("\n请输入正确操作选项:");
break;
}
printf("\n请选择是否继续操作(y/n):");
getchar();
ch=getchar();
}
here: WriteInfo();
FreeMem();
printf("\n操作结束。\n");
return 0;
}
//输出操作界面
int PrInterface()
{
printf("\n1.新增学生记录。\n");
printf("\n2.删除学生记录。\n");
printf("\n3.修改学生记录。\n");
printf("\n4.查找学生记录。\n");
printf("\n5.退出。");
printf("\n请输入所选操作:");
return 0;
}
//将学生信息读取到pStu的next指针里
int ReadInfo()
{
FILE* fp=fopen("D:\\test.txt","r+b");
char TestEOF;
int Init=0;
if(fp==NULL)
{
printf("\n打开文件失败。\n");
return 0;
}
if(!fread(&InfoNumb,sizeof(int),1,fp)) //文件首存放学生总数,初始化为0
{
fseek(fp,0,0);
fwrite(&Init,sizeof(int),1,fp);
}
if(InfoNumb==0)
{
printf("\n未查找到任何学生信息,请添加学生信息.\n");
AddInfo();
}
else while(!feof(fp))
{
pStu->next=(student*)malloc(1*sizeof(student));
if(fread(pStu->next,sizeof(student),1,fp)==0)
{
printf("\n读取文件失败\n");
exit(0);
}
pStu->next->last=pStu;
pStu=pStu->next;
pStu->next=NULL;
TestEOF=fgetc(fp);
if(feof(fp)) //检测文件位置指针是否指向EOF字符
break;
else fseek(fp,-1L,1);
}
pEnd=pStu;
pStu=headNode.next;
fclose(fp);
return 0;
}
//释放所申请的内存块
int FreeMem()
{ pStu=headNode.next;
while(pStu)
{
pStu->last=NULL;
pStu->next=NULL;
free(pStu);
pStu=pStu->next;
}
return 0;
}
//把链表中的信息全部写入文件中
int WriteInfo()
{
FILE* fp=fopen("D:\\test.txt","wb");
if(fp==NULL)
{
printf("\n打开文件失败。\n");
exit(0);
}
pStu=headNode.next;
fwrite(&InfoNumb,sizeof(int),1,fp);
while(pStu)
{
if(fwrite(pStu,sizeof(student),1,fp)!=1)
printf("\n写入文件出错。\n");
pStu=pStu->next;
}
fclose(fp);
return 0;
}
//增加学生信息
int AddInfo()
{
char ch='y';
while(ch!='n')
{
student *p=(student*)malloc(1*sizeof(student));
pEnd->next=p;
pEnd->next->last=pEnd;
pEnd=pEnd->next;
pEnd->next=NULL;
printf("\n请输入学生学号: ");
scanf("%d",&(pEnd->Number));
printf("\n请输入学生名字: ");
getchar();
gets(pEnd->Name);
printf("\n请输入学生数学成绩: ");
scanf("%f",&(pEnd->Math));
printf("\n请输入学生语文成绩: ");
scanf("%f",&(pEnd->Chinese));
printf("\n请输入学生英语成绩: ");
scanf("%f",&(pEnd->English));
getchar();
printf("\n请选择是否继续输入(y/n): ");
ch=getchar();
InfoNumb++;
}
return 0;
}
//显示学生信息
int DisplayStu(student* stu)
{
printf("\n学生学号:%d",stu->Number);
printf("\n学生名字:%s",stu->Name);
printf("\n学生数学成绩:%.1f",stu->Math);
printf("\n学生语文成绩:%.1f",stu->Chinese);
printf("\n学生英语成绩:%.1f\n\n",stu->English);
return 0;
}
//查找学生信息
student* SearchInfo()
{
int choose;
student* display=NULL;
while(1)
{
again:printf("\n1.按学生学号进行操作。");
printf("\n2.按学生名字进行操作。 选择:");
scanf("%d",&choose);
switch(choose)
{
case 1:
printf("\n请输入学号:");
int num;
scanf("%d",&num);
display=SchByNum(num);
if(!display)
goto again;
else DisplayStu(display);
break;
case 2:
printf("\n请输入姓名:");
char nm[32];
scanf("%s",nm);
display=SchByName(nm);
if(!display)
goto again;
else DisplayStu(display);
break;
default:printf("\n对不起,输入有误,请重新输入。\n");
goto again;
}
break;
}
return display;
}
//按学生学号查找
student* SchByNum(int num)
{
pStu=headNode.next;
int sNum=pStu->Number;
while(num!=sNum)
{
if(!(pStu->next))
{
printf("\n对不起,您输入的学生号有误,请重新输入.");
return NULL;
}
else
{
pStu=pStu->next;
sNum=pStu->Number;
}
}
printf("\n查找成功。该生信息为:\n");
return pStu;
}
//通过姓名查找学生信息
student* SchByName(char name[32])
{
pStu=headNode.next;
char *ch=pStu->Name;
char *ch2=name;
while(*ch2!=*ch)
{
if(!(pStu->next))
{
printf("\n对不起,您输入的学生姓名有误,请重新输入.");
return 0;
}
else
{
pStu=pStu->next;
ch=pStu->Name;
}
}
return pStu;
}
//修改学生信息
int CorInfo(student* correct)
{
char ch='y';
while(ch!='n')
{
printf("\n请输入学生学号\n");
scanf("%d",&(correct->Number));
printf("\n请输入学生名字\n");
scanf("%s",correct->Name);
printf("\n请输入学生数学成绩\n");
scanf("%f",&(correct->Math));
printf("\n请输入学生语文成绩\n");
scanf("%f",&(correct->Chinese));
printf("\n请输入学生英语成绩\n");
scanf("%f",&(correct->English));
printf("\n请选择是否继续输入(y/n):\n");
getchar();
ch=getchar();
}
DisplayStu(correct);
return 0;
}
//删除学生信息
int DelInfo(student* del)
{
del->last->next=del->next;
if(!del->next)
free(del);
else
{
del->next->last=del->last;
del->last=NULL;
del->next=NULL;
free(del);
}
InfoNumb--;
del=NULL;
printf("\n删除成功。\n");
return 0;
}