自动售货机系统程序

上传人:第*** 文档编号:38959003 上传时间:2018-05-09 格式:DOC 页数:18 大小:85.50KB
返回 下载 相关 举报
自动售货机系统程序_第1页
第1页 / 共18页
自动售货机系统程序_第2页
第2页 / 共18页
自动售货机系统程序_第3页
第3页 / 共18页
自动售货机系统程序_第4页
第4页 / 共18页
自动售货机系统程序_第5页
第5页 / 共18页
点击查看更多>>
资源描述

《自动售货机系统程序》由会员分享,可在线阅读,更多相关《自动售货机系统程序(18页珍藏版)》请在金锄头文库上搜索。

1、Coin 类:类:#include #include #include using namespace std;class Coinpublic:/*Constructs a coin with a given name and valueparam n the coin nameparam v the coin value*/Coin(string n, double v);/*Gets the coin name.return the name*/string get_name() const;/*Gets the coin valuereturn the value*/double ge

2、t_value() const;private:string name;double value;Coin:Coin(string n, double v)name = n;value = v;string Coin:get_name() constreturn name;double Coin:get_value() constreturn value;Product 类:类:#include #include #include using namespace std;class Productpublic:/*Constructs a product with a given name,

3、price and quantityparam n the product nameparam p the priceparam q the quantity*/Product(string n, double p, int q);/*Gets the product namereturn the name*/string get_name() const;/*Gets the product pricereturn the price*/double get_price() const;/*Gets the product quantityreturn the quantity*/int g

4、et_quantity() const;/*Adds to the product quantityparam amount the amount to add*/void add_quantity(int amount);private:string name;double price;int quantity;Product:Product(string n, double p, int q)name = n;price = p;quantity = q;string Product:get_name() constreturn name;double Product:get_price(

5、) constreturn price;int Product:get_quantity() constreturn quantity;void Product:add_quantity(int amount)quantity = quantity + amount;VendingMachine 类:类:class VendingMachinepublic:/*Constructs a vending machine with no currentproduct selection.*/VendingMachine();/*Adds product to the machine.param p

6、 the product to add*/void add_product(Product p);/*Sets the currently selected productparam name the product namereturn true if the machine has a product with the givenname*/bool select_product(string name);void chaxun_product();/查询当前售货机内的商品查询当前售货机内的商品/*Adds a coin to pay for the currently selected

7、product.param c the coin to addreturn true if sufficient coins have been added to payfor the selected product.*/bool add_coin(vector current_pay);/*Removes all coins that were added to pay for the currentproduct.return the value of the returned coins*/double return_coins();/*Removes all money that w

8、as paid for products.return the value of the money*/double remove_money();double add_coinbijiao(vector current_pay );/对投入的金钱和所购买的商品的价格进行对投入的金钱和所购买的商品的价格进行比较比较double return_yiyoucoins();/统计售货机中已有的货款统计售货机中已有的货款void setcurrent_product();/把当前选择的商品代号置为把当前选择的商品代号置为-1int getcurrent_product();/得到当前选择的商品代号得到

9、当前选择的商品代号private:vector products;int current_product;vector current_payment;vector coins;VendingMachine:VendingMachine()current_product = -1;void VendingMachine:add_product(Product p)/添加商品添加商品for (int i = 0; i 0)current_product = i;return true;elseif(productsi.get_name() = name i-)total = total + co

10、insi.get_value();return total;bool VendingMachine:add_coin(vector current_pay )/投币购买商品投币购买商品if (current_product = -1) return false;for(int k=0;k = productscurrent_product.get_price()/投入的钱数与所购商品的价格比较投入的钱数与所购商品的价格比较for (int i = current_payment.size() - 1; i = 0; i-)coins.push_back(current_paymenti);cu

11、rrent_payment.pop_back();productscurrent_product.add_quantity(-1);current_product = -1;cout current_pay )if (current_product = -1) return false;double total = 0;for(int k=0;k 0)return m;elsereturn -1;double VendingMachine:return_coins()/统计当前投入的金钱总额统计当前投入的金钱总额double total = 0;for (int i = current_pay

12、ment.size() - 1; i = 0; i-)total = total + current_paymenti.get_value();current_payment.pop_back();return total;double VendingMachine:remove_money()/取钱取钱double total = 0;for (int i = coins.size() - 1; i = 0; i-)total = total + coinsi.get_value();coins.pop_back();return total;void VendingMachine:chax

13、un_product() if(products.size()=0)cout#include #include #include“coin.h“#include“product.h“#include“machine.h“using namespace std;void main()vector coins;vector current_pay;coins.push_back(Coin(“nickel“, 0.05);coins.push_back(Coin(“dime“, 0.1);coins.push_back(Coin(“quarter“, 0.25);coins.push_back(Co

14、in(“rmb“, 1.00);VendingMachine machine;bool more = true;machine.chaxun_product();while (more)cout command;if (command = “a“)cout name;cout price;cout quantity;machine.add_product(Product(name, price, quantity);else if (command = “s“)double total1;total1=machine.return_yiyoucoins();if(total1=5000)/当售

15、货机中的货款大于等于当售货机中的货款大于等于 5000 时,暂停售货时,暂停售货 coutname;machine.select_product(name);else if (command = “p“) if(machine.getcurrent_product()!=-1)/如果当前已选择商品,才可以投币如果当前已选择商品,才可以投币bool panduan=false;while(!panduan)/多次投币的实现多次投币的实现cout name;while(name!=“#“)bool found = false;for (int i = 0; !found double k=mach

16、ine.add_coinbijiao(current_pay );if(k=-1) machine.add_coin(current_pay);for (int i = current_pay.size() - 1; i = 0; i-)current_pay.pop_back();panduan=true;else coutjixu;if(jixu!=“Y“)machine.setcurrent_product();double total=0;for (int i = current_pay.size() - 1; i = 0; i-)total = total + current_payi.get_value(

展开阅读全文
相关资源
相关搜索

当前位置:首页 > 中学教育 > 其它中学文档

电脑版 |金锄头文库版权所有
经营许可证:蜀ICP备13022795号 | 川公网安备 51140202000112号