#include<stdio.h>
#include<stdlib.h>
#include<string.h>
typedef struct BookStore
{
char name[100];//书籍的名称
char author[100];// 作者
char publish[100];//出版社
float date;//出版日期
double price;//价格
int location;//在目录中的位置
int count;//当前图书的数量
}books;
void menu();
void buy();
void add();
int main()
{
char ch;
system("title 书店存货清单管理");
menu();
ch=getchar();
switch(ch){
case '0':
printf("\t\t\t【退出程序】\n");
return 0;
case '1':
printf("\t\t\t【购买书籍】\n");
buy();
break;
case '2':
printf("\t\t\t【添加书籍】\n");
add();
system("cls");//清屏
buy();
break;
}
return 0;
}
void menu()
{
printf("--------------------【书店存货清单管理】--------------------\n");
printf("\t\t\t0.退出系统\n");
printf("\t\t\t1.购买书籍\n");
printf("\t\t\t2.添加书籍\n");
printf("------------------------------------------------------------\n");
printf("请输入(0~2):");
}
void buy()
{
FILE *fp;
if((fp=fopen("bookstore.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;
double Price=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);
i=0;
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){
Price+=n*a[i].price ;
printf(" 金额:%.2lf\n",n*a[i].price);
a[i].count -=n;
}else{
printf(" 所需数量不在库存范围内,现在书籍存货剩余%d本\n",a[i].count );
}
printf(" 是否还想买其他书籍?(Y/N)\n");
fflush(stdin);
printf(" ");
scanf("%c",&c);
if(c=='y'||c=='Y'){
goto flag1;
}else if(c=='n'||c=='N'){
printf(" 合计金额:%.2lf元\n",Price);
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("bookstore.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(" 请输入该书的书名:");
getchar();
//scanf("%s",a[i].name);
gets(a[i].name);
printf(" 请输入该书的作者:");
//scanf("%s",a[i].author);
gets(a[i].author);
printf(" 请输入该书的出版社:");
//scanf("%s",a[i].publish);
gets(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");
getchar();
scanf("%c",&ch);
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);
}
C语言课程设计---书店存货清单管理
于 2024-01-27 11:54:06 首次发布