#include #include #include #include #include //书店存货结构体 typedef struct BookStore { char name[100]; //书籍的名称? char author[100]; // 作者? char publish[100]; //出版社 float date; //出版日期 double price; //价格 int location; //在目录中的位置 int count; //当前图书的数量 }books; void display(); void menu(); void buy(); void add(); int main() { char ch; system("title 书店存货清单"); display(); menu(); ch=getchar(); switch (ch) { case '0': printf(" 谢谢使用,再见!\n"); return 0; case '1': buy(); break; case '2': add(); system("cls"); buy(); break; } return 0; } void display() { printf("\n\n\n\n\n\n"); system("color 7A"); printf("欢迎使用书店存货清单\n\n"); Sleep(500); printf(" ——计算机1801班雷湘辉、罗锐\n\n"); Sleep(500); printf(" (请按任意键继续)"); getch(); system("cls"); system("color 70"); } void menu() { printf("请选择功能:\n"); /*printf("(1)购买书籍;(2)添加书籍;(0)退出程序\n");*/ printf("---------购买书籍请输入[1]:\n"); printf("---------添加书籍请输入[2]:\n"); printf("---------退出程序请输入[0]:\n"); } void buy() { FILE *fp; if((fp=fopen("books.txt","r+"))==NULL) { printf("文件打开失败!\n"); system("pause"); return; } books a[100]; system("cls"); printf(" ————购买书籍————\n"); int i=0,n=0,flag=0,j=0,sum=0; char c,name[100]; while(!feof(fp)) { fscanf(fp,"%s%s%s%f%lf%d%d",a[i].name,a[i].author,a[i].publish,&a[i].date,&a[i].price,&a[i].location,&a[i].count); i++; } printf("\n"); printf(" 欢迎光临本书店,"); flag1: printf("请输入你所需要的书籍名字:"); fflush(stdin); //清空输入缓冲区,为了确保不影响后面的数据读取?? ? scanf("%s",name); while(!feof(fp)) { j++; //j加到数组尾部说明未找到该书籍? if(strcmp(a[i].name,name)==0) { printf(" 书名:"); puts(a[i].name); printf(" 作者:"); puts(a[i].author); printf(" 出版社:"); puts(a[i].publish); printf(" 出版日期:%.2f\n",a[i].date); printf(" 价格:%.2lf\n",a[i].price); printf(" 位置:%d\n",a[i].location); printf(" 库存:%d\n",a[i].count); printf(" 请输入所需数量:"); fflush(stdin); scanf("%d",&n); if(n<=a[i].count){ printf(" 总价为:%.2lf\n",n*a[i].price); } else printf(" 所需数量不在库存范围内\n"); printf(" 是否还想买其他书籍?(Y/N)\n"); fflush(stdin); printf(" "); scanf("%c",&c); if(c=='y'||c=='Y') goto flag1; else if(c=='n'||c=='N') { j=0; //当要退出是使j清0,防止当所找书籍位置为5是跳出for循环而误入下一个if语句? printf(" 谢谢使用,再见!\n"); system("pause"); exit(0); } } i++; } if(j==100) { j=0; printf(" 未查找到该书籍,请重新输入\n"); goto flag1; } else { j=0; printf(" 未查找到该书籍,请重新输入\n"); goto flag1; } fclose(fp); } void add() { FILE *fp; if((fp=fopen("books.txt","a+"))==NULL) { printf("文件打开失败!"); system("pause"); return; } books a[100]; int i=0,n=0; char ch; system("cls"); printf(" ————书籍记录添加————\n"); while(1){ flag2: printf(" 请输入该书的书名:"); scanf("%s",a[i].name); printf(" 请输入该书的作者:"); scanf("%s",a[i].author); printf(" 请输入该书的出版社:"); scanf("%s",a[i].publish); printf(" 请输入该书的出版日期:"); scanf("%f",&a[i].date); printf(" 请输入该书的价格:"); scanf("%lf",&a[i].price); printf(" 请输入该书放置的位置:"); scanf("%d",&a[i].location); printf(" 请输入该书的进货量:"); scanf("%d",&a[i].count); fprintf(fp,"\n%s %s %s %.2f %.2f %d %d",a[i].name,a[i].author,a[i].publish,a[i].date,a[i].price,a[i].location,a[i].count); i++; n=n+1; system("cls"); printf(" 是否继续添加(Y/N)\n"); ch=getch(); if(ch=='N'||ch=='n') break; else if(ch=='Y'||ch=='y') { system("cls"); goto flag2; } system("pause"); fclose(fp); } printf(" 谢谢使用,再见!\n"); system("pause"); exit(0); }