C++点餐
时间: 2023-07-11 13:10:14 浏览: 52
以下是一个简单的 C++ 点餐系统的实现:
```C++
#include <iostream>
#include <string>
#include <vector>
#include <map>
using namespace std;
struct Dish {
string name; // 菜品名称
double price; // 菜品价格
};
class OrderSystem {
public:
OrderSystem() {
// 初始化菜品信息
dishes_["A"] = { "炸鸡", 18.0 };
dishes_["B"] = { "汉堡", 15.0 };
dishes_["C"] = { "薯条", 8.0 };
dishes_["D"] = { "可乐", 4.0 };
}
void run() {
// 显示菜单
showMenu();
// 输入订单
string order;
getline(cin, order);
while (order != "0") {
double totalPrice = 0.0;
map<string, int> count;
// 统计数量和价格
for (int i = 0; i < order.length(); i++) {
string dish = string(1, order[i]);
if (dishes_.find(dish) != dishes_.end()) {
count[dish]++;
totalPrice += dishes_[dish].price;
}
}
// 输出订单信息
cout << "您的订单:\n";
for (auto item : count) {
cout << dishes_[item.first].name << " * " << item.second << endl;
}
cout << "总价:" << totalPrice << " 元\n";
// 输入下一个订单
cout << "请输入您的点餐信息:\n";
getline(cin, order);
}
}
private:
void showMenu() {
cout << "欢迎光临,请查看我们的菜单:\n";
for (auto item : dishes_) {
cout << item.first << " - " << item.second.name << ":"
<< item.second.price << " 元\n";
}
cout << "请根据菜单输入您的点餐信息(例如:ABCD),输入 0 结束:\n";
}
map<string, Dish> dishes_;
};
int main() {
OrderSystem system;
system.run();
return 0;
}
```
以上代码实现了一个简单的点餐系统,可以根据实际需求进行修改和扩展。