使用c++以及链表的结构写一个点餐系统,尽可能详细功能丰富

时间: 2024-03-24 12:38:46 浏览: 16
好的,我可以为您提供一些 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(&current->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(&current->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++ 链表点餐系统实现思路和代码示例,您可以根据您的需求进行修改和完善。

相关推荐

最新推荐

recommend-type

C++双向链表实现简单通讯录

主要为大家详细介绍了C++双向链表实现简单通讯录,文中示例代码介绍的非常详细,具有一定的参考价值,感兴趣的小伙伴们可以参考一下
recommend-type

数据结构实验报告之一元多项式求和(链表)报告2.doc

把任意给定的两个一元多项式P(x) ,Q(x) 输入计算机,计算它们的和并输出计算结果。 实验内容: 1.问题描述: 一元多项式求和——把任意给定的两个一元多项式P(x) ,Q(x) 输入计算机,计算它们的和并输出计算结果。
recommend-type

C语言:一元多项式加减法运算(链表 附答案).docx

C语言链表的入门题,里面提供了两种思路供参考,用链表来实现一元多项式的加减法,并按照一定规律输出。也是练习链表和排序算法的一道小实验,初学链表的小伙伴可以参考参考噢
recommend-type

C语言数据结构实现链表逆序并输出

主要介绍了C语言数据结构实现链表逆序并输出的相关资料,需要的朋友可以参考下
recommend-type

C++ 数据结构二叉树(前序/中序/后序递归、非递归遍历)

主要介绍了C++ 数据结构二叉树(前序/中序/后序递归、非递归遍历)的相关资料,这里提供实例代码来帮助大家理解掌握二叉树,需要的朋友可以参考下
recommend-type

zigbee-cluster-library-specification

最新的zigbee-cluster-library-specification说明文档。
recommend-type

管理建模和仿真的文件

管理Boualem Benatallah引用此版本:布阿利姆·贝纳塔拉。管理建模和仿真。约瑟夫-傅立叶大学-格勒诺布尔第一大学,1996年。法语。NNT:电话:00345357HAL ID:电话:00345357https://theses.hal.science/tel-003453572008年12月9日提交HAL是一个多学科的开放存取档案馆,用于存放和传播科学研究论文,无论它们是否被公开。论文可以来自法国或国外的教学和研究机构,也可以来自公共或私人研究中心。L’archive ouverte pluridisciplinaire
recommend-type

MATLAB柱状图在信号处理中的应用:可视化信号特征和频谱分析

![matlab画柱状图](https://img-blog.csdnimg.cn/3f32348f1c9c4481a6f5931993732f97.png) # 1. MATLAB柱状图概述** MATLAB柱状图是一种图形化工具,用于可视化数据中不同类别或组的分布情况。它通过绘制垂直条形来表示每个类别或组中的数据值。柱状图在信号处理中广泛用于可视化信号特征和进行频谱分析。 柱状图的优点在于其简单易懂,能够直观地展示数据分布。在信号处理中,柱状图可以帮助工程师识别信号中的模式、趋势和异常情况,从而为信号分析和处理提供有价值的见解。 # 2. 柱状图在信号处理中的应用 柱状图在信号处理
recommend-type

用Spring boot和vue写一个登录注册界面

好的,这是一个比较常见的技术问题,我可以为您提供一些参考资料和思路。首先,您可以使用Spring Boot作为后端框架来处理用户认证和注册的逻辑,比如使用Spring Security实现用户登录认证。同时,您还需要设计相应的数据模型和数据库表结构来存储用户信息。在前端方面,您可以使用Vue.js作为框架来构建登录注册页面,使用Axios来发起API请求并和后端进行交互。当然,在实现过程中,还需要考虑一些具体细节,比如数据校验、安全性和用户体验等方面。希望这些信息能够帮助到您。
recommend-type

JSBSim Reference Manual

JSBSim参考手册,其中包含JSBSim简介,JSBSim配置文件xml的编写语法,编程手册以及一些应用实例等。其中有部分内容还没有写完,估计有生之年很难看到完整版了,但是内容还是很有参考价值的。