学生信息管理系统C语言源代码

这个是用链表写的,功能应该比较全的,用链表可实现动态内存分配,方便存储人数不确定的信息

#include
#include
#include
#include
#define NULL 0
struct student
{
char bianhao[10];
char name[15];
char xingbie[6];
int nianling;
int zongfen;
char licence[20];
char address[40];
char beizhu[100];
struct student *next;
};
struct student *creat()/////创建链表
{
struct student *p,*head,*end;
head=p=end=(struct student *)malloc(sizeof(struct student));//创建一个节点
printf("====请输入学生信息====\n");
printf("请输入学生编号:");
scanf("%s",p->bianhao);
while(strcmp(p->bianhao,"0")!=0)
{
end=p;
printf("姓名:");
scanf("%s",p->name);
printf("性别:");
scanf("%s",p->xingbie);
printf("年龄:");
scanf("%d",&p->nianling);
printf("总分:");
scanf("%d",&p->zongfen);
printf("证件号:");
scanf("%s",p->licence);
printf("家庭住址:");
scanf("%s",p->address);
printf("备注:");
scanf("%s",p->beizhu);
p=(struct student *)malloc(sizeof(struct student));
end->next=p;
// end=p;
printf("请输入学生编号:");
scanf("%s",p->bianhao);
}
end->next=NULL;
return head;
}
void save(struct student *head)//保存链表为磁盘文件
{
FILE *fp;
struct student *p;
char filename[20];
int ch;
printf("请输入要保存的文件名:");
scanf("%s",filename);
if((fp=fopen(filename,"r"))!=NULL)
{
printf("该文件已经存在,是否覆盖?\n");
printf("1、覆盖. 2、不覆盖\n");
scanf("%d",&ch);
if(ch!=1)
{
printf("请重新输入要保存的文件名:");
scanf("%s",filename);
}
}
if((fp=fopen(filename,"w"))==NULL)
{
printf("创建文件失败!\n");
return;//exit(0);正常退出,执行后显示press anykey to continue;
}
// getchar();
p=head;
while(p!=NULL)
{
fprintf(fp,"%s\n",p->bianhao);
fprintf(fp,"%s\n",p->name);
fprintf(fp,"%s\n",p->xingbie);
fprintf(fp,"%d\n",p->nianling);
fprintf(fp,"%d\n",p->zongfen);
fprintf(fp,"%s\n",p->licence);
fprintf(fp,"%s\n",p->address);
fprintf(fp,"%s\n",p->beizhu);
p=p->next;
}
fputs("over",fp);
printf("文件保存成功!\n");//
getchar();
fclose(fp);
}
void output(struct student *head)//输出链表
{
struct student *p;
p=head;
if(p==NULL)
{
printf("未创建任何记录!\n");
getchar();
return; //head;
}
while(p!=NULL)//do
{
printf("编号: %s",p->bianhao);
printf("\n姓名: %s",p->name);
printf("\n性别: %s",p->xingbie);
printf("\n年龄: %d",p->nianling);
printf("\n总分: %d",p->zongfen);
printf("\n证件号: %s",p->licence);
printf("\n家庭地址: %s",p->address);
printf("\n备注: %s\n",p->beizhu);
p=p->next;
}//while(p!=NULL);
getchar();
}
struct student *openfile()//打开文件,即新创建链表读取磁盘文件
{
struct student *head,*p,*f;
FILE *

fp;
char filename[20];
int ch;
printf("请输入要打开的文件名:");//若文件保存在默认路径,读取时无需输入路径
scanf("%s",filename);
if((fp=fopen(filename,"r"))==NULL)
{
printf("打开文件失败!\n");
printf("1、重新输入文件名 2、退出\n");
scanf("%d",ch);
if(ch==1)
scanf("%s",filename);
else if(ch==2)
//getchar();
//exit(0);
return NULL;
}
head=f=p=(struct student *)malloc(sizeof(struct student));
fscanf(fp,"%s%s%s%d%d%s%s%s",p->bianhao,p->name,p->xingbie,&p->nianling,&p->zongfen,p->licence,p->address,p->beizhu);
while(!feof(fp))
{
p=(struct student *)malloc(sizeof(struct student));
f->next=p;//节点连接
fscanf(fp,"%s%s%s%d%d%s%s%s",p->bianhao,p->name,p->xingbie,&p->nianling,&p->zongfen,p->licence,p->address,p->beizhu);
if(strcmp(p->bianhao,"over")==0)
{
f->next=NULL;
printf("文件打开成功,可显示信息!\n");
getchar();
return head;
}
f=p;
}
// fclose(fp);
// f->next=NULL;
// return head;
}
void sort_hao(struct student *head)//按编号排序
{
struct student *p,*f,*t;
char ch[100];
int i;
p=f=t=head;
if(head==NULL)
{
printf("未打开任何文件!\n");
getchar();
return;
}//ok
// f=p->next;
for(p=head;p->next!=NULL;p=p->next)//由P来控制循环次数
{
for(t=head,f=t->next;t->next!=NULL;t=t->next,f=f->next)
{
if(strcmp(t->bianhao,f->bianhao)>0)
{
strcpy(ch,t->bianhao);
strcpy(t->bianhao,f->bianhao);
strcpy(f->bianhao,ch);

strcpy(ch,t->name);
strcpy(t->name,f->name);
strcpy(f->name,ch);

strcpy(ch,t->xingbie);
strcpy(t->xingbie,f->xingbie);
strcpy(f->xingbie,ch);

i=t->nianling;
t->nianling=f->nianling;
f->nianling=i;

i=t->zongfen;
t->zongfen=f->zongfen;
f->zongfen=i;

strcpy(ch,t->licence);
strcpy(t->licence,f->licence);
strcpy(f->licence,ch);

strcpy(ch,t->address);
strcpy(t->address,f->address);
strcpy(f->address,ch);

strcpy(ch,t->beizhu);
strcpy(t->beizhu,f->beizhu);
strcpy(f->beizhu,ch);
}
}
}
printf("完成排序!\n");
getchar();
// return head;
}////循环判断,若此链表共有n个节点,则共需进行n^2次循环判断……………………
void sort_name(struct student *head)//按学生姓名排序
{
struct student *p,*f,*t;
char ch[100];
int i;
p=f=t=head;
if(head==NULL)
{
printf("文件未能打开!\n");
getchar();
return;//void类型无返回值,可以用return结束子程序以返回主程序,但return绝不能带回任何值
}
// f=p->next;
for(p=head;p->next!=NULL;p=p->next)
{
for(t=head,f=t->next;t->next!=NULL;t=t->next,f=f->next)//由于创建链表是以p->bianhao=0结束的故以f->next结束
{
if(strcmp(t->name,f->name)>0)
{
strcpy(ch,t->bianhao);
strcpy(t->bianhao,f->bianhao);
strcpy(f->bianhao,ch);

strcpy(ch,t->nam

e);
strcpy(t->name,f->name);
strcpy(f->name,ch);

strcpy(ch,t->xingbie);
strcpy(t->xingbie,f->xingbie);
strcpy(f->xingbie,ch);

i=t->nianling;
t->nianling=f->nianling;
f->nianling=i;

i=t->zongfen;
t->zongfen=f->zongfen;
f->zongfen=i;

strcpy(ch,t->licence);
strcpy(t->licence,f->licence);
strcpy(f->licence,ch);

strcpy(ch,t->address);
strcpy(t->address,f->address);
strcpy(f->address,ch);

strcpy(ch,t->beizhu);
strcpy(t->beizhu,f->beizhu);
strcpy(f->beizhu,ch);
}
}
}
printf("完成排序!\n");
getchar();
// return head;
}
void search(struct student *head)//查询
{
struct student *p;
char str[20];
int i,j=0;
p=head;
if(head==NULL)
{
printf("未打开任何文件!");
getchar();
return;//
}
printf("1、按编号查询 2、按姓名查询\n");
scanf("%d",&i);
if(i==1)
printf("请输入编号:");
else
printf("请输入姓名:");
scanf("%s",str);
while(p!=NULL)
{
if(i==1)//按编号查询,只输出一个结果,需保证编号不能重复
{
if(strcmp(p->bianhao,str)==0)
{
printf("编号: %s\n姓名: %s\n性别: %s\n年龄: %d\n总分: %d\n证件号: %s\n家庭地址: %s\n备注: %s\n",p->bianhao,p->name,p->xingbie,p->nianling,p->zongfen,p->licence,p->address,p->beizhu);
j=1;
break;
}
}
if(i==2)//按姓名查询,可输出多个结果
{
if(strcmp(p->name,str)==0)
{
printf("编号: %s\n姓名: %s\n性别: %s\n年龄: %d\n总分: %d\n证件号: %s\n家庭地址: %s\n备注: %s\n",p->bianhao,p->name,p->xingbie,p->nianling,p->zongfen,p->licence,p->address,p->beizhu);
j=1;
}
}
p=p->next;
}
getchar();//显示查询结果,没有的话将一闪而过
if(j==0)
printf("查找完毕,未搜到结果!\n");
// return;
}
struct student *add(struct student *head)//添加记录
{
struct student *p,*e,*f,*h;
if(head==NULL)
{
printf("未能打开任何文件!\n");
getchar();
return NULL;
}
h=e=f=head;
p=(struct student *)malloc(sizeof(struct student));//新添节点即新添加的记录
printf("编号:");
scanf("%s",p->bianhao);
printf("姓名:");
scanf("%s",p->name);
printf("性别:");
scanf("%s",p->xingbie);
printf("年龄:");
scanf("%d",&p->nianling);
printf("总分:");
scanf("%d",&p->zongfen);
printf("证件号:");
scanf("%s",p->licence);
printf("家庭住址:");
scanf("%s",p->address);
printf("备注:");
scanf("%s",p->beizhu);
if(strcmp(f->bianhao,p->bianhao)>0)//和表头比较
{
p->next=f;
h=p;
printf("添加成功!\n");
return h;
}
if(f->next==NULL)//只有一个可用节点
{
f->next=p;
p->next=NULL;
printf("添加成功!\n");
return h;
}
do//while(f->next!=NULL)
{
f=f->next;
if(strcmp(f->bianhao,p->bianhao)>0)

{
e->next=p;
p->next=f;
printf("添加成功!\n");
return h;
}
else
e=e->

next;
// f=f->next;
}while(f->next!=NULL);
if(f->next==NULL)
{
f->next=p;
p->next=NULL;
printf("添加成功!\n");
return h;
}
}
struct student *delete_mem(struct student *head)//删除个人信息
{
struct student *p,*e;
char str[20];
if(head==NULL)
{
printf("未能打开任何文件!\n");
getchar();
return NULL;
}
p=e=head;
printf("请输入要删除的编号:");
scanf("%s",str);
if(strcmp(p->bianhao,str)==0)
{
head=head->next;
printf("删除成功!\n");
return head;
}
p=p->next;
while(p!=NULL)
{
if(strcmp(p->bianhao,str)==0)
{
if(p->next!=NULL)
e->next=p->next;
if(p->next==NULL)
e->next=NULL;
printf("删除成功!\n");
return head;
}
p=p->next;
e=e->next;
}
printf("搜索完毕,未能找到结果!\n");
return head;
}
struct student *change(struct student *head)//修改记录
{
struct student *p;
char str[20];
if(head==NULL)
{
printf("未能打开任何文件!\n");
getchar();
return NULL;
}
p=head;
printf("请输入要修改的学生的编号:");
scanf("%s",str);
while(p!=NULL)
{
if(strcmp(p->bianhao,str)==0)
{
printf("编号: %s\n姓名: %s\n性别: %s\n年龄: %d\n总分: %d\n证件号: %s\n家庭地址: %s\n备注: %s\n",p->bianhao,p->name,p->xingbie,p->nianling,p->zongfen,p->licence,p->address,p->beizhu);
printf("请按提示输入修改后的数据\n");
printf("编号:");
scanf("%s",p->bianhao);
printf("姓名:");
scanf("%s",p->name);
printf("性别:");
scanf("%s",p->xingbie);
printf("年龄:");
scanf("%d",&p->nianling);
printf("总分:");
scanf("%d",&p->zongfen);
printf("证件号:");
scanf("%s",p->licence);
printf("家庭住址:");
scanf("%s",p->address);
printf("备注:");
scanf("%s",p->beizhu);
printf("修改成功!\n");
getchar();
return head;
}
p=p->next;
}
printf("未能找到记录!\n");
return head;
}
void mima()//创建密码
{
FILE *fp;
char mima1[20],mima2[20];
int i=0;
if((fp=fopen("mima","r"))==NULL)
{
printf("密码尚未创建!\n");
// exit(0);
getchar();
do{
printf("请输入密码:");
scanf("%s",mima1);
printf("请再次输入密码:");
scanf("%s",mima2);
if(strcmp(mima1,mima2)!=0)
{
printf("两次输入的密码不一样,请重新输入\n");
i=1;
}
else
break;
}while(i);
fp=fopen("mima","w");
fprintf(fp,"%s",mima1);
printf("密码设置成功!\n");
fclose(fp);//创建文件后必须有fclose(fp),否则创建文件后数据丢失,内容为空
getchar();
return;
}
else
printf("密码已创建!\n");
getchar();
}
void delete_doc()//删除文件
{
FILE *fp;
char mima1[20],mima2[20],filename[20];
printf("请输入初始化密码:");
scanf("%s",mima1);
fp=fopen("mima","r");
fscanf(fp,"%s",mima2);
if(strcmp(mima1,mima2)==0)
{
printf("请输入要删除的文件名:");

scanf("%s",filename);
if(remove(filename)==0)//remove()函数
{
printf("删除成功!\n");
getchar();
return;
}
else{
printf("删除失败,请确认是否存在此文件.\n");
getchar();
return;}
}
else
printf("密码错误!\n");
getchar();
return;
}
void output_use()
{
printf("使用方法如下:\n");
printf("1、编辑个人信息后需要保存,否则当你再次启用该功能时将覆盖原来的信息.\n");
printf("2、保存信息后,若要在原文件中添加新的记录,则首先应启动/打开文件/功能,然后启动/添加记录/功能,添加完后保存.\n");
printf("3、除了要新建文件外,若要进行其他功能均需要先启动/打开文件/功能,才能继续,否则进行其他功能均不能对已有文件作任何改变.\n");
printf("4、编辑个人信息时,以学号为零作为结束,故学生信息中学号不能为零\n");
printf("5、由于有些限制没有编写,故在保存前至少编辑一个有效的学生信息\n");
printf("6、删除文件前需保证已经设置密码,否则出错!\n");
getchar();
// getchar();
// system("cls");
}
void output_view()
{
printf("==============================\n");
printf("0、使用说明\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("A、设置密码\n");
printf("B、删除文件\n");
printf("==============================\n");
}
void main()
{
struct student *head=NULL;
char ch;
int i,j=0;
printf(" ★☆88P Y88~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~★☆\n");
printf(" ★☆ 88 88 ★☆\n");
printf(" ★☆88 88 欢迎使用本系统,按回车键进入系统 ★☆\n");
printf(" ★☆ 8b,___,d8 ★☆\n");
printf(" ★☆★☆★☆★☆★☆★☆★☆★☆★☆★☆★☆★☆★☆★☆★☆★☆★☆\n");
getchar();
system("cls");
do{
output_view();
printf("请选择相应的操作:");
// getchar();
scanf("%c",&ch);
switch(ch)
{
case '0': output_use();break;//ok
case '1': head=creat();break;//ok
case '2': save(head);break;//ok
case '3': output(head);break;//ok
case '4': head=openfile();break;//output(head);break;
case '5': getchar();
system("cls");
printf("1、按编号排序\n");
printf("2、按姓名排序 \n");
scanf("%d",&i);
switch(i)
{
case 1: sort_hao(head);break;//ok
case 2: sort_name(head);break;//ok
default: exit(0);
}
break;
case '6': search(head);break;//ok
case '7': head=add(head);break;//ok
case '8':

head=delete_mem(head);break;//ok
case '9': head=change(head);break;//ok
case 'A': mima();break;//ok
case 'B': delete_doc();break;//ok
default: exit(0);//press anykey to continue
}
printf("按回车键返回\n");
getchar();
system("cls");
j=1;
}while(j);
}


相关主题
相关文档
最新文档