自动售货机系统源代码.docx
《自动售货机系统源代码.docx》由会员分享,可在线阅读,更多相关《自动售货机系统源代码.docx(17页珍藏版)》请在冰豆网上搜索。
自动售货机系统源代码
#include
#include
#include
#include"goods.h"
boolGetCharge(intSum,intMoney);
voidGetsmalcharge(intMoney,int*ChargeNum);
intKouLing();
charChooseJieMian();
charSaleCenter();
voidloadGoodsFrom*gs);
charManageCenter();
voidKunCun();
voidSale();
voidShangJia();
voidTongJiFun();
Goodsgoods[3];
TongJitj[3];
voidinit(){
for(inti=0;i<3;++i){
tj[i].jrxl=0;
tj[i].jrzje=0;
}
}
voidmain()
{
//初始化
init();
intop=0;
//读取商品信息
loadGoodsFrom);
charc=ChooseJieMian();
while(true)
{
system("cls");
if(c=='1'){
c=SaleCenter();
}elseif(c=='2'){
op=KouLing();
if(op==1){
//进入管理界面
c=ManageCenter();
//system("pause");
}
elseif(op==0){
//进去销售界面
c=SaleCenter();
}
}
}
}
//----------------------------------------------------
/**
*@return1销售界面2管理界面
*/
charSaleCenter()
{
charstate='N';
intSum=0;
charPro='d';
//printf("\n********************************\n");
while(true)
{
state='N';
Sum=0;
while(!
(Pro=='c'||Pro=='a'||Pro=='b'||Pro=='q'))
{
system("cls");
printf("\n\t*******************************************");
printf("\n\t*********顾客您好,请选择商品**************\n");
printf("\n\t·a·商品%s·b·商品%s·c·商品%s·q·管理\n",goods[0].name,goods[1].name,goods[2].name);
printf("\n\t-%d--%d--%d-(单位:
件)",goods[0].totalCount,goods[1].totalCount,goods[2].totalCount);
printf("\n\t*******************************************\n\t\t");
Pro=getchar();
}
if(Pro=='q')
break;
switch(Pro)
{
case'a':
{
Sum=Sum+2;
printf("\n\t·商品A价格为%d,请投币·",Sum);
}
break;
case'A':
{
Sum=Sum+2;
printf("\n\t·商品A价格为%d,请投币·",Sum);
}
break;
case'b':
{
Sum=Sum+3;
printf("\n\t·商品B价格为%d,请投币·",Sum);
}
break;
case'B':
{
Sum=Sum+3;
printf("\n\t·商品B价格为%d,请投币·",Sum);
}
break;
case'c':
{
Sum=Sum+6;
printf("\n\t·商品C价格为%d,请投币·",Sum);
}
break;
case'C':
{
Sum=Sum+6;
printf("\n\t·商品C价格为%d,请投币·",Sum);
}
break;
default:
{
printf("\n\t输入物品有误!
!
!
!
\n");
}
break;
};
intMoney;
boolst=false;
while(!
st)
{
//printf("\n输入金额:
");
scanf("%d",&Money);
getchar();
st=GetCharge(Sum,Money);
};
Pro='d';
system("PAUSE");
}
return'2';
}
boolGetCharge(intSum,intMoney)
{
intChargeNum[4];
for(inti=0;i<4;i++)
{
ChargeNum[i]=0;
}
if(Sum>Money)
{
printf("\n\t·所投金额不足支付所选商品,请重新投币·");
returnfalse;
}
elseif(Sum==Money)
{
printf("\n\t·所投金额等于商品价格,请在商品出口处拿取商品·\n");
//销售成功的操作
if(Sum==goods[0].price){
goods[0].totalCount--;
Sale();
tj[0].jrxl++;
tj[0].jrzje+=Sum;
}elseif(Sum==goods[1].price){
goods[1].totalCount--;
Sale();
tj[1].jrxl++;
tj[1].jrzje+=Sum;
}elseif(Sum==goods[2].price){
goods[1].totalCount--;
Sale();
tj[2].jrxl++;
tj[2].jrzje+=Sum;
}
returntrue;
}
else
{
intResult=Money-Sum;
Getsmalcharge(Result,ChargeNum);
printf("\n\t·所投金额大于商品价格,为您找零%d元·\n",Result);
printf("\n\t\t-壹元面值%d张-\n",ChargeNum[0]);
printf("\n\t\t-贰元面值%d张-\n",ChargeNum[1]);
printf("\n\t\t-伍元面值%d张-\n",ChargeNum[2]);
printf("\n\t\t-拾元面值%d张-\n",ChargeNum[3]);
printf("\n\t·请在商品出口处拿取商品·\n");
//printf("\n余额为%d元,应找零\n1元%d张\n2元%d张\n5元%d张\n10元%d张\n",Result,ChargeNum[0],ChargeNum[1],ChargeNum[2],ChargeNum[3]);
}//endif
//销售成功的操作
if(Sum==goods[0].price){
goods[0].totalCount--;
Sale();
tj[0].jrxl++;
tj[0].jrzje+=Sum;
}elseif(Sum==goods[1].price){
goods[1].totalCount--;
Sale();
tj[1].jrxl++;
tj[1].jrzje+=Sum;
}elseif(Sum==goods[2].price){
goods[2].totalCount--;
Sale();
tj[2].jrxl++;
tj[2].jrzje+=Sum;
}
returntrue;
}
/**
成功卖出操作的后台操作
*/
voidSale(){
FILE*fp;
if((fp=fopen("d:
\\goods.out","w"))==NULL)
{
printf("未找到文件d:
\\goods.out...系统将退出\n");
exit(0);
}
fprintf(fp,"%s%d%d\n",goods[0].name,goods[0].totalCount,goods[0].price);
fprintf(fp,"%s%d%d\n",goods[1].name,goods[1].totalCount,goods[1].price);
fprintf(fp,"%s%d%d\n",goods[2].name,goods[2].totalCount,goods[2].price);
//关闭文件
fclose(fp);
fp=NULL;
}
//------------------------------------
voidGetsmalcharge(intMoney,int*ChargeNum)
{
if(Money<0)
return;
intSum=Money;
intcharge[4]={1,2,5,10};
ChargeNum[3]=Sum/charge[3];
Sum=Sum%charge[3];
ChargeNum[2]=Sum/charge[2];
Sum=Sum%charge[2];
ChargeNum[1]=Sum/charge[1];
Sum=Sum%charge[1];
ChargeNum[0]=Sum/charge[0];
Sum=Sum%charge[0];
}
/**
*@return1口令正确0口令错误,并返回销售界面
*/
intKouLing(){
charkl[50];
intcount=0,ret=0;
charrkl[50]="admin";
while(strcmp(rkl,kl)!
=0){
if(count==0){
printf("\n\t*******************************************");
printf("\n\t****************管理口令验证***************\n");
printf("\n\t·Tip·忘记口令,输入“cancel”,进入销售界面\n");
printf("\n\t*******************************************\n\t\t");
}
else
{
printf("\n\t*******************************************");
printf("\n\t****************管理口令验证***************\n");
printf("\n\t·Tip·忘记口令,输入“cancel”,进入销售界面\n");
printf("\n\t*******************************************");
printf("\n\t\t\t\t输入错误次数:
%d次\n\t\t",count);
}
count++;
scanf("%s",kl);
system("CLS");
if(strcmp(kl,"cancel")==0)
break;
}
if(strcmp(kl,"cancel")==0)
ret=0;
else
ret=1;
returnret;
}
/**
*@return'1','2'
*/
charChooseJieMian()
{
charc='3';
while(true){
system("cls");
printf("\n\t*******************************************");
printf("\n\t****************界面选择*******************\n");
printf("\n\t·1·-销售界面-·2·-管理界面-\n");
printf("\n\t*******************************************\n\t\t");
c=getchar();
if(c=='1'||c=='2')
break;
}
system("cls");
returnc;
}
charManageCenter(){
while(true){
intop=7;
while(!
(op==1||op==2||op==3||op==4||op==5||op==6))
{
system("cls");
printf("\n\t*******************************************");
printf("\n\t*************欢迎您,管理员****************\n");
printf("\n\t·1·-库存查询-·2·-商品上架-\n");
printf("\n\t·3·-销售查询-·4·-会计查询-\n");
printf("\n\t·5·-结算功能-·6·-销售界面-\n");
printf("\n\t*******************************************\n\t\t");
scanf("%d",&op);
}
boolflag=false;
switch(op){
case1:
{
KunCun();
}break;
case2:
{
ShangJia();
}break;
case3:
{
TongJiFun();
}break;
case4:
{}break;
case5:
{}break;
case6:
{
flag=true;
}break;
default:
break;
}
if(flag)
break;
else
system("PAUSE");
}
return'1';
}
/***
*从文件里读取商品信息
@return商品列表
*/
voidloadGoodsFrom*gs){
//获得FILE指针
FILE*fp;
if((fp=fopen("d:
\\goods.out","r"))==NULL)
{
printf("未找到文件d:
\\goods.out...系统将退出\n");
exit(0);
}
//Goodsgs[3];
inti=0;
while(!
feof(fp))
{
fscanf(fp,"%s%d%d",gs[i].name,&gs[i].totalCount,&gs[i].price);
i++;
}
//关闭文件
fclose(fp);
fp=NULL;
}
voidKunCun(){
inti=0;
printf("\n\t·商品名称··商品价格(元)··商品余量(件)·\n");
for(i=0;i<3;++i){
printf("\n\t-%s-\t\t-%d-\t\t-%d-\n",goods[i].name,goods[i].price,goods[i].totalCount);
}
}
voidShangJia(){
printf("\n\t·Tip·正数增加,负数减少商品\n");
intgs1=0,gs2=0,gs3=0;
printf("\n\t\t-商品%s(余%d件):
",goods[0].name,goods[0].totalCount);
scanf("%d",&gs1);
printf("\n\t\t-商品%s(余%d件):
",goods[1].name,goods[1].totalCount);
scanf("%d",&gs2);
printf("\n\t\t-商品%s(余%d件):
",goods[2].name,goods[2].totalCount);
scanf("%d",&gs3);
goods[0].totalCount=gs1+goods[0].totalCount<0?
0:
gs1+goods[0].totalCount;
goods[1].totalCount=gs2+goods[1].totalCount<0?
0:
gs2+goods[1].totalCount;
goods[2].totalCount=gs3+goods[2].totalCount<0?
0:
gs3+goods[2].totalCount;
//写文件
//获得FILE指针
FILE*fp;
if((fp=fopen("d:
\\goods.out","w"))==NULL)
{
printf("未找到文件d:
\\goods.out...系统将退出\n");
exit(0);
}
fprintf(fp,"%s%d%d\n",goods[0].name,goods[0].totalCount,goods[0].price);
fprintf(fp,"%s%d%d\n",goods[1].name,goods[1].totalCount,goods[1].price);
fprintf(fp,"%s%d%d\n",goods[2].name,goods[2].totalCount,goods[2].price);
//关闭文件
fclose(fp);
fp=NULL;
}
voidTongJiFun(){
printf("\n\t·今日商品销售情况一览·\n");
printf("\n\t\t-商品%s%d件共%d元-\n",goods[0].name,tj[0].jrxl,tj[0].jrzje);
printf("\n\t\t-商品%s%d件共%d元-\n",goods[1].name,tj[1].jrxl,tj[1].jrzje);
printf("\n\t\t-商品%s%d件共%d元-\n",goods[2].name,tj[2].jrxl,tj[2].jrzje);
printf("\n\t·共计%d件,%d元·\n",tj[0].jrxl+tj[1].jrxl+tj[2].jrxl,tj[0].jrzje+tj[1].jrzje+tj[2].jrzje);
}
//Goods.h
typedefstruct_Goods{
charname[50];
inttotalCount;
intprice;
}Goods;
typedefstruct_TongJi{
intjrxl;
intjrzje;
}TongJi;