自动售货机C++(共7页).doc
精选优质文档-倾情为你奉上一、程序名称:自动售货机二、程序功能:将程序中的数组输出,并分别按从大到小和从小到大的顺序输出,在输出4.4三、程序设计及实现。1、主函数:main()实现程序的主界面2、public公有成员、private私有成员3、数据设置为string字符串类型、float单精度类型、int整形4、输出:cout<< <<用来输出显示的内容5、选择语句:switch() case1:6、实现简单的界面设计,类的成员变量,调用函数,循环,if语句可操作销售、支付、上架、找零等功能四、程序运行结果:五、程序代码/头文件 #include <iostream>#include <string>#include <vector>#include <cstdlib>using namespace std; /命名空间struct DrinkDrink(float pr, string n);string name; /字符串类型 float price; /单精度 int account; /整形 ;class Vendingpublic: Vending(); /类的公有成员inline void showStorage() const cout << "可乐:" << m_cola->account << endl << "奶茶:" << m_milkT->account << endl;cout << "凉茶:" << m_T->account << endl;inline void showSoldNum() constcout << m_cola->name << endl << "销售量:" << m_soldCola << endl;cout << "销售额:" << m_soldCola*(m_cola->price) << endl;cout << m_milkT->name << endl << "销售量:" << m_soldMilkTea << endl;cout << "销售额:" << m_soldMilkTea*(m_milkT->price) << endl;cout << m_T->name << endl << "销售量:" << m_soldTea << endl;cout << "销售额:" << m_soldTea*(m_T->price) << endl;void addStore();void sellItem();void popMenu();private: /类的私有成员 Drink *m_cola, *m_milkT, *m_T;int m_soldCola, m_soldMilkTea, m_soldTea;Drink:Drink(float pr, string n):name(n), price(pr), account(10) /名称、价格、库存数量 /销售界面Vending:Vending():m_soldCola(0), m_soldMilkTea(0), m_soldTea(0)m_cola = new Drink(float(2.8), "可乐");m_milkT = new Drink(float(3.0), "奶茶");m_T = new Drink(float(3.5), "凉茶");/上架界面 void Vending:addStore()cout << "请选择要上架的商品:n1.可乐n2.奶茶n3.凉茶n"int n, count;cin >> n;if(n > 3)cout << "选择错误" << endl;return;cout << "请输入要上架的数量:"cin >> count;if(n = 1)m_cola->account += count;else if(n = 2)m_milkT->account += count;else if(n = 3)m_T->account += count;elsecout << "操作失败" << endl;return;cout << "操作完成" << endl;/销售界面 void Vending:sellItem()cout << "1." << m_cola->name << ":" << endl;cout << "价格:" << m_cola->price << ends << "库存:" << m_cola->account << (m_cola->account = 0 ? "(售完)n" : "n");cout << "2." << m_milkT->name << ":" << endl;cout << "价格:" << m_milkT->price << ends << "库存:" << m_milkT->account << (m_milkT->account = 0 ? "(售完)n" : "n");cout << "3." << m_T->name << ":" << endl;cout << "价格:" << m_T->price << ends << "库存:" << m_T->account << (m_T->account = 0 ? "(售完)n" : "n");cout << "请选择:"int n = 0, num = 0;cin >> n;float price = 0;int acc = 0;if(n = 1)price = m_cola->price;acc = m_cola->account;else if(n = 2)price = m_milkT->price;acc = m_milkT->account;else if(n = 3)price = m_T->price;acc = m_T->account;do if(num > 0)cout << "库存不足" << endl;cout << "请输入数量:"cin >> num;while(num > acc);price *= num;/支付 float pay = 0;do if(pay > 0)cout << "支付不足" << endl;cout << "请支付:"cin >> pay;while (pay < price);cout << "操作完成" << endl;if(pay - price > 0)cout << "找零:" << pay-price << endl;if(n = 1)m_soldCola += num;m_cola->account -= num;else if(n = 2)m_soldMilkTea += num;m_milkT->account -= num;else if(n = 3)m_soldTea += num;m_T->account -= num;void Vending:popMenu()const string menu1 = "=自动贩卖机=n1.管理人员界面n2.销售界面n3.退出n"const string menu2 = "=自动贩卖机=n1.显示库存n2.商品上架n3.销售查询n"int n;while(true)cout << menu1 << "请选择:"cin >> n;if(n = 1)cout << "请输入密码:"int password;cin >> password;if(password != 123) /密码为123,输入不是123则现实密码错误 cout << "密码错误" << endl;continue;cout << menu2 << "请选择:"cin >> n;switch(n) /选择语句 case 1:showStorage();break; case 2:addStore();break; case 3:showSoldNum();break;default:cout << "选择错误" << endl;return;else if(n = 2)sellItem();else if(n = 3)exit(0);/main() 函数的实现,程序的主界面的引导 int main()Vending *vend = new Vending;vend->popMenu();return 0;专心-专注-专业