#include <stdio.h>
#include<stdlib.h>
#include<string.h> typedef struct student {
int number;
char name[];
char faculty[];
char classes[];
struct student * link;
}STU; STU * CreateLink();
void WriteLink(STU *); void input();
void del();
void del_number(STU *);
void del_name(STU *);
void del_faculty(STU *);
void del_classes(STU *);
void search();
void search_number(STU *);
void search_name(STU *);
void search_faculty(STU *);
void search_classes(STU *);
void sort();
void show();
void edit(); void main()
{
char c;
FILE * fp; if((fp=fopen("student.dat","r"))==NULL)
{
fp=fopen("student.dat","w");
fclose(fp);
} do
{
system("cls");
printf("\n\n\n");
printf("\t\t╔═════════════════════╗\n");
printf("\t\t║ ║\n");
printf("\t\t║ 1: 添加一个学生 ║\n");
printf("\t\t║ 2: 删除一个学生 ║\n");
printf("\t\t║ 3: 查找一个学生 ║\n");
printf("\t\t║ 4: 学生信息排序 ║\n");
printf("\t\t║ 5: 修改学生信息 ║\n");
printf("\t\t║ 6: 查看学生信息 ║\n");
printf("\t\t║ 9: 退出 ║\n");
printf("\t\t║ ║\n");
printf("\t\t╚═════════════════════╝\n");
printf("\t\t \n");
printf("\t\t请选择输入选项[1\\2\\3\\4\\5\\6\\9]:> ");
do
{
c=getchar();
}while(c!=''&&c!=''&&c!=''&&c!=''&&c!=''&&c!=''&&c!='');
getchar(); switch(c)
{
case '': input(); break;
case '': del(); break;
case '': search(); break;
case '': sort(); break;
case '': edit(); break;
case '': show(); break;
case '': exit();
}
system("cls");
}while();
} STU * CreateLink()
{
FILE *fp;
STU * head,*p1, *p2; if((fp=fopen("student.dat","r"))==NULL)
{
printf("打开文件错误,退出。");
exit();
}
head=p1=p2=(STU *)malloc(sizeof(STU));
while(!feof(fp))
{
if(fscanf(fp,"%d %s %s %s\n",&p2->number,p2->name,p2->faculty,p2->classes)==EOF)
{
free(head);
return NULL;
}
p1->link=p2;
p1=p2;
p2=(STU *)malloc(sizeof(STU));
}
p1->link=NULL;
free(p2);
fclose(fp); return head;
} void WriteLink(STU * p2)
{
FILE *fp; if((fp=fopen("student.dat","w"))==NULL)
{
printf("打开文件错误,退出。");
exit();
}
while(p2!=NULL)
{
fprintf(fp,"%d %s %s %s\n",p2->number,p2->name,p2->faculty,p2->classes);
p2=p2->link;
}
fclose(fp);
} void input()
{
STU * head, *p1, *p2;
system("cls"); head=CreateLink();
p1=head; if(p1!=NULL)
while(p1->link!=NULL)
p1=p1->link; p2=p1;
printf("\n\n\n\t\t输入待添加学生(-1 结束):\n");
while()
{
p2=(STU *)malloc(sizeof(STU));
if(p1==NULL) head=p1=p2;
printf("输入学号:");
scanf("%d",&p2->number);
if(p2->number==-) break;
printf("输入 姓名、系别、班级:");
scanf("%s%s%s",p2->name,p2->faculty,p2->classes);
p1->link=p2;
p1=p2;
}
p1->link=NULL;
free(p2); WriteLink(head); free(head);
} void del()
{
STU * head=CreateLink();
char c;
do
{
system("cls");
printf("\n\n\n");
printf("\t\t╔═════════════════════╗\n");
printf("\t\t║ ║\n");
printf("\t\t║ 请输入删除内容: ║\n");
printf("\t\t║ ║\n");
printf("\t\t║ 1: 按照学号删除 ║\n");
printf("\t\t║ 2: 按照姓名删除 ║\n");
printf("\t\t║ 3: 按照系别删除 ║\n");
printf("\t\t║ 4: 按照班级删除 ║\n");
printf("\t\t║ 9: 返回主菜单 ║\n");
printf("\t\t║ ║\n");
printf("\t\t╚═════════════════════╝\n");
printf("\t\t \n");
printf("\t\t请选择输入选项[0\\1\\2\\3\\4\\9]:> ");
do
{
c=getchar();
}while(c!=''&&c!=''&&c!=''&&c!=''&&c!='');
getchar(); switch(c)
{
case '': del_number(head); break;
case '': del_name( head); break;
case '': del_faculty(head); break;
case '': del_classes(head); break;
case '': return;
}
printf("\t\t按任意键返回主菜单:\n");
getchar();
system("cls");
}while(); free(head);
}
void del_number(STU * head)
{
STU * p1, *p2, temp;
system("cls");
do
{
printf("\n\n\n\t\t输入要删除学生学号(-1 结束):");
scanf("%d",&temp.number);
getchar();
p1=p2=head;
while(p2!=NULL)
{
if(head->number==temp.number)
{
head=head->link;
p1->link=NULL;
free(p1);
p1=p2=head;
}
else
{
if(p2->number==temp.number)
{
p1->link=p2->link;
p2->link=NULL;
free(p2);
p2=p1;
}
p1=p2;
p2=p2->link;
}
}
}while(temp.number!=-);
WriteLink(head);
}
void del_name(STU * head)
{
STU * p1, *p2, temp;
system("cls");
do
{
printf("\n\n\n\t\t输入要删除学生姓名(EOF 结束):");
if(scanf("%s",temp.name)==EOF) break;
getchar();
p1=p2=head;
while(p2!=NULL)
{
if(strcmp(head->name,temp.name)==)
{
head=head->link;
p1->link=NULL;
free(p1);
p1=p2=head;
}
else
{
if(strcmp(head->name,temp.name)==)
{
p1->link=p2->link;
p2->link=NULL;
free(p2);
p2=p1;
}
p1=p2;
p2=p2->link;
}
}
}while();
WriteLink(head);
}
void del_faculty(STU * head)
{
STU * p1, *p2, temp;
system("cls");
do
{
printf("\n\n\n\t\t输入要删除学生系别(EOF 结束):");
if(scanf("%s",temp.faculty)==EOF) break;
getchar();
p1=p2=head;
while(p2!=NULL)
{
if(strcmp(head->faculty,temp.faculty)==)
{
head=head->link;
p1->link=NULL;
free(p1);
p1=p2=head;
}
else
{
if(strcmp(head->faculty,temp.faculty)==)
{
p1->link=p2->link;
p2->link=NULL;
free(p2);
p2=p1;
}
p1=p2;
p2=p2->link;
}
}
}while();
WriteLink(head);
}
void del_classes(STU * head)
{
STU * p1, *p2, temp;
system("cls");
do
{
printf("\n\n\n\t\t输入要删除学生班级(EOF 结束):");
if(scanf("%s",temp.classes)==EOF) break;
getchar();
p1=p2=head;
while(p2!=NULL)
{
if(strcmp(head->classes,temp.classes)==)
{
head=head->link;
p1->link=NULL;
free(p1);
p1=p2=head;
}
else
{
if(strcmp(head->classes,temp.classes)==)
{
p1->link=p2->link;
p2->link=NULL;
free(p2);
p2=p1;
}
p1=p2;
p2=p2->link;
}
}
}while();
WriteLink(head);
} void search()
{
STU * head=CreateLink(); char c;
do
{
system("cls");
printf("\n\n\n");
printf("\t\t╔═════════════════════╗\n");
printf("\t\t║ ║\n");
printf("\t\t║ 请输入查找内容: ║\n");
printf("\t\t║ ║\n");
printf("\t\t║ 1: 按照学号查找 ║\n");
printf("\t\t║ 2: 按照姓名查找 ║\n");
printf("\t\t║ 3: 按照系别查找 ║\n");
printf("\t\t║ 4: 按照班级查找 ║\n");
printf("\t\t║ 9: 返回主菜单 ║\n");
printf("\t\t║ ║\n");
printf("\t\t╚═════════════════════╝\n");
printf("\t\t \n");
printf("\t\t请选择输入选项[0\\1\\2\\3\\4\\9]:> ");
do
{
c=getchar();
}while(c!=''&&c!=''&&c!=''&&c!=''&&c!='');
getchar(); switch(c)
{
case '': search_number(head); break;
case '': search_name( head); break;
case '': search_faculty(head); break;
case '': search_classes(head); break;
case '': return;
}
printf("\t\t按任意键返回主菜单:\n");
getchar();
system("cls");
}while();
}
void search_number(STU * head)
{
STU temp,*p2=head;
system("cls"); do
{
printf("\n\n\n\t\t输入要查找学生学号(-1 结束):\n\t\t");
scanf("%d",&temp.number);
printf("\t\t查询结果:\n");
while(p2!=NULL)
{
if(p2->number==temp.number)
printf("\t\t%d %s %s %s",p2->number,p2->name,p2->faculty,p2->classes);
p2=p2->link;
}
p2=head;
}while(temp.number!=-); }
void search_name(STU * head)
{
STU temp, *p2=head;
system("cls"); printf("\n\n\n\t\t输入要查找学生姓名(EOF 结束):\n\t\t");
do
{
if(scanf("%s",temp.name)==EOF) return;
printf("\t\t查询结果:\n");
while(p2!=NULL)
{
if(strcmp(p2->name,temp.name)==)
printf("\t\t%d %s %s %s\n\t\t",p2->number,p2->name,p2->faculty,p2->classes);
p2=p2->link;
}
p2=head;
getchar();
printf("\n\n\n\t\t输入要查找学生姓名(EOF 结束):\n\t\t");
}while();
}
void search_faculty(STU * head)
{
STU temp, *p2=head;
system("cls"); printf("\n\n\n\t\t输入要查找学生系别(EOF 结束):\n\t\t");
do
{
if(scanf("%s",temp.faculty)==EOF) return;
printf("\t\t查询结果:\n");
while(p2!=NULL)
{
if(strcmp(p2->faculty,temp.faculty)==)
printf("\t\t%d %s %s %s\n\t\t",p2->number,p2->name,p2->faculty,p2->classes);
p2=p2->link;
}
p2=head;
getchar();
printf("\n\n\n\t\t输入要查找学生系别(EOF 结束):\n\t\t");
}while();
}
void search_classes(STU * head)
{
STU temp, *p2=head;
system("cls"); printf("\n\n\n\t\t输入要查找学生班级(EOF 结束):\n\t\t");
do
{
if(scanf("%s",temp.classes)==EOF) return;
printf("\t\t查询结果:\n");
while(p2!=NULL)
{
if(strcmp(p2->classes,temp.classes)==)
printf("\t\t%d %s %s %s\n\t\t",p2->number,p2->name,p2->faculty,p2->classes);
p2=p2->link;
}
p2=head;
getchar();
printf("\n\n\n\t\t输入要查找学生班级(EOF 结束):\n\t\t");
}while();
} void sort()
{
STU * head, *p1, *p2;
STU temp; head=CreateLink(); system("cls");
printf("\n\n\n\t\t按照学号排序(正序):\n"); //常排序
for(p1=head;p1!=NULL;p1=p1->link)
for(p2=p1->link;p2!=NULL;p2=p2->link)
if(p1->number>p2->number)
{
temp.number=p1->number;
strcpy(temp.name,p1->name);
strcpy(temp.faculty,p1->faculty);
strcpy(temp.classes,p1->classes);
p1->number=p2->number;
strcpy(p1->name,p2->name);
strcpy(p1->faculty,p2->faculty);
strcpy(p1->classes,p2->classes);
p2->number=temp.number;
strcpy(p2->name,temp.name);
strcpy(p2->faculty,temp.faculty);
strcpy(p2->classes,temp.classes);
} WriteLink(head); printf("\t\t排序完毕,输出排序结果:");
show();
free(head);
} void show()
{
STU * head, *p2; head=CreateLink(); printf("\n\n\n\t\t学号 姓名 系别 班级:\n");
p2=head;
while(p2!=NULL)
{
printf("\t\t%d %s %s %s\n",p2->number,p2->name,p2->faculty,p2->classes);
p2=p2->link;
}
getchar();
} void edit()
{
STU * head, *p2;
STU temp;
head=CreateLink();
p2=head; system("cls");
do
{
printf("\n\n\n\t\t输入要查找学生学号(-1 结束):\n");
scanf("%d",&temp.number);
getchar();
while(p2!=NULL)
{
if(p2->number==temp.number)
{
printf("请输入修改后的内容:姓名、系别、班级:\n");
scanf("%s %s %s",p2->name,p2->faculty,p2->classes);
}
p2=p2->link;
}
}while(temp.number!=-); WriteLink(head);
free(head);
}