書店通常使用個人電腦設置一個清單來管理書籍的存貨情況,該清單記錄每本書籍的書名,作者,出版社,在清單中的位置等信息。當顧客想要買某本書時,只要輸入書籍的名稱和作者,系統就會顯示該書籍是否在清單中,如果書籍在清單中,系統會顯示書籍的詳細信息以及庫存數量,如果不在清單中,也會顯示相應的提示信息。如果顧客想買的書籍數量在庫存的範圍內,系統會計算總價格並顯示出來,否則,會提示“所需數量不在庫存範圍內”。
根據以上的分析,編程要求:
- 用結構體自行定義幾本書籍,要求包括書籍名稱,作者,出版社,出版日期,價格,在目錄中的位置;
- 當從鍵盤輸入某本書的名稱和作者姓名,如果能查詢到則顯示該書籍的所有信息,並提示“請輸入所需數量”,如果所需數量在庫存範圍內,則顯示總價,否則,顯示“所需數量不在庫存範圍內”,接着繼續提示顧客是否還想買其他書籍,如果輸入“y”或“Y”表示繼續輸入下一本書的名稱和作者姓名,進行下一輪查詢;如果不能查詢到該書籍,則顯示“謝謝,再見!”,依次類推,不斷循環。*/
#include<stdio.h>
#include<stdlib.h>
#include<string.h>
#include<conio.h>
#include<windows.h>
//書店存貨結構體
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 書店存貨清單4.0版本");
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(" 歡");
Sleep(250);
system("color 7B");
printf("迎");
Sleep(250);
system("color 7C");
printf("使");
Sleep(250);
system("color 7D");
printf("用");
Sleep(250);
system("color 7E");
printf("書");
Sleep(250);
system("color 7F");
printf("店");
Sleep(250);
system("color 7A");
printf("存");
Sleep(250);
system("color 7B");
printf("貨");
Sleep(250);
system("color 7C");
printf("清");
Sleep(250);
system("color 7D");
printf("單\n\n");
Sleep(500);
printf(" ——計算機1812班***\n\n");
Sleep(500);
printf(" (請按任意鍵繼續)");
getch();
system("cls");
system("color 70");
}
void menu()
{
printf("請選擇功能:");
printf("(1)購買書籍;(2)添加書籍;(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);
}
/*stdin就是標準輸入 std即standard(標準),in即input(輸入),合起來就是標準輸入。 一般就是指鍵盤輸入到緩衝區裏的東西。
函數名: fflush
功能: 清除文件緩衝區,文件以寫方式打開時將緩衝區內容寫入文件
原型:int fflush(FILE *stream)*/