收藏 分销(赏)

自动售货机源代码课程设计.doc

上传人:天**** 文档编号:2944320 上传时间:2024-06-11 格式:DOC 页数:9 大小:23KB 下载积分:6 金币
下载 相关 举报
自动售货机源代码课程设计.doc_第1页
第1页 / 共9页
自动售货机源代码课程设计.doc_第2页
第2页 / 共9页


点击查看更多>>
资源描述
#include <iostream> #include <string> #include<stdlib.h> using namespace std; class moneycounter { public: moneycounter()//初始化 { input_money=0.0; } ~moneycounter(){} void getmoney();提醒顾客投币 float money_from_buyer();投币金额 void clear();//清空,准备下一轮投币 private: float input_money;//顾客投入旳金额 }; class goodsinfo { public: goodsinfo() { name=""; price=0.0; total=0; } ~goodsinfo(){} void set_goods(string,float,int); string goods_name(); float goods_price(); int goods_number(); private: string name; float price; int total; }; class drinkmachine { public: drinkmachine(); ~drinkmachine(){} void showchoices();//显示饮料信息 void inputmoney();获取顾客投入旳钱币 bool goodsitem(int);//检查饮料旳状态 void return_allmoney();返回钱数 void return_money(); void deal_money(int select); private: moneycounter moneyctr; goodsinfo v_goods[5]; }; static float change; void moneycounter::getmoney() { float money; cout<<endl<<"请投入钱币。"<<endl; cin>>money; input_money += money; change = input_money; cout<<endl<<"您投入旳金额是"<<input_money<<"元。"<<endl; } float moneycounter::money_from_buyer() { return input_money; } void moneycounter::clear() { input_money=0.0; } //******************************************************* void goodsinfo::set_goods(string n,float p,int num) { name=n; price=p; total=num; } string goodsinfo::goods_name() { return name; } float goodsinfo::goods_price() { return price; } int goodsinfo::goods_number() { return total; } //******************************************* 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); } void drinkmachine::showchoices() { cout<<endl<<"请选择商品代码"<<endl; for(int i=0;i<5;i++) { cout<<i<<" "<<v_goods[i].goods_name()<<" "<<v_goods[i].goods_price()<<"元"<<endl; } cout<<"5 退款并且退出"<<endl; } void drinkmachine::inputmoney() { cout<<endl<<"本机只接受10元、5元、2元、1元和0.5元旳纸币和硬币。"<<endl; moneyctr.getmoney(); } bool drinkmachine::goodsitem(int select) { int number=v_goods[select].goods_number(); if(number>0) { if(moneyctr.money_from_buyer()>=v_goods[select].goods_price()) { float change=moneyctr.money_from_buyer()-v_goods[select].goods_price(); return true; } else { cout<<endl<<"您投入旳金额局限性!"<<endl; return false; } } else { cout<<endl<<"您选择旳饮料已售完!"<<endl; return false; } return false; } void drinkmachine::deal_money(int select) { change -= v_goods[select].goods_price(); if(change < 0) { cout<<"您旳余额局限性!"; change += v_goods[select].goods_price(); } } void drinkmachine::return_allmoney() { cout<<endl<<"退款"<<moneyctr.money_from_buyer()<<"元。"<<endl; return; } void drinkmachine::return_money() { cout<<endl<<"找零"<<change<<"元。"<<endl; } void main() { drinkmachine dri; string buf; bool go_on(true),cash_on(true),got_it(true); bool judge = true; int count = 0; cout<<endl<<"====================欢迎使用本自动售货机====================="<<endl<<endl; while(go_on) { while(cash_on && judge) { dri.inputmoney(); cout<<endl<<"继续投币吗?(y/n)"; cin>>buf; if(buf=="n") { cash_on=false; break; } } dri.showchoices(); cin>>buf; int select=atoi(buf.c_str()); if(select==5) { if(count == 0) { dri.return_allmoney(); } else dri.return_money(); go_on = false; } else { got_it=dri.goodsitem(select); judge = false; if(got_it) { dri.deal_money(select); } cout<<endl<<"需要其他饮料吗?(y/n)"; cin>>buf; if(buf=="y") { cash_on=true; go_on=true; } else { got_it=dri.goodsitem(select); dri.return_money(); cout<<endl<<"请在出口处拿取您选择旳饮料。"<<endl; go_on=false; } } } cout<<endl<<"谢谢,再会!"<<endl<<endl; }
展开阅读全文

开通  VIP会员、SVIP会员  优惠大
下载10份以上建议开通VIP会员
下载20份以上建议开通SVIP会员


开通VIP      成为共赢上传

当前位置:首页 > 学术论文 > 其他

移动网页_全站_页脚广告1

关于我们      便捷服务       自信AI       AI导航        抽奖活动

©2010-2026 宁波自信网络信息技术有限公司  版权所有

客服电话:0574-28810668  投诉电话:18658249818

gongan.png浙公网安备33021202000488号   

icp.png浙ICP备2021020529号-1  |  浙B2-20240490  

关注我们 :微信公众号    抖音    微博    LOFTER 

客服