那还是把代码发上来把。
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
typedef struct Student
{
int Number;
char* Name;
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);
break;
case 3:pass=SearchInfo();
CorInfo(pass);
break;
case 4:begin:pass=SearchInfo();
operation:
printf("请选择操作选项:\n");
printf("1.删除该生信息\n");
printf("2.修改该生信息\n");
printf("3.继续查找\n");
printf("4.退出\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:return 0;
default:printf("对不起,输入有误,请重新输入。");
goto operation;
}
break;
case 5:
goto here;
default:
printf("请输入正确操作选项:");
break;
}
printf("请选择是否继续操作(y/n):");
ch=getchar();
}
here: WriteInfo();
FreeMem();
return 0;
}
//输出操作界面
int PrInterface()
{
printf("1.新增学生记录。\n");
printf("2.删除学生记录。\n");
printf("3.修改学生记录。\n");
printf("4.查找学生记录。\n");
printf("5.退出。");
printf("请输入所选操作:");
return 0;
}
//将学生信息读取到pStu的next指针里
int ReadInfo()
{
FILE* fp=fopen("D:\\test.txt","rb");
if(fp==NULL)
{
printf("打开文件失败。");
return 0;
}
fread(&InfoNumb,sizeof(int),1,fp);
if(InfoNumb==0)
{
printf("未查找到任何学生信息,请添加学生信息.\n");
AddInfo();
}
else while(!feof(fp))
{
pStu->next=(student*)malloc(1*sizeof(student));
if(fread(pStu->next,sizeof(student),1,fp)==0)
{
printf("读取文件失败\n");
exit(0);
}
pStu->next->last=pStu;
pStu=pStu->next;
pStu->next=NULL;
}
pEnd=pStu;
pStu=headNode.next;
fclose(fp);
return 0;
}
//释放所申请的内存块
int FreeMem()
{
pStu=headNode.next;
while(!pStu)
{
free(pStu);
pStu=pStu->next;
}
return 0;
}
//把链表中的信息全部写入文件中
int WriteInfo()
{
FILE* fp=fopen("D:\\test.txt","wb");
if(fp==NULL)
{
printf("\n打开文件失败。");
exit(0);
}
pStu=headNode.next;
fwrite(&InfoNumb,sizeof(int),1,fp);
while(pStu)
{
if(fwrite(pStu,sizeof(student),1,fp)!=1)
printf("\n写入文件出错。");
}
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请输入学生名字: ");
scanf("%s",pEnd->Name);
printf("\n请输入学生数学成绩: ");
scanf("%f",pEnd->Math);
printf("\n请输入学生语文成绩: ");
scanf("%f",pEnd->Chinese);
printf("\n请输入学生英语成绩: ");
scanf("%f",pEnd->English);
printf("\n请选择是否继续输入(y/n): ");
InfoNumb++;
ch=getchar();
}
return 0;
}
//显示学生信息
int DisplayStu(student* stu)
{
printf("\n学生学号:%d",stu->Number);
printf("\n学生名字:%s",stu->Name);
printf("\n学生数学成绩:%f",stu->Math);
printf("\n学生语文成绩:%f",stu->Chinese);
printf("\n学生英语成绩:%f",stu->English);
return 0;
}
//查找学生信息
student* SearchInfo()
{
int chioce;
student* display=NULL;
while(1)
{
again:printf("\n1.按学生学号进行查找。");
printf("\n2.按学生名字进行查找。 选择:");
scanf("%d",chioce);
switch(chioce)
{
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* name;
scanf("%s",name);
display=SchByName(name);
if(!display)
goto again;
else DisplayStu(display);
break;}
default:printf("\n对不起,输入有误,请重新输入。");
goto again;
}
}
return display;
}
//按学生学号查找
student* SchByNum(int num)
{
pStu=headNode.next;
int sNum=pStu->Number;
while(num!=sNum)
{
if(!(pStu->next))
{
printf("\n对不起,您输入的学生号有误,请重新输入:");
return 0;
}
else
{
pStu=pStu->next;
sNum=pStu->Number;
}
}
return pStu;
}
//通过姓名查找学生信息
student* SchByName(char* name)
{
pStu=headNode.next;
while(*name!=*(pStu->Name))
{
if(!(pStu->next))
{
printf("\n对不起,您输入的学生号有误,请重新输入:");
return 0;
}
else
{
pStu=pStu->next;
}
}
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:");
ch=getchar();
}
DisplayStu(correct);
return 0;
}
//删除学生信息
int DelInfo(student* del)
{
del->last->next=del->next;
del->next->last=del->last;
del->last=NULL;
del->next=NULL;
InfoNumb--;
free(del);
return 0;
}