自动售货机系统程序_第1页
自动售货机系统程序_第2页
自动售货机系统程序_第3页
自动售货机系统程序_第4页
自动售货机系统程序_第5页
已阅读5页,还剩18页未读 继续免费阅读

下载本文档

版权说明:本文档由用户提供并上传,收益归属内容提供方,若内容存在侵权,请进行举报或认领

文档简介

1、Coin类:#include <iostream>#include <string>#include <vector> using namespace std; class Coinpublic:   /*      Constructs a coin with a given name and value      param n the coin name     

2、param v the coin value   */   Coin(string n, double v);   /*      Gets the coin name.      return the name   */   string get_name() const;   /*      Gets the coin va

3、lue      return the value   */   double get_value() const;private:   string name;   double value; Coin:Coin(string n, double v)   name = n;   value = v; string Coin:get_name() const   return name

4、; double Coin:get_value() const   return value;  Product类:#include <iostream>#include <string>#include <vector> using namespace std; class Productpublic:   /*      Constructs a product with a given name, price an

5、d quantity      param n the product name      param p the price      param q the quantity   */   Product(string n, double p, int q);   /*      Gets the product name 

6、     return the name   */   string get_name() const;   /*      Gets the product price      return the price   */   double get_price() const;   /*    &

7、#160; Gets the product quantity      return the quantity   */   int get_quantity() const;   /*      Adds to the product quantity      param amount the amount to add   */   voi

8、d 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() const   return name; d

9、ouble Product:get_price() const   return price; int Product:get_quantity() const   return quantity; void Product:add_quantity(int amount)   quantity = quantity + amount;  VendingMachine类: class VendingMachinepublic:   /*  

10、60;   Constructs a vending machine with no current      product selection.   */   VendingMachine();   /*      Adds product to the machine.      param p the product to add   */

11、   void add_product(Product p);   /*      Sets the currently selected product      param name the product name      return true if the machine has a product with the given      nam

12、e   */   bool select_product(string name);   void chaxun_product();/查询当前售货机内的商品   /*      Adds a coin to pay for the currently selected product.      param c the coin to add      return

13、 true if sufficient coins have been added to pay      for the selected product.   */   bool add_coin(vector<Coin> current_pay);   /*      Removes all coins that were added to pay for the current   &#

14、160;  product.      return the value of the returned coins   */   double return_coins();   /*      Removes all money that was paid for products.      return the value of the money  

15、; */   double remove_money();double add_coinbijiao(vector<Coin> current_pay );/对投入的金钱和所购买的商品的价格进行比较double return_yiyoucoins();/统计售货机中已有的货款void setcurrent_product();/把当前选择的商品代号置为-1int getcurrent_product();/得到当前选择的商品代号private:   vector<Product> products;   int

16、 current_product;   vector<Coin> current_payment;   vector<Coin> coins; VendingMachine:VendingMachine()   current_product = -1; void VendingMachine:add_product(Product p)/添加商品   for (int i = 0; i < products.size(); i+)   

17、0;     if (productsi.get_name() = p.get_name() && productsi.get_price() = p.get_price()               productsi.add_quantity(p.get_quantity();           

18、;    cout<<"添加成功!"<<endl;         return;               else if(productsi.get_name() = p.get_name() && productsi.get_price() != p.get_price()  &#

19、160;                      cout<<"已存在该商品,与您输入的价格不同!"<<endl;                return;      

20、        products.push_back(p);   cout<<"添加成功!"<<endl; bool VendingMachine:select_product(string name)/选择商品                 int i;     

21、0;        for ( i = 0;  i < products.size(); i+)                                   if (productsi.

22、get_name() = name && productsi.get_quantity() > 0)                                        

23、0;        current_product = i;                            return true;          

24、                                else                   

25、                              if(productsi.get_name() = name && productsi.get_quantity() = 0)          

26、;                                                  

27、0;  cout << "对不起,该商品已售完!n"                                   return false;      

28、;                                                   

29、60;                   if(i=products.size()                             

30、0;     cout<<"对不起,不存在该商品!"<<endl;                     return false;               double

31、 VendingMachine:return_yiyoucoins()/统计售货机当前的金钱总额   double total = 0;   for (int i = coins.size() - 1; i >= 0; i-)         total = total + coinsi.get_value();      return total; bool VendingMachine:add_coin(vector<C

32、oin> current_pay )/投币购买商品       if (current_product = -1) return false;              for(int k=0;k < current_pay.size(); k+)            

33、60;                      current_payment.push_back(current_payk);                       &#

34、160;    double total = 0;              for (int i = 0; i < current_payment.size(); i+)/计算投入的总钱数                     &

35、#160;             total = total + current_paymenti.get_value();                            if (total >= pro

36、ductscurrent_product.get_price()/投入的钱数与所购商品的价格比较                                   for (int i = current_payment.size() - 1; i >= 0;

37、i-)                                                 coins.push_ba

38、ck(current_paymenti);                            current_payment.pop_back();               

39、;                           productscurrent_product.add_quantity(-1);                 

40、0;   current_product = -1;                     cout<<"交易成功!"<<endl;                 

41、;    return true;                            else                   &#

42、160;                return false;               double VendingMachine:add_coinbijiao(vector<Coin> current_pay )       i

43、f (current_product = -1) return false;       double total = 0;       for(int k=0;k < current_pay.size(); k+)                     total = total

44、 + current_payk.get_value();           double m=productscurrent_product.get_price()-total;   if (m>0)              return m;   else      

45、    return -1;   double VendingMachine:return_coins()/统计当前投入的金钱总额   double total = 0;   for (int i = current_payment.size() - 1; i >= 0; i-)         total = total + current_paymenti.get_value();    

46、;  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();&#

47、160;     coins.pop_back();      return total; void VendingMachine:chaxun_product()        if(products.size()=0)                    

48、; cout<<"暂时没有添加商品!"<<endl;              else                     cout<<"商品名"<<"tt"<<&

49、quot;价格"<<"tt"<<"数量"<<endl;              for (int i = 0; i < products.size(); i+)                   

50、;                cout<<productsi.get_name()<<"tt"<<productsi.get_price()<<"tt"<<productsi.get_quantity()<<endl;         

51、0;            void VendingMachine:setcurrent_product()       current_product=-1; int VendingMachine:getcurrent_product()       return current_product;  Main函数:#include <ios

52、tream>#include <string>#include <vector>#include"coin.h"#include"product.h"#include"machine.h"using namespace std; void main()   vector<Coin> coins;   vector<Coin> current_pay;   coins.push_back(Coin("ni

53、ckel", 0.05);   coins.push_back(Coin("dime", 0.1);   coins.push_back(Coin("quarter", 0.25);   coins.push_back(Coin("rmb", 1.00);    VendingMachine machine;   bool more = true;   machine.chaxun_product

54、();   while (more)         cout << "a)添加商品 x)查询商品 s)选择商品 p)投币 c)取消 e)查询当前金额 r)取款 q)退出: "      string command;      cin>>command;      if (command = "a&qu

55、ot;)                           cout << "商品名: "                   

56、;  string name;                     cin>>name;                        

57、0;                        cout << "价格: "                     double price;

58、                     cin >> price;                     cout << "数量: " 

59、60;                   int quantity;                     cin >> quantity;     &#

60、160;               machine.add_product(Product(name, price, quantity);                    else if (command = "s")   

61、;                   double total1;                total1=machine.return_yiyoucoins();           

62、;     if(total1>=5000)/当售货机中的货款大于等于5000时,暂停售货                                         cout<<&quo

63、t;对不起,现在暂停售货!"<<endl;                                else                 

64、                    machine.setcurrent_product();                     cout << "商品名: "

65、0;                    string name;                     cin>>name;     

66、0;               machine.select_product(name);                            else if (command = "p")

67、60;                       if(machine.getcurrent_product()!=-1)/如果当前已选择商品,才可以投币                      

68、                           bool panduan=false;                    

69、0;       while(!panduan)/多次投币的实现                                        

70、60;                      cout << "所投钱币名称( 以 # 结束投币过程):"                    

71、0;              string name;                                   ci

72、n>>name;                                   while(name!="#")         &

73、#160;                                                  

74、                 bool found = false;                              

75、0;           for (int i = 0; !found && i < coins.size(); i+)                              

76、;                                                  

77、0;          if (coinsi.get_name() = name)                                   

78、0;                                                  

79、60;                  current_pay.push_back(coinsi);                           &#

80、160;                            found=true;                    &#

81、160;                                                  &

82、#160;                                                  

83、           if(!found)                                      &

84、#160;                                                  

85、  cout << "不存在该货币,请重新投入:n"                                           

86、                                         cin>>name;       &

87、#160;                                                   

88、;     double k=machine.add_coinbijiao(current_pay );                                   if(k=-1)   &#

89、160;                                                  &

90、#160;                       machine.add_coin(current_pay);                      

91、                    for (int i = current_pay.size() - 1; i >= 0; i-)                      &#

92、160;                                                  &

93、#160;                 current_pay.pop_back();                             &

94、#160;                                                  

95、           panduan=true;                                      

96、60;                               else                  

97、0;                                                   &#

98、160;           cout<<"您的金额不足,还差 "<<k<<" 是否继续投币 Y  N :"                         

99、60;                string jixu;                                &#

100、160;         cin>>jixu;                                      

101、60;   if(jixu!="Y")                                            &#

102、160;                                              machine.setcurrent_produc

103、t();                                                 double

104、total=0;                                                 for

105、 (int i = current_pay.size() - 1; i >= 0; i-)                                          

106、                                                   

107、;            total = total + current_payi.get_value();                                

108、;                        current_pay.pop_back();                       

109、;                                                  

110、0;                        cout << "Returned(退还) " <<total<< "n"              

111、                                   panduan=true;             

112、0;                                                  

113、60;                                                   &

114、#160;                    else                       cout<<"您还没有选择商品!"<<endl;   

115、;         else if (command = "c")/取消操作并并退还货款                            machine.setcurrent_product();      

116、60;         double total=0;                for (int i = current_pay.size() - 1; i >= 0; i-)                  

117、60;                      total = total + current_payi.get_value();                      &#

118、160;     current_pay.pop_back();                                cout << "Returned(退还) " <<total<< "n"&

119、#160;               else if (command = "r")         cout << "Removed (取走) " << machine.remove_money() << "n"        

120、else if(command="x")                         machine.chaxun_product();                  else if(command="e&quo

121、t;)/查询当前售货机中的货款               cout<<"当前金额:"<<machine.return_yiyoucoins()<<endl;         else if(command = "q")        

122、; more = false;     double VendingMachine:remove_money()/取钱   double total = 0;   for (int i = coins.size() - 1; i >= 0; i-)         total = total + coinsi.get_value();      coins.pop_back();    

温馨提示

  • 1. 本站所有资源如无特殊说明,都需要本地电脑安装OFFICE2007和PDF阅读器。图纸软件为CAD,CAXA,PROE,UG,SolidWorks等.压缩文件请下载最新的WinRAR软件解压。
  • 2. 本站的文档不包含任何第三方提供的附件图纸等,如果需要附件,请联系上传者。文件的所有权益归上传用户所有。
  • 3. 本站RAR压缩包中若带图纸,网页内容里面会有图纸预览,若没有图纸预览就没有图纸。
  • 4. 未经权益所有人同意不得将文件中的内容挪作商业或盈利用途。
  • 5. 人人文库网仅提供信息存储空间,仅对用户上传内容的表现方式做保护处理,对用户上传分享的文档内容本身不做任何修改或编辑,并不能对任何下载内容负责。
  • 6. 下载文件中如有侵权或不适当内容,请与我们联系,我们立即纠正。
  • 7. 本站不保证下载资源的准确性、安全性和完整性, 同时也不承担用户因使用这些下载资源对自己和他人造成任何形式的伤害或损失。

评论

0/150

提交评论