程序中定义了太多的全局变量,如何修改
#include<stdio.h> #include<string.h>
#include<stdlib.h>
#include<ctype.h>
#define STACK_INIT_SIZE 10
#define OK 1
#define TRUE 1
#define ERROR 0
struct student {
int carnum;
char lendbook[10];
}student[1000];
struct book
{
int booknum;
char bookname[20];
float bookprice;
char bookcreat[20];
int turefalse;
}book[1000];
struct car
{
int carnum;
char sex[5];
char studentname[20];
int studentclass;
}car[1000];
findbook()
{
FILE *fp;
char bookname[10];
int i;
fp=fopen("book.txt","r");
printf("请输入你要查找的书名:\n");
scanf("%s",bookname);
for(i=0;fread(&book[i],sizeof(struct book),1,fp)!=0;i++)
{
if(strcmp(bookname,book[i].bookname)==0)
{
if(book[i].turefalse==1)
{
printf(" 书号 书名 作者 \n %d %s %s \n",book[i].booknum,book[i].bookname,book[i].bookcreat);
printf("此书现在无人借阅\n按任意键返回\n"); }
else {printf("这本书已经有人借出\n");fclose(fp);return 0;}
fclose(fp);
return 0;
}
}
printf("没有你要查询的书籍!\n");
fclose(fp);
return 0;
}
findbook1()
{
FILE *fp;
char bookcreat[10];
int i;
fp=fopen("book.txt","r");
printf("请输入你要查找的作者名:\n");
scanf("%s",bookcreat);
for(i=0;fread(&book[i],sizeof(struct book),1,fp)!=0;i++)
{
if(strcmp(bookcreat,book[i].bookcreat)==0)
{
if(book[i].turefalse==1)
{
printf(" 书号 书名 作者 \n %d %s %s \n",book[i].booknum,book[i].bookname,book[i].bookcreat);
printf("此书现在无人借阅\n按任意键返回\n");
}
else {printf("这本书已经有人借出!\n");
fclose(fp);
return 0;
}
fclose(fp);
return 0;
}
}
printf("没有你要查询的书籍\n");
fclose(fp);
return 0;
}
chabook()
{
char ch5;
do
{
printf(" 1: 按书名查找 \n");
printf(" 2: 按作者查找 \n");
printf(" 0: 返回 \n");
printf("请输入0━2:\n");
ch5=getch();
switch(ch5)
{
case '1':findbook();getch();break;
case '2':findbook1();getch();break;
case '0':break;
default:printf("无此操作!\n");getch();break;
}
}while(ch5!='0');
return 0;
}
lendbook()
{
FILE *fp,*fp2;
int i,n;
int carnum;
printf("请输入借书人的:\n卡号:\n");
scanf("%d",&carnum);
fp=fopen("car.txt","r");
for(i=0;fread(&car[i],sizeof(struct car),1,fp)!=0;i++)
{
if(car[i].carnum==carnum)
{
n=i;
fclose(fp);
printf("书名:\n");
scanf("%s",student[n].lendbook);
fp=fopen("book.txt","r");
for(i=0;fread(&book[i],sizeof(struct book),1,fp)!=0;i++)
{
if(strcmp(book[i].bookname,student[n].lendbook)==0)
{
if(book[i].turefalse==0) {printf("对不起,此书有人借出,请借其他书\n");fclose(fp);getch();return 0;}
else
fclose(fp);
fp=fopen("record.txt","a+");
student[n].carnum=carnum;
fwrite(&student[n],sizeof(struct student),1,fp);
fclose(fp);
fp=fopen("book.txt","r");
fp2=fopen("bookl.txt","w");
for(i=0;fread(&book[i],sizeof(struct book),1,fp)!=0;i++)
{
if(strcmp(book[i].bookname,student[n].lendbook)==0)
{
book[i].turefalse=0;
fwrite(&book[i],sizeof(struct book),1,fp2);
continue;
}
fwrite(&book[i],sizeof(struct book),1,fp2);
}
fclose(fp);
fclose(fp2);
fp=fopen("book.txt","w");
fp2=fopen("bookl.txt","r");
for(i=0;fread(&book[i],sizeof(struct book),1,fp2)!=0;i++)
{
fwrite(&book[i],sizeof(struct book),1,fp);
}
fclose(fp);
fclose(fp2);
fopen("bookl.txt","w");
fclose(fp2);
printf("借书完毕,按任意键返回...\n");
getch();
return 0;
}
}
printf("不存在这样的书,任意键返回...\n");
fclose(fp);
getch();
return 0;
}
}
printf("你的卡号不存在,请申请新卡,按任意键返回...\n");
fclose(fp);
getch();
return 0;}
huanbook()
{
FILE *fp,*fp2;
int i,n;
int carnum;
char lendbook[10];
printf("\t\t(^_^)欢迎进入还书界面(^_^)");
printf("\n请你输入你的\n卡号:\n");
scanf("%d",&carnum);
fp=fopen("car.txt","r");
for(i=0;fread(&car[i],sizeof(struct car),1,fp)!=0;i++)
{
if(car[i].carnum==carnum)
{
n=i;
fclose(fp);
printf("请输入你要还的书的名字:\n");
scanf("%s",lendbook);
fp=fopen("record.txt","r");
for(i=0;fread(&student[i],sizeof(struct student),1,fp)!=0;i++)
{
if(strcmp(student[i].lendbook,lendbook)==0)
{
fclose(fp);
fp=fopen("record.txt","r");
fp2=fopen("bookl.txt","w");
for(i=0;fread(&student[i],sizeof(struct student),1,fp)!=0;i++)
{
if(strcmp(student[i].lendbook,lendbook)==0)
{
continue;
}
fwrite(&student[i],sizeof(struct student),1,fp2);
}
fclose(fp);
fclose(fp2);
fp=fopen("record.txt","w");
fp2=fopen("bookl.txt","r");
for(i=0;fread(&student[i],sizeof(struct student),1,fp2)!=0;i++)
{
fwrite(&student[i],sizeof(struct student),1,fp);
}
fclose(fp);
fclose(fp2);
fopen("bookl.txt","w");
fclose(fp2);
fp=fopen("book.txt","r");
fp2=fopen("bookl.txt","w");
for(i=0;fread(&book[i],sizeof(struct book),1,fp)!=0;i++)
{
if(i==n)
{
book[i].turefalse=1;
fwrite(&book[i],sizeof(struct book),1,fp2);
continue;
}
fwrite(&book[i],sizeof(struct book),1,fp2);
}
fclose(fp);
fclose(fp2);
fp=fopen("book.txt","w");
fp2=fopen("bookl.txt","r");
for(i=0;fread(&book[i],sizeof(struct book),1,fp2)!=0;i++)
{
fwrite(&book[i],sizeof(struct book),1,fp);
}
fclose(fp);
fclose(fp2);
fopen("bookl.txt","w");
fclose(fp2);
printf("还书完毕,按任意键返回...\n");
getch();
return 1;
}
}
printf("你没有借这样的书,任意键返回...\n");
fclose(fp);
getch();
return 0;
}
}
printf("没这样的卡,和管理员联系,按任意键返回...\n");
fclose(fp);
getch();
return 0;}
lendcount()
{
FILE *fp;
int i,n=0;
fp=fopen("record.txt","r");
if((fp=fopen("record.txt","r"))==0)
{printf("文件中没有数据!\n");
printf("按任意键返回...\n");
getch();
return(0);
}
for(i=0;fread(&student[i],sizeof(struct student),1,fp)!=0;i++)
{
printf("卡号 借出的书籍 \n");
printf("%d %s \n",student[i].carnum,student[i].lendbook);
n=n+1;
}
fclose(fp);
printf("目前共有%d本书借出\n",n);
printf("按任意键...\n");
getch();
return n;
}
addcar()
{
FILE *fp;
int i=0;
fp=fopen("car.txt","a+");
printf("卡号:\n");
scanf("%d",&car[i].carnum);
printf("性别:\n");
scanf("%s",car[i].sex);
printf("姓名:\n");
scanf("%s",car[i].studentname);
printf("班级:\n");
scanf("%d",&car[i].studentclass);
fwrite(&car[i],sizeof(struct car),1,fp);
fclose(fp);
printf("输入完毕,任意键返回...\n");
getch();
}
delcar()
{
FILE *fp,*fp2;
int i;
int carnum;
char choice;
fp=fopen("car.txt","r");
fp2=fopen("bookl.txt","w");
printf("要删除的卡号(不存在则不作改动):\n");
scanf("%d",&carnum);
for(i=0;fread(&car[i],sizeof(struct car),1,fp)!=0;i++)
{
if(car[i].carnum!=carnum)
{
fwrite(&car[i],sizeof(struct car),1,fp2);
}
}
fclose(fp);
fclose(fp2);
printf("确认删除?(Y/N)!\n");
scanf("%s",&choice);
if(choice=='y'||choice=='Y')
{
fp=fopen("car.txt","w");
fp2=fopen("bookl.txt","r");
for(i=0;fread(&car[i],sizeof(struct car),1,fp2)!=0;i++)
{
fwrite(&car[i],sizeof(struct car),1,fp);
}
fclose(fp);
fclose(fp2);
fp2=fopen("bookl.txt","w");
fclose(fp2);
printf("操作超成功!\n按任意键返回...\n");
getch();
return 0;
}
else
{
printf("操作已取消!\n按任意键返回...\n");
getch();
return 0;
}
}
carcount()
{
FILE *fp;
int i,n=0;
fp=fopen("car.txt","r");
for(i=0;fread(&car[i],sizeof(struct car),1,fp)!=0;i++)
{
printf("第%d张卡 \n",i+1);
printf(" 卡号 性别 姓名 班级 \n");
printf("\t\t\n");
printf(" %d %6s %10s %3d \n\n",car[i].carnum,car[i].sex,car[i].studentname,car[i].studentclass);
n=n+1;
}
fclose(fp);
printf("目前共有%d个学生。\n",n);
printf("按任意键返回...\n");
getch();
}
addbook()
{
FILE *fp;
int i=0;
char choice='y';
fp=fopen("book.txt","a+");
while(choice=='y'||choice=='Y')
{
printf("书号:\n");
scanf("%d",&book[i].booknum);
printf("书名:\n");
scanf("%s",book[i].bookname);
printf("作者:\n");
scanf("%s",book[i].bookcreat);
printf("单价:\n");
scanf("%f",&book[i].bookprice);
printf("请你设定书的状态(1在架/0不在):\n");
scanf("%d",&book[i].turefalse);
fwrite(&book[i],sizeof(struct book),1,fp);
printf("是否要输入下本书(y/n)\n");
scanf("%s",&choice);
}
fclose(fp);
}
delbook()
{
FILE *fp,*fp2;
int i;
char bookname[10],choice;
fp=fopen("book.txt","r");
fp2=fopen("bookl.txt","w");
printf("书名:\n");
scanf("%s",bookname);
for(i=0;fread(&book[i],sizeof(struct book),1,fp)!=0;i++)
{
if(strcmp(bookname,book[i].bookname)!=0)
{
fwrite(&book[i],sizeof(struct book),1,fp2);
}
}
fclose(fp);
fclose(fp2);
printf("确认删除(y/n)?\n");
scanf("%s",&choice);
if(choice=='y'||choice=='Y')
{
fp=fopen("book.txt","w");
fp2=fopen("bookl.txt","r");
for(i=0;fread(&book[i],sizeof(struct book),1,fp2)!=0;i++)
{
fwrite(&book[i],sizeof(struct book),1,fp);
}
fclose(fp);
fclose(fp2);
fp2=fopen("bookl.txt","w");
fclose(fp2);
printf("按任意键返回...\n");
getch();
return 0;
}
else
{
printf("按任意键返回...\n");
getch();
return 0;
}
}
changebook()
{
FILE *fp,*fp2;
char bookname[10],choice;
int i;
fp=fopen("book.txt","r");
fp2=fopen("bookl.txt","w");
printf("书名:\n");
scanf("%s",bookname);
for(i=0;fread(&book[i],sizeof(struct book),1,fp)!=0;i++)
{
if(strcmp(book[i].bookname,bookname)==0)
{
printf("你所要修改的书的资料如下,请选择你要修改的内容:\n");
printf("序号 书名 作者 \n");
printf("%d %s %s\n",book[i].booknum,book[i].bookname,book[i].bookcreat);
printf("1: 修改书号:\n");
printf("2: 修改书名:\n");
printf("3: 修改作者:\n");
printf("4: 修改单价:");
printf("请输入1━3:");
choice=getch();
switch(choice)
{
case '1':
{
printf("新的书号:\n");
scanf("%d",&book[i].booknum);
fwrite(&book[i],sizeof(struct book),1,fp2);
}break;
case '2':
{
printf("新的书名:\n");
scanf("%s",book[i].bookname);
fwrite(&book[i],sizeof(struct book),1,fp2);
}break;
case '3':
{
printf("新的作者:\n");
scanf("%s",book[i].bookcreat);
fwrite(&book[i],sizeof(struct book),1,fp2);
}break;
case '4':
{
printf("新的单价:");
scanf("%f",book[i].bookprice);
fwrite(&book[i],sizeof(struct book),1,fp2);
}
default:printf("没有这样的操作!");break;
}
continue;
}
fwrite(&book[i],sizeof(struct book),1,fp2);
}
fclose(fp);
fclose(fp2);
fp=fopen("book.txt","w");
fp2=fopen("bookl.txt","r");
for(i=0;fread(&book[i],sizeof(struct book),1,fp2)!=0;i++)
{
fwrite(&book[i],sizeof(struct book),1,fp);
}
fclose(fp);
fp2=fopen("bookl.txt","w");
fclose(fp2);
printf("按任意键返回...\n");
getchar();
return 0;
}
bookcount()
{
FILE *fp;
int i,n=0;
fp=fopen("book.txt","r");
for(i=0;fread(&book[i],sizeof(struct book),1,fp)!=0;i++)
{
if(book[i].booknum!=0&&strlen(book[i].bookname)!=0&&strlen(book[i].bookcreat)!=0)
{
printf("第%d本书\n序号 书名 作者 单价 状态 \n",i+1);
printf("\t\t\n");
printf(" %d %10s ",book[i].booknum,book[i].bookname);
printf("%15s %4.2f %d(1在/0不在)\n",book[i].bookcreat,book[i].bookprice,book[i].turefalse);
n=n+1;
}
}
fclose(fp);
printf("目前共有%d本书.\n",n);
printf("按任意键返回...\n");
}
end()
{
printf("\t\t\n");
printf("\t\t\n");
printf("\t\t┃┃ 谢谢使用本系统 , 再见! ┃┃\n");
printf("\t\t\n");
printf("\t\t\n");
}
main()
{
char ch1,ch2,ch4;
do
{
printf("\t\n");
printf("\t\t 『制作:郭献安 学号:20101001871 班号: 054101-13』\n\n");
printf("\t\t\n");
printf("\t\t\n");
printf(" 请选择需要的服务:\n");
printf(" 1:查阅图书 \n");
printf(" 2:借阅图书 \n");
printf(" 3:管理系统 \n");
printf(" 4:还书 \n");
printf(" 0:退出 \n");
printf("请输入数字0━4:\n");
ch1=getch();
switch(ch1)
{
case '1':chabook();break;
case '2':lendbook();;break;
case '3':{
do
{
printf(" 1:增加图书 \n");
printf(" 2:删除图书 \n");
printf(" 3:修改图书 \n");
printf(" 4:图书统计 \n");
printf(" 5:学生管理系统 \n");
printf(" 0:返回 \n");
printf("请输入0━5:\n");
ch2=getch();
switch(ch2)
{
case '1':addbook();
break;
case '2':delbook();break;
case '3':changebook();break;
case '4':bookcount();getch();break;
case '5':{
do
{
printf(" 1: 申请新卡 \n");
printf(" 2: 删除卡号 \n");
printf(" 3: 借书统计 \n");
printf(" 4: 卡号统计 \n");
printf(" 0: 返回 \n");
printf("请输入0━4:\n");
ch4=getch();
switch(ch4)
{
case '1':addcar();break;
case '2':delcar();break;
case '3':lendcount();break;
case '4':carcount();break;
case '0':break;
default:printf("无此操作!\n");getch();break;
}
}while(ch4!='0');}break;
case '0':break;
default:printf("无此操作!\n");getch();break;
}
}
while(ch2!='0');}break;
case '4':huanbook();break;
case '0':break;
default:printf("无此操作!\n");getch();break;
}
}while(ch1!='0');
}