<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.name<<endl
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
<<count<<endl
for(unsigned
int
i(0)i<counti++)
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)i<counti++){
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)i<items.size()i++)
{
if(items.at(i).barcode
==
barcode
)
{
items.at(i).Modify()
}
}
}
public:
vector<ItemInfo>
items
}
//
Purchase
item
list
class
class
PurchaseItemList
{
public:
PurchaseItemList(){items.clear()}
void
Add(ItemPurchaseInfo
item)
{items.insert(items.end(),
item)}
public:
vector<ItemPurchaseInfo>
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,
float&price)
{
//
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
}
超市管理系统*/
#include<stdio.h>
#include<string.h>
//欢迎界面//
void welcome()
//功能浏览//
void mainMenu()
//购物结算//
void saleCalc()
//进货管理//
void addGoods()
//修改信息
void updateGoods()
//显示商品//
void showGoods()
//删除商品//
void deleteGoods()
//查询商品//
int searchGoods()
//购买结算
void saleCalc()
//更新库存//
void updateGoodsNum(int number,char name[50])
//结构体
struct goods
{
char name[50]//商品名称
int num//商品数量
float price//商品价格
}
int count/雀族袭/商品种类数量
goods list[1000]//声明goods的变量数组
char name[50]
void main()//主函数
{
int num
welcome()
do{
mainMenu()
printf("请选择功能:")
scanf("%d",&num)
switch(num)
{
case 1:
saleCalc()
break
case 2:
addGoods()
break
case 3:
updateGoods()
break
case 4:
showGoods()
break
case 5:
deleteGoods()
break
case 0:
break
}
}while(num!=0)
}
void welcome()//欢迎界面
{
printf("-------------------------------------------------------------------------\n")
printf("****************欢迎使用超市管理系统*******************\n")
printf("-------------------------------------------------------------------------\n")
}
void mainMenu()//功能浏览界面
{
printf("****1.购物结算****\n")
printf("****2.进货管理****\n")
printf("****3.修改商品****\n")
printf("****4.显示商品****\n")
printf("****5.删穗厅除商品****\n")
printf("****0.退出系统****\n")
}
//查询信息
int searchGoods()//查询商品信息
{
printf("请输入您要购买商品的名字:")
scanf("%s",name)
for(int i=0i<counti++)
{
if(stricmp(name,list[i].name)==0)
{
return 1
}
else
{
printf("对不起了,没有找到%c商品\n",name)
return -1
}
}
}
//购物结算
void saleCalc()//购物结算
{
int number,i,s
float priceGoods=0,sum=0,money
char c=' '
s=searchGoods()
do
{
if(s==-1)
{
s=searchGoods()
}else
{
printf("该商品b存在")
for(i=0i<counti++)
{
if(strcmp(name,list[i].name)==0)
{
priceGoods=list[i].price
break
}
}
printf("请顷兄输入您要购买的数量:")
scanf("%d",&number)
sum=priceGoods*number
updateGoodsNum(-number,name)
printf("是否继续购买(y/n)")
fflush(stdin)
c=getchar()
}
}while(c=='y'||c=='Y')
printf("本次消费总金额为:%.2f",sum)
printf("请输入您的实际付款金额:")
scanf("%f",&money)
printf("找零:%.2f\n",money-sum)
}
//进货管理
void addGoods() //商品的录入
{
int i=0
char c=' '
do
{
printf("请输入商品的名称:")
fflush(stdin)
scanf("%s",list[i].name)
printf("请输入商品的数量:")
scanf("%d",&list[i].num)
printf("请输入商品的单价:")
scanf("%f",&list[i].price)
printf("是否继续(y/n)")
fflush(stdin)
c=getchar()
}while(c=='Y'||c=='y')
}
//更新库存
void updateGoodsNum(int number,char name[50])
{
int i
for(i=0i<counti++)
{
list[i].num+=number//list[i].num=list[i].num+number
}
}
//显示货物信息
void showGoods()
{
printf("商品名称\t数量\t单价\n")
for(int i=0i<counti++)
{
printf("%s\t%d\t%.2f\n",list[i].name,list[i].num,list[i].price)
}
}
//修改商品信息
void updateGoods()
{
int i
printf("请输入您要修改商品的名字:")
scanf("%s",&name)
for(i=0i<counti++)
{
if(strcmp(name,list[i].name)==0)
{
printf("请输入新的商品名字:")
scanf("%s",list[i].name)
printf("请输入您要修改商品的数量:")
scanf("%d",list[i].num)
printf("请输入您要修改商品的单价:")
scanf("%f",list[i].price)
}
else
{
printf("对不起,没有找到该商品信息!")
}
}
}
//删除商品信息
void deleteGoods()
{
int index=-1
printf("请输入您要删除的商品名字:")
scanf("%s",name)
for(int i=0i<counti++)
{
if(strcmp(name,list[i].name)==0)
{
index=i
}
}if(index==-1)
{
printf("对不起!没有找到您要删除的商品信息!")
}
else{
for(int i=indexi<counti++)
{
list[i]=list[i+1]
}
}
}
欢迎分享,转载请注明来源:内存溢出
评论列表(0条)