用c++编写一个程序实现: 订单信息包括:订单编号、公司名称、产品名称、产品定价、订购数量、订单金额等。功能要求: 1)添加:输入各信息并计算订单金额2)浏览 3)查询:按某个条件查询 7 4)删除:删除某份不需要的订单 5)统计:输入某个公司名称,统计并输出所有订单总额6)对所有订单信息读写文件
时间: 2024-03-26 07:35:52 浏览: 49
好的,以下是一个简单的 C++ 程序,实现了您提出的订单信息管理系统的基本功能要求,包括添加、浏览、查询、删除、统计和读写文件等。程序中使用了结构体来存储订单信息,使用文件来保存订单信息。
```c++
#include <iostream>
#include <fstream>
#include <string>
#include <vector>
using namespace std;
// 订单信息结构体
struct Order {
string orderNumber; // 订单编号
string companyName; // 公司名称
string productName; // 产品名称
double productPrice; // 产品定价
int quantity; // 订购数量
double amount; // 订单金额
};
// 全局变量,存储所有订单信息
vector<Order> orders;
// 添加订单
void addOrder() {
Order order;
cout << "请输入订单编号:";
cin >> order.orderNumber;
cout << "请输入公司名称:";
cin >> order.companyName;
cout << "请输入产品名称:";
cin >> order.productName;
cout << "请输入产品定价:";
cin >> order.productPrice;
cout << "请输入订购数量:";
cin >> order.quantity;
order.amount = order.productPrice * order.quantity;
orders.push_back(order);
cout << "订单添加成功!" << endl;
}
// 浏览订单
void browseOrders() {
if (orders.empty()) {
cout << "订单列表为空!" << endl;
} else {
cout << "订单列表:" << endl;
for (int i = 0; i < orders.size(); i++) {
cout << "订单编号:" << orders[i].orderNumber << endl;
cout << "公司名称:" << orders[i].companyName << endl;
cout << "产品名称:" << orders[i].productName << endl;
cout << "产品定价:" << orders[i].productPrice << endl;
cout << "订购数量:" << orders[i].quantity << endl;
cout << "订单金额:" << orders[i].amount << endl;
cout << "========================" << endl;
}
}
}
// 按条件查询订单
void searchOrders() {
cout << "请输入查询条件(订单编号/公司名称/产品名称):";
string keyword;
cin >> keyword;
bool found = false;
for (int i = 0; i < orders.size(); i++) {
if (orders[i].orderNumber == keyword ||
orders[i].companyName == keyword ||
orders[i].productName == keyword) {
cout << "订单编号:" << orders[i].orderNumber << endl;
cout << "公司名称:" << orders[i].companyName << endl;
cout << "产品名称:" << orders[i].productName << endl;
cout << "产品定价:" << orders[i].productPrice << endl;
cout << "订购数量:" << orders[i].quantity << endl;
cout << "订单金额:" << orders[i].amount << endl;
cout << "========================" << endl;
found = true;
}
}
if (!found) {
cout << "未找到符合条件的订单!" << endl;
}
}
// 删除订单
void deleteOrder() {
cout << "请输入要删除的订单编号:";
string orderNumber;
cin >> orderNumber;
bool found = false;
for (vector<Order>::iterator it = orders.begin(); it != orders.end(); ) {
if (it->orderNumber == orderNumber) {
it = orders.erase(it);
cout << "订单已删除!" << endl;
found = true;
} else {
it++;
}
}
if (!found) {
cout << "未找到指定的订单!" << endl;
}
}
// 统计订单总额
void sumOrderAmount() {
cout << "请输入要统计的公司名称:";
string companyName;
cin >> companyName;
double sum = 0;
for (int i = 0; i < orders.size(); i++) {
if (orders[i].companyName == companyName) {
sum += orders[i].amount;
}
}
cout << "总金额为:" << sum << endl;
}
// 保存订单信息到文件
void saveToFile() {
ofstream ofs("orders.txt", ios::out);
if (ofs.is_open()) {
for (int i = 0; i < orders.size(); i++) {
ofs << orders[i].orderNumber << " ";
ofs << orders[i].companyName << " ";
ofs << orders[i].productName << " ";
ofs << orders[i].productPrice << " ";
ofs << orders[i].quantity << " ";
ofs << orders[i].amount << endl;
}
cout << "订单信息已保存到文件!" << endl;
} else {
cout << "无法打开文件!" << endl;
}
ofs.close();
}
// 从文件中读取订单信息
void readFromFile() {
ifstream ifs("orders.txt", ios::in);
if (ifs.is_open()) {
orders.clear();
Order order;
while (ifs >> order.orderNumber >> order.companyName >> order.productName
>> order.productPrice >> order.quantity >> order.amount) {
orders.push_back(order);
}
cout << "订单信息已从文件中读取!" << endl;
} else {
cout << "无法打开文件!" << endl;
}
ifs.close();
}
int main() {
int choice;
do {
cout << "========================" << endl;
cout << "1. 添加订单" << endl;
cout << "2. 浏览订单" << endl;
cout << "3. 查询订单" << endl;
cout << "4. 删除订单" << endl;
cout << "5. 统计订单总额" << endl;
cout << "6. 保存订单信息到文件" << endl;
cout << "7. 从文件中读取订单信息" << endl;
cout << "0. 退出程序" << endl;
cout << "========================" << endl;
cout << "请选择操作:";
cin >> choice;
switch (choice) {
case 1: addOrder(); break;
case 2: browseOrders(); break;
case 3: searchOrders(); break;
case 4: deleteOrder(); break;
case 5: sumOrderAmount(); break;
case 6: saveToFile(); break;
case 7: readFromFile(); break;
case 0: break;
default: cout << "无效的选择!" << endl;
}
} while (choice != 0);
return 0;
}
```
注意:为了简化程序,此处没有进行输入验证和异常处理等操作,实际使用时需要根据具体情况进行完善。
阅读全文