C++自动售货机源代码-课程设计

整理文档很辛苦,赏杯茶钱您下走!

免费阅读已结束,点击下载阅读编辑剩下 ...

阅读已结束,您可以下载文档离线阅读编辑

资源描述

#includeiostream#includestring#includestdlib.husingnamespacestd;classmoneycounter{public:moneycounter()//初始化{input_money=0.0;}~moneycounter(){}voidgetmoney();提示顾客投币floatmoney_from_buyer();投币金额voidclear();//清空,准备下一轮投币private:floatinput_money;//顾客投入的金额};classgoodsinfo{public:goodsinfo(){name=;price=0.0;total=0;}~goodsinfo(){}voidset_goods(string,float,int);stringgoods_name();floatgoods_price();intgoods_number();private:stringname;floatprice;inttotal;};classdrinkmachine{public:drinkmachine();~drinkmachine(){}voidshowchoices();//显示饮料信息voidinputmoney();获取顾客投入的钱币boolgoodsitem(int);//检查饮料的状态voidreturn_allmoney();返回钱数voidreturn_money();voiddeal_money(intselect);private:moneycountermoneyctr;goodsinfov_goods[5];};staticfloatchange;voidmoneycounter::getmoney(){floatmoney;coutendl请投入钱币。endl;cinmoney;input_money+=money;change=input_money;coutendl您投入的金额是input_money元。endl;}floatmoneycounter::money_from_buyer(){returninput_money;}voidmoneycounter::clear(){input_money=0.0;}//*******************************************************voidgoodsinfo::set_goods(stringn,floatp,intnum){name=n;price=p;total=num;}stringgoodsinfo::goods_name(){returnname;}floatgoodsinfo::goods_price(){returnprice;}intgoodsinfo::goods_number(){returntotal;}//*******************************************drinkmachine::drinkmachine(){v_goods[0].set_goods(橙汁,3,20);v_goods[1].set_goods(咖啡,5,0);v_goods[2].set_goods(纯净水,1.5,20);v_goods[3].set_goods(可口可乐,2,30);v_goods[4].set_goods(百事可乐,2,28);}voiddrinkmachine::showchoices(){coutendl请选择商品代码endl;for(inti=0;i5;i++){coutiv_goods[i].goods_name()v_goods[i].goods_price()元endl;}cout5退款并且退出endl;}voiddrinkmachine::inputmoney(){coutendl本机只接受10元、5元、2元、1元和0.5元的纸币和硬币。endl;moneyctr.getmoney();}booldrinkmachine::goodsitem(intselect){intnumber=v_goods[select].goods_number();if(number0){if(moneyctr.money_from_buyer()=v_goods[select].goods_price()){floatchange=moneyctr.money_from_buyer()-v_goods[select].goods_price();returntrue;}else{coutendl您投入的金额不足!endl;returnfalse;}}else{coutendl您选择的饮料已售完!endl;returnfalse;}returnfalse;}voiddrinkmachine::deal_money(intselect){change-=v_goods[select].goods_price();if(change0){cout您的余额不足!;change+=v_goods[select].goods_price();}}voiddrinkmachine::return_allmoney(){coutendl退款moneyctr.money_from_buyer()元。endl;return;}voiddrinkmachine::return_money(){coutendl找零change元。endl;}voidmain(){drinkmachinedri;stringbuf;boolgo_on(true),cash_on(true),got_it(true);booljudge=true;intcount=0;coutendl====================欢迎使用本自动售货机=====================endlendl;while(go_on){while(cash_on&&judge){dri.inputmoney();coutendl继续投币吗?(y/n);cinbuf;if(buf==n){cash_on=false;break;}}dri.showchoices();cinbuf;intselect=atoi(buf.c_str());if(select==5){if(count==0){dri.return_allmoney();}elsedri.return_money();go_on=false;}else{got_it=dri.goodsitem(select);judge=false;if(got_it){dri.deal_money(select);}coutendl需要其他饮料吗?(y/n);cinbuf;if(buf==y){cash_on=true;go_on=true;}else{got_it=dri.goodsitem(select);dri.return_money();coutendl请在出口处拿取您选择的饮料。endl;go_on=false;}}}coutendl谢谢,再见!endlendl;}

1 / 6
下载文档,编辑使用

©2015-2020 m.777doc.com 三七文档.

备案号:鲁ICP备2024069028号-1 客服联系 QQ:2149211541

×
保存成功