#include <string.h>
#include <stdio.h>
#include <stdlib.h>
struct student/*结构体的定义*/
{int xh;/*学号*/
char xm[10];/*姓名*/
char xb[10];/*性别*/
char sr[20];/*生日*/
char xbb[20];/*所在系*/
int sh;/*宿舍号*/
int fy;/*费用*/
char sz[10];/*宿舍长*/
struct student *next;/*指向下一个接点的指针*/
};
int table=0;
main()/*主函数*/
{
struct student *head=NULL;
struct student *createmain(struct student *head);/*新建函数的声明*/
struct student *displaymain(struct student *head); /*查询函数的声明*/
struct student *addmain(struct student *head); /*添加函数的声明*/
struct student *delmain(struct student *head); /*删除函数的声明*/
struct student *repairmain(struct student *head); /*修改函数的声明*/
struct student *loadmain(struct student *head);
struct student *savemain(struct student *head);
int pd;/*实现循环的变量*/
head=loadmain(head);
do{
clrscr();
pd=1;
printf(" 欢迎您使用宿舍管理系统2.0测试版\n");
printf(" 本软件由(解沣凡)制做\n");
printf(" 2006年4月9日开发完成\n");
printf("说明:本程序还支持数据的保存,属于测试版本,望大家发现致命毛病时能即使联系本人,本人非常感谢,谢谢大家,祝大家发大财。联系QQ:6997467\n");
printf("********************************************************************************\n");
printf("当前记录有<%d>\n",table) ;
printf("请选择我可以为你服务的操作:\n");
printf("<0>建立 ");
printf("<1>查询 ");
printf("<2>添加 ");
printf("<3>删除 ");
printf("<4>修改 ");
printf("<5>退出 ");
printf("\n");
switch(getch())
{
case '0' : head=createmain(head);;break;
case '1' : head=displaymain(head);;break;
case '2' : head=addmain(head);break;
case '3' : head=delmain(head);break;
case '4' : head=repairmain(head);break;
case '5' : printf("\n\n谢谢您的操作,再见!\n");pd=0;break;
default :pd=0;
}
}while(pd != 0);
head=savemain(head);
}
struct student *addmain(struct student *head)/*添加记录的函数*/
{
struct student *p1,*p2;
int n,i;
clrscr();
if(head != NULL)
{
printf("输入你要添加的记录个数\n");
scanf("%d",&n);
p2=head;
while(p2->next != NULL)
p2=p2->next;
for(i=1;i<=n;i++)
{p1=(struct student *) malloc(sizeof(struct student));
table++;
printf("<%d>个学生\n",i);
printf("学号:");
scanf("%d",&p1->xh);
printf("姓名:");
scanf("%s",p1->xm);
printf("性别:");
scanf("%s",p1->xb);
printf("生日:");
scanf("%s",p1->sr);
printf("费用:");
scanf("%d",&p1->fy);
printf("宿舍号:");
scanf("%d",&p1->sh);
printf("宿舍长:");
scanf("%s",p1->sz);
printf("系别:");
scanf("%s",p1->xbb);
p2->next=p1;
p2=p2->next;
p2->next=NULL;}}
else printf("您还没有建立数据,请先建立数据");
return(head);
}
struct student *createmain(struct student *head)/*建立记录*/
{
struct student *p1,*p2;
int n,i;
p1=p2=head;
if(head==NULL)
{
printf("输入你想建立的记录个数\n");
scanf("%d",&n);
if(n == 0)
printf("输入有错误\n");
for(i=1;i<=n;i++)
{
p1=(struct student *)malloc(sizeof(struct student));
table++;
printf("<%d>个学生\n",i);
printf("学号:");
scanf("%d",&p1->xh);
printf("姓名:");
scanf("%s",p1->xm);
printf("性别:");
scanf("%s",p1->xb);
printf("费用:");
scanf("%d",&p1->fy);
printf("生日:");
scanf("%s",p1->sr);
printf("宿舍号:");
scanf("%d",&p1->sh);
printf("系别:");
scanf("%s",p1->xbb);
printf("宿舍长:");
scanf("%s",p1->sz);
if(head == NULL) p2=p1,head=p1 ;
else {p2->next=p1 ; p2=p2->next;}
p2->next=NULL;
}
}
else
printf("已经有了建立,请查询后再操作\n");
return(head);
}
struct student *delmain(struct student *head)/*删除记录*/
{
struct student *p1,*p2;
int n,xx=1,yy=0;
clrscr();
if(head== NULL)
{printf("您还没有数据建立\n"); return(head);}
do{
yy=0;
printf("输入你要删除的学号\n");
scanf("%d",&n);
getchar();
p1=head;
p2=p1->next;
while( p2 != NULL)
{
if(p2->xh == n) {p1->next=p2->next;free(p2);p2=p1->next;yy++;table--;}
else {p1=p2;p2=p2->next;}
}
if(head->xh == n){p1=head;head=head->next;free(p1);yy++;table--;}
printf(" 是否继续操作,已删除(%d)条记录\n",yy);
printf("是否退出删除操作 ? (1:继续操作,0:退出操作)\n");
scanf("%d",&xx);
}while(xx != 0);
return(head);
}
struct student *repairmain(struct student *head)/*修改记录*/
{
struct student *p1;
int n,xx=1;
clrscr();
do{
printf("输入你要修改的学生学号\n");
scanf("%d",&n);
p1=head;
while(p1 != NULL)
{if(p1->xh == n)
{printf("学号=%-8d 姓名=%-8s 性别=%-8s 生日=%-8s\n系别=%-8s 费用=%-8d 宿舍号=%-6d 宿舍长=%-8s\n",p1->xh,p1->xm,p1->xb,p1->sr,p1->xbb,p1->fy,p1->sh,p1->sz);
printf("修改为\n");
printf("学号:");
scanf("%d",&p1->xh);
printf("姓名:");
scanf("%s",p1->xm);
printf("性别:");
scanf("%s",p1->xb);
printf("生日:");
scanf("%s",p1->sr);
printf("费用:");
scanf("%d",&p1->fy);
printf("宿舍号:");
scanf("%d",&p1->sh);
printf("宿舍长:");
scanf("%s",p1->sz);
printf("系别:");
scanf("%s",p1->xbb);
printf("修改后:\n");
printf("学号=%-8d 姓名=%-8s 性别=%-8s 生日=%-8s\n系别=%-8s 费用=%-8d 宿舍号=%-6d 宿舍长=%-8s\n",p1->xh,p1->xm,p1->xb,p1->sr,p1->xbb,p1->fy,p1->sh,p1->sz);
p1=p1->next;}
p1=p1->next;
}
printf("是否继续修改操作 ? (1:继续操作,0:退出操作)\n");
scanf("%d",&xx);
}while(xx != 0);
return(head);
}
struct student *displaymain(struct student *head)/*查询函数*/
{
int pd;
struct student *head2=head;
struct student *xhmain(struct student *head2);
struct student *xmmain(struct student *head2);
struct student *xbbmain(struct student *head2);
struct student *szmain(struct student *head2);
struct student *shmain(struct student *head2);
struct student *fymain(struct student *head2);
struct student *xbmain(struct student *head2);
struct student *srmain(struct student *head2);
struct student *allmain(struct student *head);
clrscr();
do
{ pd=1;
printf("<0>学号查询 ");
printf("<1>性别查询 ");
printf("<2>姓名查询 ");
printf("<3>系别查询 ");
printf("<4>舍长查询 ");
printf("<5>舍号查询 \n");
printf("<6>费用查询 ");
printf("<7>生日查询 ");
printf("<8>全部查询 ");
printf("<9>退出查询 \n");
printf("等待您输入:");
switch(getch())
{case '0' :head=xhmain(head2);break;
case '1' :head=xbmain(head2);break;
case '2' :head=xmmain(head2);break;
case '3' :head=xbbmain(head2);break;
case '4' :head=szmain(head2);break;
case '5' :head=shmain(head2);break;
case '6' :head=fymain(head2);break;
case '7' :head=srmain(head2);break;
case '8' :head=allmain(head2);break;
case '9' :pd=0;break;
default :printf("欢迎再次查询\n");pd=0;}
}while(pd != 0);
return(head);
}
struct student *allmain(struct student *head2)/*全部查询*/
{
int zz;
struct student *p1;
p1=head2;
if(p1!=NULL)
{
clrscr();
printf("你的查询结果:\n");
for(zz=0;1;){ zz++;
if(p1 == NULL) break;
printf("(%3d)学号=%-8d 姓名=%-8s 性别=%-8s 生日=%-8s\n 系别=%-8s 费用=%-8d 宿舍号=%-6d 宿舍长=%-8s\n",zz,p1->xh,p1->xm,p1->xb,p1->sr,p1->xbb,p1->fy,p1->sh,p1->sz);
p1=p1->next;
}
}
else
printf("没有可查询的记录,请建立数据\n");
return (head2);
}
struct student *xhmain(struct student *head2)/*学号查询*/
{
struct student *p1;
int n,zz=0;
clrscr();
printf("输入要查找的学号\n");
scanf("%d",&n);
if(head2 != NULL)
{
printf("你的查询结果:\n");
p1=head2;
do
{if(p1->xh == n) {zz++;printf("(%3d)学号=%-8d 姓名=%-8s 性别=%-8s 生日=%-8s\n 系别=%-8s 费用=%-8d 宿舍号=%-6d 宿舍长=%-8s\n",zz,p1->xh,p1->xm,p1->xb,p1->sr,p1->xbb,p1->fy,p1->sh,p1->sz);
}
p1=p1->next;}
while(p1 != NULL);}
else printf("没有可查询的记录,请建立数据\n");
return(head2);
}
struct student *shmain(struct student *head2)/*宿舍号查询*/
{
struct student *p1;
int n,zz=0;
clrscr();
printf("输入要查找的宿舍号\n");
scanf("%d",&n);
if(head2 != NULL)
{printf("你的查询结果:\n");
p1=head2;
do
{if(p1->sh == n) {zz++; printf("(%3d)学号=%-8d 姓名=%-8s 性别=%-8s 生日=%-8s\n 系别=%-8s 费用=%-8d 宿舍号=%-6d 宿舍长=%-8s\n",zz,p1->xh,p1->xm,p1->xb,p1->sr,p1->xbb,p1->fy,p1->sh,p1->sz);
}p1=p1->next;
}
while(p1 != NULL);}
else printf("没有可查询的记录,请建立数据\n");
return(head2);
}
struct student *fymain(struct student *head2)/*费用查询*/
{
struct student *p1;
int n,zz=0;
clrscr();
printf("输入要查找的宿舍费用\n");
scanf("%d",&n);
if(head2 != NULL)
{printf("你的查询结果:\n");
p1=head2;
do
{if(p1->fy == n) {zz++; printf("(%3d)学号=%-8d 姓名=%-8s 性别=%-8s 生日=%-8s\n 系别=%-8s 费用=%-8d 宿舍号=%-6d 宿舍长=%-8s\n",zz,p1->xh,p1->xm,p1->xb,p1->sr,p1->xbb,p1->fy,p1->sh,p1->sz);
}
p1=p1->next;}
while(p1 != NULL);}
else printf("没有可查询的记录,请建立数据\n");
return(head2);
}
struct student *xmmain(struct student *head2)/*姓名查询*/
{
struct student *p1;
int zz=0;
char xx[20];
clrscr();
printf("输入你要查找的姓名\n");
scanf("%s",xx);
if(head2 != NULL)
{printf("你的查询结果:\n");
p1=head2;
do
{if( !strcmp(p1->xm,xx)){zz++;
printf("(%3d)学号=%-8d 姓名=%-8s 性别=%-8s 生日=%-8s\n 系别=%-8s 费用=%-8d 宿舍号=%-6d 宿舍长=%-8s\n",zz,p1->xh,p1->xm,p1->xb,p1->sr,p1->xbb,p1->fy,p1->sh,p1->sz);
}p1=p1->next;
}
while(p1 != NULL);}
else printf("没有可查询的记录,请建立数据\n");
return(head2);
}
struct student *srmain(struct student *head2)/*生日查询*/
{
struct student *p1;
char xx[20];
int zz=0;
clrscr();
printf("输入你要查找的生日\n");
scanf("%s",xx);
if(head2 != NULL)
{printf("你的查询结果:\n");
p1=head2;
do
{if( !strcmp(p1->sr,xx))
{zz++;printf("(%3d)学号=%-8d 姓名=%-8s 性别=%-8s 生日=%-8s\n 系别=%-8s 费用=%-8d 宿舍号=%-6d 宿舍长=%-8s\n",zz,p1->xh,p1->xm,p1->xb,p1->sr,p1->xbb,p1->fy,p1->sh,p1->sz);
}p1=p1->next;
}
while(p1 != NULL);}
else printf("没有可查询的记录,请建立数据\n");
return(head2);
}
struct student *xbmain(struct student *head2)/*性别查询*/
{
struct student *p1;
char xx[20];
int zz=0;
clrscr();
printf("输入你要查找的性别\n");
scanf("%s",xx);
if(head2 != NULL)
{printf("你的查询结果:\n");
p1=head2;
do
{if(!strcmp(p1->xb,xx)) {zz++;
printf("(%3d)学号=%-8d 姓名=%-8s 性别=%-8s 生日=%-8s\n 系别=%-8s 费用=%-8d 宿舍号=%-6d 宿舍长=%-8s\n",zz,p1->xh,p1->xm,p1->xb,p1->sr,p1->xbb,p1->fy,p1->sh,p1->sz);
}p1=p1->next;
}
while(p1 != NULL);}
else printf("没有可查询的记录,请建立数据\n");
return(head2);}
struct student *xbbmain(struct student *head2)/*所在系查询*/
{
struct student *p1;
char xx[20]; int zz=0;
clrscr();
printf("输入你要查找的系别\n");
scanf("%s",xx);
if(head2 != NULL)
{printf("你的查询结果:\n");
p1=head2;
do
{if(!strcmp(p1->xbb,xx)) {zz++;
printf("(%3d)学号=%-8d 姓名=%-8s 性别=%-8s 生日=%-8s\n 系别=%-8s 费用=%-8d 宿舍号=%-6d 宿舍长=%-8s\n",zz,p1->xh,p1->xm,p1->xb,p1->sr,p1->xbb,p1->fy,p1->sh,p1->sz);
}p1=p1->next;
}
while(p1 != NULL);}
else printf("没有可查询的记录,请建立数据\n");
return(head2);}
struct student *szmain(struct student *head2)/*宿舍长查询*/
{
struct student *p1;
char xx[20];
int zz=0;
clrscr();
printf("输入你要查找的舍长\n");
scanf("%s",xx);
if(head2 != NULL)
{printf("你的查询结果:\n");
p1=head2;
do
{if(!strcmp(p1->sz,xx))
{zz++;printf("(%3d)学号=%-8d 姓名=%-8s 性别=%-8s 生日=%-8s\n 系别=%-8s 费用=%-8d 宿舍号=%-6d 宿舍长=%-8s\n",zz,p1->xh,p1->xm,p1->xb,p1->sr,p1->xbb,p1->fy,p1->sh,p1->sz);
}p1=p1->next;
}
while(p1 != NULL);}
else printf("没有可查询的记录,请建立数据\n");
return(head2);}
struct student *savemain(struct student *head) /*数据的保存函数*/
{FILE *fp,*fp2;
struct student *p1;
int i;
p1=head;
if((fp=fopen("c:\data.txt","wb+"))==NULL)
{printf("没有完成文件的操作\n");
return (head);}
for(i=0;i<table;i++)
{
if(fwrite(p1,sizeof(struct student),1,fp) != 1)
printf("没有完成操作");
p1=p1->next;
}
fclose(fp);
if((fp2=fopen("c:\data2.txt","wb"))==NULL)
{printf("没有完成文件的操作\n");
return (head);}
else putc(table,fp2);
printf("<%d>",table);
fclose(fp2);
printf("条记录保存完毕");
return(head) ;
}
struct student *loadmain(struct student *head) /*数据的读取函数*/
{FILE *fp,*fp2;
int i;
struct student *p1,*p2;
if((fp2=fopen("c:\data2.txt","rb")) == NULL)
{printf("没有读取成功\n");return (head);}
table=getc(fp2);
printf("%d条记录被加载\n",table);
if(table > 0){
fp=fopen("c:\data.txt","rb") ;
p2=p1=head;
for(i=0;i<table;i++)
{ p1=(struct student *)malloc(sizeof(struct student));
fread(p1,sizeof(struct student),1,fp) ;
if(head == NULL) { p2=p1,head=p1 ; }
else {p2->next=p1 ; p2=p2->next;}
p2->next=NULL;} }
else printf("没有文件供您打开,可以建立新的数据\n");
return (head);
}