C++实现点菜系统
时间: 2023-09-23 22:03:27 浏览: 110
一个简单的点菜系统可以分为以下几个部分:
1. 菜单:包括菜品名称、价格等信息。
2. 订单:记录顾客所点的菜品和数量。
3. 结算:计算订单总价并输出。
下面是一个简单的C++实现:
```c++
#include <iostream>
#include <vector>
using namespace std;
// 菜单项
struct MenuItem {
string name;
double price;
};
// 订单项
struct OrderItem {
string name;
int quantity;
};
int main() {
vector<MenuItem> menu = {{"鱼香肉丝", 25.0}, {"宫保鸡丁", 30.0}, {"麻婆豆腐", 20.0}, {"清蒸鲈鱼", 50.0}};
vector<OrderItem> order;
while (true) {
// 输出菜单
cout << "======================" << endl;
cout << " 菜单" << endl;
cout << "======================" << endl;
for (int i = 0; i < menu.size(); i++) {
cout << i + 1 << ". " << menu[i].name << " " << menu[i].price << "元" << endl;
}
// 选择菜品
cout << "请选择菜品(输入0结束选择):" << endl;
int choice;
cin >> choice;
if (choice == 0) {
break;
}
if (choice < 1 || choice > menu.size()) {
cout << "选择无效,请重新选择!" << endl;
continue;
}
// 输入数量
cout << "请输入数量:" << endl;
int quantity;
cin >> quantity;
if (quantity < 1) {
cout << "数量无效,请重新输入!" << endl;
continue;
}
// 添加到订单中
OrderItem item = {menu[choice - 1].name, quantity};
order.push_back(item);
cout << "已添加到订单中!" << endl;
}
// 输出订单
if (order.size() == 0) {
cout << "订单为空!" << endl;
return 0;
}
cout << "======================" << endl;
cout << " 订单" << endl;
cout << "======================" << endl;
double total = 0.0;
for (int i = 0; i < order.size(); i++) {
cout << order[i].name << " x " << order[i].quantity << " = " << menu[i].price * order[i].quantity << "元" << endl;
total += menu[i].price * order[i].quantity;
}
cout << "总价:" << total << "元" << endl;
return 0;
}
```
该程序使用了vector来存储菜单和订单信息,并使用了结构体来表示菜单项和订单项。用户可以通过选择菜品和输入数量来将菜品加入到订单中,然后程序会计算订单总价并输出。
相关推荐
![pdf](https://img-home.csdnimg.cn/images/20210720083512.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![application/x-rar](https://img-home.csdnimg.cn/images/20210720083606.png)