|
发表于 2021-6-22 08:24:26
From FishC Mobile
|
显示全部楼层
|阅读模式
马上注册,结交更多好友,享用更多功能^_^
您需要 登录 才可以下载或查看,没有账号?立即注册
x
音像图书租借管理系统,运行后发现同一个人同时借几本书,当还了第一本书后,该租借者所有的借书数据都被删除了,要怎么改才能只删除已还书的那部分数据?(以下是部分代码)
#include<stdio.h> #define Max 100 #include<string.h>#include<conio.h>#include<stdlib.h> struct date{ //租借&归还日期 int year; char ch1; int month; char ch2; int day; }; struct library{ //音像图书库 char bianhao[10]; char name[100]; int price; int amount; int shengyu; }; struct borrower{ //租借者信息 char uname[100]; char bianhao[10]; struct date borrow; int money; struct date back; int rent; };char mima1[10]; void print(); void mima(); void build_book(); //创建音像图书库 void borrow_book(); //租借音像图书 void back_book(); //归还音像图书 void buy_book(); //购进音像图书 void scrap_book(); //报废音像图书 void storage_book(); //图书库存储概况 int borrow_day(struct borrower e); //计算租借的天数 void print(){ system("cls");printf("\t\n" "\t\n" "\t\n" "\t |------------------------------------------|\t\n" "\t | 音像租借管理系统 |\t\n" "\t |------------------------------------------|\t\n" "\t | 请输入选项编号(0-6) |\t\n" "\t |------------------------------------------|\t\n" "\t | . 1--创建音像图书库 |\t\n" "\t | . 2--租借音像图书 |\t\n" "\t | . 3--归还音像图书 |\t\n" "\t | . 4--购进音像图书 |\t\n" "\t | . 5--报废音像图书库 |\t\n" "\t | . 6--音像图书库概况 |\t\n" "\t | . 0--退出系统 |\t\n" "\t |------------------------------------------|\t\n");} int main(void){ char choice,yesno; char op; int i=0,pnum=0; char psword[10]; fflush(stdin); //密码功能 printf("\n"); printf("\n"); printf("\n"); printf(" 【欢迎使用音像租借管理系统】 \n"); printf("\n"); printf(" 请输入登录密码:"); do{ i=0; while((psword[i]=getch())!='\r'){ printf("*"); i++; } psword[i]='\0'; printf("\n"); if(strcmp(psword,"123456")==0){ printf(" 密码正确!\n"); do{ print(); printf(" "); fflush(stdin); scanf(" %c",&choice); switch(choice) { case '1':build_book(); break; case '2':borrow_book(); break; case '3':back_book(); break; case '4':buy_book(); break; case '5':scrap_book(); break; case '6':storage_book(); break; case '0':system("cls"); printf("\n"); printf("\n"); printf("\t\t\t--------------------------------------------\t\n"); printf("\t\t\t|感谢使用本软件!已正常退出,按任意键结束。|\t\n"); printf("\t\t\t--------------------------------------------\t\n"); exit(0); default:printf("\n 没有选项为%c的选项!\n",choice); printf("\n 请输入任意键继续!"); }printf("\n 要进入主菜单吗(y/n)?\n"); fflush(stdin); yesno=getchar(); if(yesno!='y'&&yesno!='Y'&&yesno!='N'&&yesno!='n'){ printf("\n 请重新输入(y/n)!\n"); fflush(stdin); yesno=getchar(); } else if(yesno=='N'||yesno=='n'){ system("cls"); printf("\n"); printf("\n"); printf("\t\t\t--------------------------------------------\t\n"); printf("\t\t\t|感谢使用本软件!已正常退出,按任意键结束。|\t\n"); printf("\t\t\t--------------------------------------------\t\n"); exit(0); } }while(yesno=='Y'||yesno=='y');}else{ pnum++; if(pnum==1) printf(" 还有两次机会,请输入密码:"); else if(pnum==2) printf(" 最后一次机会,请输入密码:"); else if(pnum>=3){ printf(" 错误超过3次,结束!\n"); return 0; } } }while(pnum<3); //最多允许输3次}//创建音像图书库 void build_book() { char ch[10],op; struct library a; FILE *fp1=NULL; fflush(stdin); fp1=fopen("d:\\Library.txt","a"); if(fp1==NULL){ printf("\n文件打开错误!\n"); exit(0); } printf("\n 请输入音像图书的编号(B_**、V_**和R_**):"); scanf("%s",a.bianhao); strcpy(ch,a.bianhao); if(ch[0]!='B'&&ch[0]!='V'&&ch[0]!='R'){ printf(" 非法输入!\n"); printf(" 请按任意键重新输入!"); op=getchar(); op=getchar(); build_book(); } printf(" 请输入音像图书的名称:"); scanf("%s",a.name); printf(" 请输入音像图书的原价:"); scanf("%d",&a.price); printf(" 请输入音像图书的原数量:"); scanf("%d",&a.amount); printf(" 请输入音像图书的剩余数量:"); scanf("%d",&a.shengyu); if(a.amount <=a.shengyu ){ printf(" 原数量不得小于剩余数量!请重新输入!\n"); printf(" 请输入音像图书的原数量:"); scanf("%d",&a.amount); printf(" 请输入音像图书的剩余数量:"); scanf("%d",&a.shengyu); } fprintf(fp1,"%s\t%s\t%d\t%d\t%d\n",a.bianhao,a.name,a.price,a.amount,a.shengyu); if(fclose(fp1)){ printf("不能关闭文件!\n"); exit(0); } printf("\n 创建音像图书《%s》成功!\n",a.name); printf("\n 输入任意键继续!\n"); op=getchar(); op=getchar(); system("cls"); } //租借音像图书 void borrow_book() { struct library a={0},b[Max]={0},*p1=NULL,*p2=NULL; //b为结构数组 struct borrower c={0}; char ch[10]={0},op; int n=0,m=0; FILE *fp1; FILE *fp2; fflush(stdin); if((fp1=fopen("d:\\borrow.txt","a"))==NULL){ printf("文件打开错误!\n"); exit(0); } if((fp2=fopen("d:\\Library.txt","r"))==NULL){ printf("文件打开错误!\n"); exit(0); } printf("请输入您的姓名: "); scanf("%s",c.uname); printf("请输入租借的书的编号: "); scanf("%s",c.bianhao); printf("请输入租借的日期(年/月/日): "); scanf("%d%c%d%c%d",&c.borrow.year,&c.borrow.ch1 ,&c.borrow.month,&c.borrow.ch2,&c.borrow.day); strcpy(ch,c.bianhao); fprintf(fp1,"%s\t%s\t%d%c%d%c%d ",c.uname ,c.bianhao,c.borrow.year,c.borrow.ch1,c.borrow.month,c.borrow.ch2,c.borrow.day); p1=b; while(feof(fp2)==0) { fscanf(fp2,"%s%s%d%d%d",p1->bianhao,p1->name,&p1->price,&p1->amount,&p1->shengyu); p1++; n++; } for(p1=b;p1<b+n;p1++) if(strcmp(c.bianhao,p1->bianhao)==0) { p2=p1; m=1; break; } if(m==0) { printf("无此音像图书!\n"); printf("输入任意键继续!\n"); op=getchar(); op=getchar(); } else if((strcmp(ch,p2->bianhao)==0)&&(p2->shengyu==0)) { printf("\n此音像图书已全部借出!\n"); printf("输入任意键继续!\n"); op=getchar(); op=getchar(); } else if((strcmp(ch,p2->bianhao)==0)&&(p2->shengyu!=0)) { printf("应交的押金额为 %d 元!",(p2->price)*3); c.money=(p2->price)*3; p2->shengyu=p2->shengyu-1; fprintf(fp1,"%d\n",c.money); fclose(fp1); fclose(fp2); printf("\n输入任意键继续!\n"); op=getchar(); op=getchar(); } if((fp2=fopen("d:\\Library.txt","w"))==NULL){ printf("文件打开错误!\n"); exit(0); } for(p1=b;p1<b+n;p1++) fprintf(fp2,"%s\t%s\t%d\t%d\t%d\n",p1->bianhao,p1->name,p1->price,p1->amount,p1->shengyu); system("cls"); fclose(fp2); } //归还音像图书 void back_book() { struct library a={0},b[Max]={0},*p1=NULL,*p2=NULL; //b为结构数组 struct borrower c={0},d[Max]={0},*p3=NULL,*p4=NULL; char ch[10]={0},ch1[100],op; int day,n1=0,n2=0,m=0,rent,money_1; int x,i; int month_day[2][13]={{0,31,28,31,30,31,30,31,31,30,31,30,31}, {0,31,29,31,30,31,30,31,31,30,31,30,31} }; FILE *fp1; FILE *fp2; fflush(stdin); if((fp1=fopen("d:\\Library.txt","r"))==NULL){ printf("文件打开错误!\n"); exit(0); } if((fp2=fopen("d:\\borrow.txt","r"))==NULL){ printf("文件打开错误!\n"); exit(0); } printf("请输入您的名字: "); scanf("%s",c.uname); printf("请输入归还的书的编号: "); scanf("%s",c.bianhao); printf("请输入归还的日期(年/月/日): "); scanf("%d%c%d%c%d",&c.back.year,&c.back.ch1,&c.back.month,&c.back.ch2,&c.back.day); strcpy(ch,c.bianhao); strcpy(ch1,c.uname); //在文件中寻找被借的书 p1=b; p3=d; while(feof(fp1)==0) { fscanf(fp1,"%s%s%d%d%d",p1->bianhao,p1->name,&p1->price,&p1->amount,&p1->shengyu); p1++; n1++; } while(feof(fp2)==0) { fscanf(fp2,"%s%s%d%c%d%c%d%d",p3->uname ,p3->bianhao,&p3->borrow.year,&p3->borrow.ch1,&p3->borrow.month,&p3->borrow.ch2,&p3->borrow.day,&p3->money); p3++; n2++; } for(p1=b;p1<b+n1;p1++) if(strcmp(ch,p1->bianhao)==0){ p2=p1; break; } for(p3=d;p3<d+n2;p3++) if(strcmp(ch,p3->bianhao)==0&&strcmp(ch1,p3->uname)==0){ p4=p3; m=1; break; } //当输入的姓名或者借书的编号错误时 if(m==0) { printf("您没有借书记录!\n"); printf("输入y重新填写资料,输入n返回上一菜单!\n"); scanf(" %c",&op); if(op=='y') back_book(); if(op=='n') { system("cls"); main() ; } } printf("\n此音像图书名为《%s》 \n",p2->name); if((fp1=fopen("d:\\Library.txt","w"))==NULL){ printf("文件打开错误!\n"); exit(0); } p2->shengyu =p2->shengyu+1; for(p1=b;p1<b+n1;p1++) fprintf(fp1,"%s\t%s\t%d\t%d\t%d\n",p1->bianhao ,p1->name,p1->price,p1->amount ,p1->shengyu ); //计算租借的总天数 x=(p4->borrow.year%4==0&&p4->borrow.year%100!=0||p4->borrow.year%400==0); if(p4->borrow.year==c.back.year) { if(p4->borrow.month==c.back.month) day=c.back.day-p4->borrow.day; else { day=month_day[x][p4->borrow.month]-p4->borrow.day; for(i=p4->borrow.month+1;i<c.back.month;i++) day=day+month_day[x][i]; day=day+c.back.day; } } else{ day=month_day[x][p4->borrow.month]-p4->borrow.day; for(i=p4->borrow.month+1;i<=12;i++) day=day+month_day[x][i]; if(p4->borrow.year-c.back.year==1){ for(i=0;i<c.back.month;i++) day=day+month_day[x][i]; day=day+c.back.day; } else{ if(x==0) day+=365; else day+=366; for(i=0;i<c.back.month;i++) day=day+month_day[x][i]; day=day+c.back.day; } } printf("一共租借了 %d 天!\n",day); if(ch[0]=='B') rent=day; if(ch[0]=='V') rent=day*2; if(ch[0]=='R') rent=day*3; if(rent>=p3->money) money_1=0; if(rent<p3->money) money_1=p3->money-rent; printf("已交押金 %d 元,应交租金 %d元,应退金额 %d 元\n",p4->money,rent,money_1); fclose(fp1); fclose(fp2); //删除租借者的信息 p3=d; n1=0; if((fp2=fopen("d:\\borrow.txt","r"))==NULL){ printf("文件打开错误!\n"); exit(0); } while(feof(fp2)==0) { fscanf(fp2,"%s%s%d%c%d%c%d%d",p3->uname ,p3->bianhao,&p3->borrow.year,&p3->borrow.ch1,&p3->borrow.month,&p3->borrow.ch2,&p3->borrow.day,&p3->money); p3++; n2++; } fclose(fp2); for(p3=d;p3<d+n1;p3++) if(strcmp(ch1,p3->uname)==0) { p4=p3; break; } if((fp2=fopen("d:\\borrow.txt","w"))==NULL) { printf("文件打开错误!\n"); exit(0); } for(p3=d;p3<d+n1-1;p3++) { if(strcmp(p4->uname,p3->uname)==0) break; else fprintf(fp1,"%s %s %d%c%d%c%d %d",p3->uname ,p3->bianhao,p3->borrow.year,p3->borrow.ch1,p3->borrow.month,p3->borrow.ch2,p3->borrow.day,p3->money); } printf("成功删除 %s 的信息!\n",p4->uname); printf("按任意键结束!"); op=getchar();op=getchar(); system("cls"); fclose(fp2); } |
|