本文目錄一覽:
C語言編寫的超市管理系統
有一個小型超市,出售N(N=10)種商品,設計並實現一個系統,完成下列功能:
1. 保存及輸出。超市中的各種商品信息保存在指定文件中,可以把它們輸出顯示。
2. 計算並排序。計算每類商品的總價值(sum,單精度)及平均價(aver,單精度,輸出一位小數),將每類商品按平均價從大到小的順序排序列印出來。
3. 統計。統計輸出庫存量低於100的貨號及類別。統計輸出有兩種以上(含兩種)商品庫存量低於100的商品類別。
1.2總體結構
本程序主要分為八個模塊:主模塊、信息輸出修改模塊、新建信息模塊、排序模塊、計算模塊、統計模塊1、統計模塊2、列印模塊。
1) 主模塊:通過調用各分模塊實現功能;
2) 信息輸出修改模塊:輸出顯示文件中商品信息內容,添加商品信息,刪除商品信息,修改商品信息;
3) 新建商品信息模塊:建立一個新結構體,為鏈表存信息用,並且將信息保存在指定的文件中;
4) 排序模塊:把文件中順序零亂的商品信息按單價的大小從高到低進行排序,放到鏈表裡存儲;
5) 計算模塊:將所有商品的價格與庫存量進行累加求和;
6) 列印模塊:將商品信息按每類平均價格排序(從高到低)按順序列印出來;
7) 統計模塊1:統計庫存量低於100的貨名及類別;
8) 統計模塊2:統計商品庫存量有2種以上(含2種)低於100的商品類別。
附 錄(程序清單)
#include “stdio.h” /*輸入,輸出頭文件*/
#include “stdlib.h” /*申請空間頭文件*/
#include “string.h” /*對字元串加工頭文件*/
#include “conio.h” /*清屏頭文件*/
FILE *fp;
int n=0; /*定義文件指針類型*/
int i,j,a[4],m; /*定義整數類型*/
float aver[4],sum[4],g[4],h; /*定義浮點類型*/
char c[5]=”elec”; /*定義字元數組類型*/
char d[5]=”comm”; /*定義字元數組類型*/
char e[5]=”food”; /*定義字元數組類型*/
char f[5]=”offi”; /*定義字元數組類型*/
struct good /*定義結構體*/
{
int num; /*商品編號*/
char name[20]; /*商品名稱*/
char kind[40]; /*商品類型*/
float price; /*商品價格*/
char unit[10]; /*商品單位*/
int quantity; /*商品數量*/
struct good *next; /*定義結構體指針類型*/
}*head,*p1,*p2;
struct good *createlist() /*創建鏈表函數*/
{
struct good *head1,*p1,*p2; /*定義結構體指針類型*/
if((fp=fopen(“goods message.txt”,”w”))==NULL) /*判斷能否打開文件*/
{
printf(“can not open the file”);
exit(0); /*結束程序*/
}
head1=(struct good *)malloc(sizeof(struct good)); /*申請頭結點空間*/
p1=head1;
p2=head1;
printf(“*********************************************\n”);
printf(“請輸入信息:編號,名稱,類型,價格,單位,數目\n”);
printf(” (以輸入「-1」表示結束輸入)\n”);
printf(“*********************************************\n”);
printf(“____________________\n”);
scanf(“%d %s %s %f %s %d”,p1-num,p1-name,p1-kind,p1-price,p1-unit,p1-quantity); /*輸入商品信息*/
printf(“____________________\n”);
p1-next=NULL;
fprintf(fp,”%d %s %s %f %s %d “,p1-num,p1-name,p1-kind,p1-price,p1-unit,p1-quantity); /*將商品信息寫入文件*/
while(1)
{
p1=(struct good *)malloc(sizeof(struct good)); /*申請新空間*/
printf(“*********************************************\n”);
printf(“請輸入信息:編號,名稱,類型,價格,單位,數目\n”);
printf(” (以輸入「-1」表示結束輸入)\n”);
printf(“*********************************************\n”);
printf(“____________________\n”);
scanf(“%d”,p1-num);
if(p1-num==-1) /*申請空間結束條件*/
{
printf(“____________________\n\n”);
fprintf(fp,”%d”,-1);
fclose(fp);
return head1; /*返回頭指針*/
}
scanf(“%s %s %f %s %d”,p1-name,p1-kind,p1-price,p1-unit,p1-quantity); /*輸入商品信息*/
printf(“________________\n”);
fprintf(fp,”%d %s %s %f %s %d “,p1-num,p1-name,p1-kind,p1-price,p1-unit,p1-quantity); /*將商品信息寫入文件*/
p1-next=NULL;
p2-next=p1;
p2=p1;
}
}
struct good *paixu(struct good*head2) /*鏈表排序函數*/
{
struct good *p6,*p7,*r,*s; /*定義結構體指針類型*/
for(i=0;i=3;i++) /*賦初值值*/
{
a[i]=0;
sum[i]=0;
aver[i]=0;
}
p6=(struct good *)malloc(sizeof(struct good)); /*申請新空間*/
p6-next=head2;
head2=p6;
while(p6-next!=NULL) /*判斷循環結束條件*/
{
p7=p6-next;
r=p6;
while(p7-next!=NULL) /*判斷循環結束條件*/
{
if((p7-next-price)(r-next-price)) /*判斷是否調換*/
r=p7;
p7=p7-next;
}
if(p6!=r) /*判斷循環結束條件*/
{
s=r-next; /*指針調換*/
r-next=s-next;
s-next=p6-next;
p6-next=s;
}
p6=p6-next;
}
p6=head2;
head2=head2-next;
free(p6); /*釋放第一個無效空間*/
return head2;
}
void jisuan()
{
p1=head;
do
{
if(strcmp(p1-kind,c)==0) /*判斷是否為電器類型*/
{
sum[0]=sum[0]+(p1-price)*(p1-quantity); /*求電器總價*/
a[0]=a[0]+p1-quantity; /*求電器總件數*/
}
if(strcmp(p1-kind,d)==0) /*判斷是否為日用品類型*/
{
sum[1]=sum[1]+(p1-price)*(p1-quantity); /*求日用品總價*/
a[1]=a[1]+p1-quantity; /*求日用品總件數*/
}
if(strcmp(p1-kind,e)==0) /*判斷是否為辦公用品類型*/
{
sum[2]=sum[2]+(p1-price)*(p1-quantity); /*求辦公用品總價*/
a[2]=a[2]+p1-quantity; /*求辦公用品總件數*/
}
if(strcmp(p1-kind,f)==0) /*判斷是否為食品類型*/
{
sum[3]=sum[3]+(p1-price)*(p1-quantity); /*求食品總價*/
a[3]=a[3]+p1-quantity; /*求食品總件數*/
}
p1=p1-next;
}while (p1!=NULL); /*遍歷鏈表結束條件*/
for(i=0;i4;i++)
aver[i]=sum[i]/a[i]; /*求每類商品平均價*/
printf(“****************************************************\n”);
printf(“商品類型 \t 平均價\t 總庫存量\n”);
printf(“****************************************************\n”);
printf(“____________________________________________________\n”);
printf(“電器總價值:%0.1f\t平均價:%0.1f\t總庫存量:%d\n”,sum[0],aver[0],a[0]);
printf(“____________________________________________________\n”);
printf(“日用品總價值:%0.1f\t平均價:%0.1f\t總庫存量:%d\n”,sum[1],aver[1],a[1]);
printf(“____________________________________________________\n”);
printf(“食品總價值:%0.1f\t平均價:%0.1f\t總庫存量:%d\n”,sum[2],aver[2],a[2]);
printf(“____________________________________________________\n”);
printf(“辦公用品總價值:%0.1f\t平均價:%0.1f\t總庫存量:%d\n”,sum[3],aver[3],a[3]);
printf(“____________________________________________________\n”);
}
void shuchu() /*輸出商品信息函數*/
{
do
{
struct good *p3,*p4,*p5; /*定義結構體指針類型*/
int n=0,p=0,q=0,r=0;
printf(“所有商品信息:\n”);
printf(“編號,名稱,類型,價格,單位,數目\n”);
printf(“**********************************\n”);
if((fp=fopen(“goods message.txt”,”rb+”))==NULL) /*判斷能否打開文件*/
{
printf(“can not open the file”);
exit(0); /*結束程序*/
}
head=(struct good *)malloc(sizeof(struct good)); /*申請頭結點空間*/
p3=head;
fscanf(fp,”%d %s %s %f %s %d “,p3-num,p3-name,p3-kind,p3-price,p3-unit,p3-quantity); /*從文件中寫到鏈表*/
while(1)
{
p4=(struct good *)malloc(sizeof(struct good)); /*申請頭結點空間*/
fscanf(fp,”%d “,p4-num);
if(p4-num!=-1) /*判斷循環結束條件*/
{
fscanf(fp,”%s %s %f %s %d “,p4-name,p4-kind,p4-price,p4-unit,p4-quantity); /*從文件中寫到鏈表*/
p4-next=NULL;
p3-next=p4;
p3=p4;
}
else
{
p3-next=NULL;
break;
}
}
fclose(fp); /*關閉文件*/
p3=head;
while(p3!=NULL)
{
printf(” %d %s %s %0.1f %s %d\n\n”,p3-num,p3-name,p3-kind,p3-price,p3-unit,p3-quantity);
printf(“__________________________________\n”);
p3=p3-next;
}
printf(“**********************************\n”);
printf(“//////////////////////////////////\n”);
while(n!=4)
{
p3=head;
printf(“**********************************\n”);
printf(“1 添加商品信息\n”);
printf(“2 刪除某商品信息\n”);
printf(“3 修改某商品信息\n”);
printf(“4 返回(當你完成了對某一商品的添加、刪除或者修改後請按4返回)\n”);
printf(“**********************************\n”);
scanf(“%d”,n);
if(n==1) /*添加商品信息*/
{
printf(“請輸入商品 編號 名稱 類型 價格 單位 數目\n”);
printf(“**********************************\n”);
p4=(struct good *)malloc(sizeof(struct good)); /*申請空間*/
scanf(“%d %s %s %f %s %d”,p4-num,p4-name,p4-kind,p4-price,p4-unit,p4-quantity); /*輸入商品信息*/
p4-next=NULL;
while(p3-next!=NULL) /*判斷循環結束條件*/
{
p3=p3-next;
}
p3-next=p4;
p3=head;
if((fp=fopen(“goods message.txt”,”w”))==NULL) /*判斷能否打開文件*/
{
printf(“can not open the file”);
exit(0); /*結束程序*/
}
while(p3!=NULL)
{
fprintf(fp,”%d %s %s %f %s %d “,p3-num,p3-name,p3-kind,p3-price,p3-unit,p3-quantity) /*將商品信息寫入文件*/
p3=p3-next;
}
fprintf(fp,”%d”,-1);
fclose(fp); /*關閉文件*/
printf(“**********************************\n”);
printf(“__________________________________\n”);
printf(“————請按4返回————-\n”);
printf(“__________________________________\n”);
printf(“**********************************\n”);
}
if(n==2) /*刪除商品*/
{
printf(“**********************************\n”);
printf(“請輸入需要刪除的商品編號\n”);
printf(“**********************************\n”);
scanf(“%d”,p);
printf(“**********\n”);
printf(“1 確認刪除\n2 取消刪除\n”);
printf(“**********\n”);
scanf(“%d”,r);
if(r==1)
{
if((head-num)==p)
{
head=head-next;
free(p3); /*釋放空間*/
}
else
{
p4=head;
p3=p4-next;
while(p3!=NULL) /*判斷循環結束條件*/
{
if((p3-num)==p)
{
p5=p3-next;
free(p3); /*釋放空間*/
p4-next=p5;
break;
}
p3=p3-next;
p4=p4-next;
}
}
if((fp=fopen(“goods message.txt”,”w”))==NULL) /*判斷能否打開文件*/
{
printf(“can not open the file”);
exit(0); /*結束程序*/
}
p3=head;
while(p3!=NULL) /*判斷循環結束條件*/
{
fprintf(fp,”%d %s %s %f %s %d “,p3-num,p3-name,p3-kind,p3-price,p3-unit,p3-quantity); /*將商品信息寫入文件*/
p3=p3-next;
}
fprintf(fp,”%d”,-1);
fclose(fp); /*關閉文件*/
}
if(r==2)
continue; /*繼續循環*/
printf(“**********************************\n”);
printf(“__________________________________\n”);
printf(“————請按4返回————-\n”);
printf(“__________________________________\n”);
printf(“**********************************\n”);
}
if(n==3) /*修改某商品信息*/
{
printf(“請輸入需要修改的商品編號\n”);
scanf(“%d”,q);
while(p3!=NULL) /*判斷循環結束條件*/
{
if((p3-num)==q) /*判斷是否為所需要修改的商品*/
{
printf(“請輸入商品單價與庫存量(如果單價不變請輸入原來的單價)\n”);
scanf(“%f %d”,p3-price,p3-quantity); /*輸入商品價格與庫存量*/
}
p3=p3-next;
}
if((fp=fopen(“goods message.txt”,”w”))==NULL) /*判斷能否打開文件*/
{
printf(“can not open the file”);
exit(0); /*結束程序*/
}
p3=head;
while(p3!=NULL) /*判斷循環結束條件*/
{
fprintf(fp,”%d %s %s %f %s %d “,p3-num,p3-name,p3-kind,p3-price,p3-unit,p3-quantity); /*將商品信息寫入文件*/
p3=p3-next;
}
fprintf(fp,”%d”,-1);
fclose(fp); /*關閉文件*/
printf(“**********************************\n”);
printf(“__________________________________\n”);
printf(“————請按4返回————-\n”);
printf(“__________________________________\n”);
printf(“**********************************\n”);
}
if(n==4) /*退出*/
break;
}
printf(“**********\n”);
printf(“1 繼續修改\n———\n2 返回\n”);
printf(“**********\n”);
scanf(“%d”,p);
if(p==1)
continue; /*繼續循環*/
if(p==2)
break; /*跳出循環*/
}while(n!=2);
fclose(fp); /*關閉文件*/
}
void printf0(struct good *p) /*遍歷鏈表並列印電器類商品函數*/
{
struct good *p3; /*定義結構體指針類型*/
p3=p;
while (p3!=NULL) /*判斷遍歷鏈表循環結束條件*/
{
if(strcmp(p3-kind,c)==0) /*判斷商品類型是否為電器類型*/
{
printf(“%d\t%s\t%s\t%0.1f\t%s\t%d\n”,p3-num,p3-name,p3-kind,p3-price,p3-unit,p3-quantity); /*輸出電器類商品信息*/
printf(“________________________________________________\n”);
}
p3=p3-next;
}
return;
}
void printf1(struct good *p) /*遍歷鏈表並列印日用品類商品函數*/
{
struct good *p3; /*定義結構體指針類型*/
p3=p;
while (p3!=NULL) /*判斷遍歷鏈表循環結束條件*/
{
if(strcmp(p3-kind,d)==0) /*判斷商品類型是否為日用品類型*/
{
printf(“%d\t%s\t%s\t%0.1f\t%s\t%d\n”,p3-num,p3-name,p3-kind,p3-price,p3-unit,p3-quantity); /*輸出日用品類商品信息*/
printf(“________________________________________________\n”);
}
p3=p3-next;
}
return;
}
void printf2(struct good *p) /*遍歷鏈表並列印辦公用品類商品函數*/
{
struct good *p3; /*定義結構體指針類型*/
p3=p;
while (p3!=NULL) /*判斷遍歷鏈表循環結束條件*/
{
if(strcmp(p3-kind,e)==0) /*判斷商品類型是否為辦公用品類型*/
{
printf(“%d\t%s\t%s\t%0.1f\t%s\t%d\n”,p3-num,p3-name,p3-kind,p3-price,p3-unit,p3-quantity); /*輸出辦公用品類商品信息*/
printf(“________________________________________________\n”);
}
p3=p3-next;
}
return;
}
void printf3(struct good *p) /*遍歷鏈表並列印食品類商品函數*/
{
struct good *p3; /*定義結構體指針類型*/
p3=p;
while (p3!=NULL) /*判斷遍歷鏈表循環結束條件*/
{
if(strcmp(p3-kind,f)==0) /*判斷商品類型是否為食品類型*/
{
printf(“%d\t%s\t%s\t%0.1f\t%s\t%d\n”,p3-num,p3-name,p3-kind,p3-price,p3-unit,p3-quantity); /*輸出食品類商品信息*/
printf(“________________________________________________\n”);
}
p3=p3-next;
}
return;
}
void shunxudayin()
{
for(i=0;i4;i++)
g[i]=aver[i]; /*將平均價賦給新數組*/
for(j=0;j3;j++) /*將新數組用冒泡排序法排序*/
for(i=j+1;i4;i++)
{
if(g[j]g[i])
{
h=g[j];
g[j]=g[i];
g[i]=h;
}
}
printf(“\n****************************\n”);
printf(“商品平均價格排序表(從高到低)\n”);
printf(“****************************\n”);
printf(“________________________________________________\n”);
printf(“編號\t名稱\t類別\t單價\t單位\t數量\n”);
printf(“________________________________________________\n”);
for(j=0;j4;j++)
for(i=0;i4;i++)
{
if (aver[i]==g[j]) /*判斷每類商品平均價格的先後順序*/
switch(i)
{
case 0:
printf0(head); /*調用遍歷鏈表並列印電器類商品函數*/
break;
case 1:
printf1(head); /*調用遍歷鏈表並列印日用品類商品函數*/
break;
case 2:
printf2(head);/*調用遍歷鏈表並列印辦公用品類商品函數*/
break;
case 3:
printf3(head); /*調用遍歷鏈表並列印食品類商品函數*/
break;
}
}
}
void tongji1()
{
p1=head;
printf(“\n************************\n”);
printf(“庫存量低於100的貨名及類別\n”);
printf(“************************\n”);
printf(“________________________\n”);
printf(“商品名稱\t商品類型\n”);
printf(“________________________\n”);
while(p1!=NULL) /*判斷遍歷鏈表循環結束條件*/
{
if(p1-quantity100) /*判斷庫存量是否小於100*/
{
printf(“%s\t%s\n”,p1-name,p1-kind); /*輸出商品名稱及類別*/
printf(“________________________\n”);
}
p1=p1-next;
}
}
void tongji2()
{
printf(“\n**********************************************\n”);
printf(“商品庫存量有2種以上(含2種)低於100的商品類別:\n”);
printf(“**********************************************\n”);
printf(“________\n”);
if((a[0]100)(a[0]=2)) /*判斷電器類庫存量是否為2種以上(含2種)低於100*/
{
printf(“電器\n”);
printf(“________\n”);
}
if((a[1]100)(a[1]=2)) /*判斷日用品類庫存量是否為2種以上(含2種)低於100*/
{
printf(“日用品\n”);
printf(“________\n”);
}
if((a[2]100)(a[2]=2)) /*判斷食品類庫存量是否為2種以上(含2種)低於100*/
{
printf(“食品\n”);
printf(“________\n”);
}
if((a[3]100)(a[3]=2)) /*判斷辦公用品類庫存量是否為2種以上(含2種)低於100*/
{
printf(“辦公用品\n”);
printf(“________\n”);
}
}
int main(int argc, char* argv[])
{
struct good *p1,*p2; /*定義結構體指針類型*/
while(1)
{
printf(“***********************************************\n”);
printf(“1 ———-輸出查看或者修改已存信息———–\n”);
printf(“———————————————–\n”);
printf(“2 —–重新輸入新信息(並且刪除原有信息)——\n”);
printf(“———————————————–\n”);
printf(“3 統計商品信息(如果您還沒有查看過信息請先按1)\n”);
printf(“———————————————–\n”);
printf(“4 ——————-退出———————\n”);
printf(“***********************************************\n”);
scanf(“%d”,m);
if(m==1)
shuchu(); /*調用輸出信息函數*/
if(m==2)
{
system(“cls”);
head=createlist(); /*調用建立鏈表函數*/
}
if(m==3)
{
printf(“統計結果如下\n”);
head=paixu(head); /*調用鏈表排序函數*/
jisuan(); /*調用計算函數*/
shunxudayin(); /*調用順序列印函數*/
tongji1(); /*調用統計1函數*/
tongji2(); /*調用統計2函數*/
}
if(m==4)
{
p1=head;
while(p1!=NULL) /*判斷遍歷鏈表結束條件*/
{
p2=p1-next;
free(p1); /*釋放空間*/
p1=p2;
}
break;
}
}
return 0; /*結束程序*/
}
C語言的關於超市的代碼
case ‘1’:if (number1 – n 10);這句中case後面的1不用打引號,直接跟數字就可以了,單引號一般用於其字元的ascll碼,後面的幾句case也是同一個問題
C語言編寫「超市結賬系統」 急急急!!!
#include
stdio.h
#include
fstream
#include
iostream
#include
string
#include
vector
#include
assert.h
using
namespace
std;
//
Item
info
base
class
class
ItemInfo{
public:
ItemInfo(){}
ItemInfo(string
barcode,
string
name,
float
price)
{
this-barcode
=
barcode;
this-name
=
name;
this-price
=
price;
}
ItemInfo(string
barcode)
{
this-barcode
=
barcode;
}
void
Display()
{
cout
barcode
“\t”name”\t”price
endl;
}
void
Input()
{
cout
“輸入條形碼:”
endl;
cin
barcode;
cout
“輸入名稱:”
endl;
cin
name;
cout
“輸入價格:”
endl;
cin
price;
}
void
Modify()
{
cout
“輸入名稱:”
endl;
cin
name;
cout
“輸入價格:”
endl;
cin
price;
}
friend
ostream
operator(ostream
stream,
ItemInfo
item){
stream
item.barcode
‘\t’
item.price
‘\t’
item.nameendl;
return
stream;
}
friend
istream
operator(istream
stream,
ItemInfo
item){
stream
item.barcode
item.price
item.name;
return
stream;
}
public:
string
barcode;
string
name;
float
price;
};
//
Interface
class
class
DataProvider{
public:
virtual
void
GetFullData(string
barcode,
string
name,
float
price)
=
0;
};
//
Purchase
item
class
class
ItemPurchaseInfo
:
public
ItemInfo{
public:
ItemPurchaseInfo():ItemInfo(){}
ItemPurchaseInfo(string
barcode,
int
count=1)
:
ItemInfo(barcode)
{
this-count
=
count;
}
//
Rember
to
call
this
when
barcode
set
void
GetFullData(DataProvider
aPro)
{
aPro.GetFullData(barcode,
name,
price);
}
void
Input()
{
cout
“輸入條形碼:”
endl;
cin
barcode;
cout
“輸入數量:”
endl;
cin
count;
}
void
Display()
{
cout
barcode
“\t”name”\t”price”\t”count
endl;
}
public:
string
barcode;
int
count;
};
//
Item
list
class
class
ItemList
{
public:
ItemList(){items.clear();}
friend
ostream
operator(ostream
stream,
ItemList
list){
unsigned
int
count
=
list.items.size();
stream
countendl;
for(unsigned
int
i(0);icount;i++)
stream
list.items.at(i);
return
stream;
}
friend
istream
operator(istream
stream,
ItemList
list){
unsigned
int
count(0);
stream
count;
list.items.clear();
for(unsigned
int
i(0);icount;i++){
ItemInfo
item;
stream
item;
list.items.insert(list.items.end(),
item);
}
return
stream;
}
void
Add(ItemInfo
item)
{
items.insert(items.end(),
item);
}
void
Modify()
{
string
barcode;
cout
“輸入條形碼:”
endl;
cin
barcode;
for(unsigned
int
i(0);iitems.size();i++)
{
if(items.at(i).barcode
==
barcode
)
{
items.at(i).Modify();
}
}
}
public:
vectorItemInfo
items;
};
//
Purchase
item
list
class
class
PurchaseItemList
{
public:
PurchaseItemList(){items.clear();}
void
Add(ItemPurchaseInfo
item)
{items.insert(items.end(),
item);}
public:
vectorItemPurchaseInfo
items;
};
//
Implements
the
interface
class
class
Cashier
:
public
DataProvider
{
public:
Cashier()
:
purchase(),
stock(){}
~Cashier(){}
public:
//
User
funcs
void
CheckIn(){
purchase.items.clear();
int
opt(0);
do
{
unsigned
int
i(0);
ItemPurchaseInfo
ipi;
ipi.Input();
purchase.Add(ipi);
cout
“按0退出,任意鍵繼續”
endl;
cin
opt;
}
while(opt);
}
void
CheckOut(){
for(unsigned
int
i(0);
i
purchase.items.size();
i++)
{
purchase.items.at(i).GetFullData(
*this
);
}
float
checkin(0);
cout
“輸入收款數:”
endl;
cin
checkin;
DisplayList(checkin);
}
void
Display()
{
cout
endl”商品清單
“
stock.items.size()
endl;
cout
“————————————–“
endl;
for(unsigned
int
i(0);i
stock.items.size();
i++){
stock.items.at(i).Display();
}
cout
“————————————–“
endl;
}
void
DisplayList(float
checkin)
{
cout
endl”購物小票清單”
endl;
cout
“————————————–“
endl;
float
total(0.0);
for(unsigned
int
i(0);
i
purchase.items.size();
i++)
{
purchase.items.at(i).Display();
total
+=
purchase.items.at(i).price
*
purchase.items.at(i).count;
}
cout
“————————————–“
endl;
cout
“貨款合計:”
total
“元”
endl;
cout
“收款數:”
checkin
“元”
endl;
float
change(checkin-total);
assert(
change
=
0.0);
cout
“找零:”
change
“元”
endl
endl;
}
friend
ostream
operator(ostream
stream,
Cashier
c){
stream
c.stock;
return
stream;
}
friend
istream
operator(istream
stream,
Cashier
c){
c.stock.items.clear();
stream
c.stock;
return
stream;
}
public:
//
interface
func
void
GetFullData(string
barcode,
string
name,
floatprice)
{
//
go
through
stock
and
find
the
item
by
barcode
matching
for(unsigned
int
i(0);
i
stock.items.size();
i++)
{
if(stock.items.at(i).barcode
==
barcode)
{
name
=
stock.items.at(i).name;
price
=
stock.items.at(i).price;
}
}
}
public:
PurchaseItemList
purchase;
ItemList
stock;
};
int
main()
{
int
opt(0);
Cashier
cashier;
ifstream
fin(“data.bin”,
ios::in
|
ios::binary);
fin.seekg(0,
ios::beg);
//cashier.stock.Load(fin);
fin
cashier;
fin.close();
ofstream
fout;
ItemInfo
item;
do{
cout
“1.
新購買”
endl;
cout
“2.
輸入新商品信息”
endl;
cout
“3.
修改商品信息”
endl;
cout
“4.
顯示商品信息”
endl;
cout
“0.
退出”
endl;
cin
opt;
switch(opt)
{
case
1:
cashier.CheckIn();
cashier.CheckOut();
break;
case
2:
item.Input();
cashier.stock.Add(item);
fout.open(“data.bin”,
ios::out|
ios::binary);
fout.seekp(0,ios::beg);
fout
cashier;
fout.close();
break;
case
3:
cashier.stock.Modify();
fout.open(“data.bin”,
ios::out|
ios::app
|
ios::binary);
fout
cashier;
fout.close();
break;
case
4:
cashier.Display();
break;
default:
break;
}
}
while(opt);
return
0;
}
原創文章,作者:OONI,如若轉載,請註明出處:https://www.506064.com/zh-tw/n/141136.html