使用c++以及链表的结构写一个点餐系统,尽可能详细功能丰富
时间: 2024-03-24 20:38:46 浏览: 64
基于C++的点餐系统设计
好的,我可以为您提供一些 C++ 链表点餐系统的实现思路和代码示例。这个系统可以实现以下功能:
1. 菜单的展示
2. 点餐和加菜
3. 订单的展示和总价的计算
4. 取消订单和删除菜品
5. 导出订单到文件
接下来,让我们来一步步实现这些功能。
首先,我们需要定义一个菜单类(Menu),用于存储所有的菜品信息。每个菜品包含名字、价格和描述等信息。我们可以使用链表来存储所有的菜品信息。
```c++
#include <iostream>
#include <fstream>
#include <string>
using namespace std;
// 定义菜品结构体
struct MenuItem {
string name;
string description;
double price;
MenuItem* next;
};
class Menu {
public:
Menu() {
head = NULL;
size = 0;
}
// 添加菜品
void addMenuItem(string name, string description, double price) {
MenuItem* item = new MenuItem;
item->name = name;
item->description = description;
item->price = price;
item->next = head;
head = item;
size++;
}
// 展示菜单
void showMenu() {
MenuItem* current = head;
while (current != NULL) {
cout << current->name << " - " << current->description << " - " << current->price << endl;
current = current->next;
}
}
// 导出菜单到文件
void exportMenuToFile(string fileName) {
ofstream outFile;
outFile.open(fileName);
MenuItem* current = head;
while (current != NULL) {
outFile << current->name << "," << current->description << "," << current->price << endl;
current = current->next;
}
outFile.close();
}
// 从文件导入菜单
void importMenuFromFile(string fileName) {
ifstream inFile;
inFile.open(fileName);
string line;
while (getline(inFile, line)) {
string name, description;
double price;
int pos1 = line.find(",");
name = line.substr(0, pos1);
int pos2 = line.find(",", pos1 + 1);
description = line.substr(pos1 + 1, pos2 - pos1 - 1);
price = stod(line.substr(pos2 + 1));
addMenuItem(name, description, price);
}
inFile.close();
}
// 根据菜品名查找菜品
MenuItem* findMenuItem(string name) {
MenuItem* current = head;
while (current != NULL) {
if (current->name == name) {
return current;
}
current = current->next;
}
return NULL;
}
private:
MenuItem* head;
int size;
};
```
接下来,我们需要定义一个订单类(Order),用于存储所有的订单信息。订单包含订单号、时间、菜品列表等信息。同样,我们可以使用链表来存储所有的订单信息。
```c++
#include <iostream>
#include <string>
#include <ctime>
using namespace std;
// 定义订单结构体
struct OrderItem {
string name;
double price;
OrderItem* next;
};
struct Order {
int id;
time_t time;
OrderItem* itemList;
Order* next;
};
class OrderList {
public:
OrderList() {
head = NULL;
size = 0;
}
// 添加订单
void addOrder(Order* order) {
order->id = size + 1;
order->time = time(0);
order->next = head;
head = order;
size++;
}
// 展示订单
void showOrder(int id) {
Order* current = head;
while (current != NULL) {
if (current->id == id) {
cout << "订单号:" << current->id << endl;
cout << "时间:" << ctime(¤t->time);
OrderItem* item = current->itemList;
double totalPrice = 0;
while (item != NULL) {
cout << item->name << " - " << item->price << endl;
totalPrice += item->price;
item = item->next;
}
cout << "总价:" << totalPrice << endl;
return;
}
current = current->next;
}
cout << "没有找到该订单!" << endl;
}
// 删除订单
void deleteOrder(int id) {
if (head == NULL) {
cout << "订单列表为空,无法删除订单!" << endl;
return;
}
if (head->id == id) {
Order* temp = head;
head = head->next;
delete temp;
size--;
cout << "订单删除成功!" << endl;
return;
}
Order* current = head;
while (current->next != NULL) {
if (current->next->id == id) {
Order* temp = current->next;
current->next = temp->next;
delete temp;
size--;
cout << "订单删除成功!" << endl;
return;
}
current = current->next;
}
cout << "没有找到该订单!" << endl;
}
// 导出订单到文件
void exportOrderToFile(string fileName) {
ofstream outFile;
outFile.open(fileName);
Order* current = head;
while (current != NULL) {
outFile << current->id << "," << ctime(¤t->time);
OrderItem* item = current->itemList;
while (item != NULL) {
outFile << item->name << "," << item->price << ",";
item = item->next;
}
outFile << endl;
current = current->next;
}
outFile.close();
}
// 从文件导入订单
void importOrderFromFile(string fileName, Menu* menu) {
ifstream inFile;
inFile.open(fileName);
string line;
while (getline(inFile, line)) {
Order* order = new Order;
int pos1 = line.find(",");
order->id = stoi(line.substr(0, pos1));
int pos2 = line.find(",", pos1 + 1);
order->time = stol(line.substr(pos1 + 1, pos2 - pos1 - 1));
OrderItem* itemList = NULL;
int pos = pos2 + 1;
while (pos < line.length()) {
pos1 = line.find(",", pos);
string name = line.substr(pos, pos1 - pos);
pos2 = line.find(",", pos1 + 1);
double price = stod(line.substr(pos1 + 1, pos2 - pos1 - 1));
MenuItem* menuItem = menu->findMenuItem(name);
if (menuItem == NULL) {
cout << "菜单中没有该菜品:" << name << endl;
} else {
OrderItem* item = new OrderItem;
item->name = name;
item->price = menuItem->price;
item->next = itemList;
itemList = item;
}
pos = pos2 + 1;
}
order->itemList = itemList;
addOrder(order);
}
inFile.close();
}
// 点餐
void order(Menu* menu) {
Order* order = new Order;
order->itemList = NULL;
string name;
while (true) {
cout << "请输入您要点的菜品名(输入 exit 结束点餐):" << endl;
cin >> name;
if (name == "exit") {
break;
}
MenuItem* menuItem = menu->findMenuItem(name);
if (menuItem == NULL) {
cout << "菜单中没有该菜品:" << name << endl;
} else {
OrderItem* item = new OrderItem;
item->name = name;
item->price = menuItem->price;
item->next = order->itemList;
order->itemList = item;
}
}
if (order->itemList == NULL) {
cout << "您没有选择任何菜品!" << endl;
delete order;
} else {
addOrder(order);
cout << "订单添加成功!" << endl;
}
}
private:
Order* head;
int size;
};
```
最后,我们需要一个主函数来调用上面的类和函数,实现点餐系统的整体功能。
```c++
int main() {
Menu menu;
menu.importMenuFromFile("menu.txt");
menu.showMenu();
OrderList orderList;
orderList.importOrderFromFile("orders.txt", &menu);
while (true) {
cout << "请输入选项编号:" << endl;
cout << "1. 点餐" << endl;
cout << "2. 查看订单" << endl;
cout << "3. 删除订单" << endl;
cout << "4. 导出订单到文件" << endl;
cout << "5. 退出系统" << endl;
int option;
cin >> option;
switch (option) {
case 1:
orderList.order(&menu);
break;
case 2:
int id;
cout << "请输入要查看的订单号:" << endl;
cin >> id;
orderList.showOrder(id);
break;
case 3:
cout << "请输入要删除的订单号:" << endl;
cin >> id;
orderList.deleteOrder(id);
break;
case 4:
orderList.exportOrderToFile("orders.txt");
break;
case 5:
menu.exportMenuToFile("menu.txt");
cout << "谢谢使用!" << endl;
return 0;
default:
cout << "无效的选项,请重新输入!" << endl;
break;
}
}
}
```
以上是一个简单的 C++ 链表点餐系统实现思路和代码示例,您可以根据您的需求进行修改和完善。
阅读全文