Write a program to manage inventory of goods. The specific functions are: (1) Enter the quantity of products and the name, number, and inventory quantity of each product through the keyboard. (2) Save the inventory quantity of the product to a text file. (3) Read the document and print product information in descending order of inventory for the product.use c++
时间: 2024-03-06 21:50:52 浏览: 70
Sure, here's an example C++ program that implements the requirements you've specified:
```c++
#include <iostream>
#include <fstream>
#include <string>
#include <vector>
#include <algorithm>
using namespace std;
struct Product {
string name;
int number;
int quantity;
};
vector<Product> inventory;
void addProduct() {
Product p;
cout << "Enter product name: ";
cin >> p.name;
cout << "Enter product number: ";
cin >> p.number;
cout << "Enter product quantity: ";
cin >> p.quantity;
inventory.push_back(p);
}
void saveInventory() {
ofstream fout("inventory.txt");
if (fout) {
for (const auto& p : inventory) {
fout << p.name << "," << p.number << "," << p.quantity << endl;
}
fout.close();
cout << "Inventory saved to file." << endl;
} else {
cout << "Error: could not open file." << endl;
}
}
void readInventory() {
ifstream fin("inventory.txt");
if (fin) {
inventory.clear();
string line;
while (getline(fin, line)) {
Product p;
size_t pos = line.find(",");
p.name = line.substr(0, pos);
line = line.substr(pos + 1);
pos = line.find(",");
p.number = stoi(line.substr(0, pos));
p.quantity = stoi(line.substr(pos + 1));
inventory.push_back(p);
}
fin.close();
cout << "Inventory loaded from file." << endl;
} else {
cout << "Error: could not open file." << endl;
}
}
void printInventory() {
sort(inventory.begin(), inventory.end(), [](const Product& p1, const Product& p2) {
return p1.quantity > p2.quantity;
});
cout << "Inventory:" << endl;
for (const auto& p : inventory) {
cout << p.name << " (" << p.number << "): " << p.quantity << endl;
}
}
int main() {
char choice;
do {
cout << "Select an option:" << endl;
cout << "1. Add product" << endl;
cout << "2. Save inventory to file" << endl;
cout << "3. Load inventory from file" << endl;
cout << "4. Print inventory" << endl;
cout << "5. Exit" << endl;
cout << "> ";
cin >> choice;
switch (choice) {
case '1':
addProduct();
break;
case '2':
saveInventory();
break;
case '3':
readInventory();
break;
case '4':
printInventory();
break;
case '5':
cout << "Exiting program." << endl;
break;
default:
cout << "Invalid choice." << endl;
break;
}
} while (choice != '5');
return 0;
}
```
This program defines a `Product` struct to hold the name, number, and quantity of each product. It uses a `vector` to store the inventory of products. The program provides a menu with options to add a product, save the inventory to a file, load the inventory from a file, print the inventory, and exit the program. The program uses `fstream` to read from and write to the `inventory.txt` file. The `printInventory` function sorts the products in descending order of their inventory quantity before printing them to the console.
阅读全文