vector<vector<int> > warehouse(N + 1, vector<int>(M));
时间: 2024-06-15 19:03:21 浏览: 10
vector<vector<int>> warehouse是一个二维向量,它的类型是vector<int>,表示每一行的数据。warehouse的大小是N+1行,每行有M个元素。每个元素的类型是int,表示仓库中的某个物品的数量。
这样定义的warehouse可以用来表示一个N行M列的仓库,每个位置上存储着对应物品的数量。你可以通过warehouse[i][j]来访问第i行第j列的物品数量。
相关问题
#ifndef WAREHOUSE_H #define WAREHOUSE_H #include <vector> #include <iostream> // 添加iostream头文件 #include <algorithm> // 添加algorithm头文件 struct Item { int id; // 商品编号 std::string name; // 商品名称 int quantity; // 库存数量 double price; // 单价 }; class Warehouse { public: void add(); // 增加物品 void remove(); // 删除物品 void query(); // 查询物品信息 void modify(); // 修改物品信息 void display(); // 显示物品信息 void sort(); // 排序物品信息 void save(); // 保存并退出 private: std::vector<Item> items; // 商品列表 std::vector<Item>::iterator findItem(int id); // 查找商品 }; #endif缺少.cpp文件
好的,下面是一个简单的仓库管理的实现,包括.h和.cpp文件:
warehouse.h
```c++
#ifndef WAREHOUSE_H
#define WAREHOUSE_H
#include <vector>
#include <iostream>
#include <algorithm>
struct Item {
int id; // 商品编号
std::string name; // 商品名称
int quantity; // 库存数量
double price; // 单价
};
class Warehouse {
public:
void add(); // 增加物品
void remove(); // 删除物品
void query(); // 查询物品信息
void modify(); // 修改物品信息
void display(); // 显示物品信息
void sort(); // 排序物品信息
void save(); // 保存并退出
private:
std::vector<Item> items; // 商品列表
std::
用c++写一段关于仓库管理系统的代码,要求:仓库中货物信息包括:货物编号、货物名称、货物单价、货物数量等。 功能要求: 1) 增加和删除货物信息 2) 按编号或名称查询货物信息 3) 显示库存量不足一定值(由用户输入)的货物信息 4) 显示所有货物的信息 5) 对所有货物信息读写文
好的,以下是一份 C++ 代码,实现了基本的仓库管理系统功能,可以满足您的需求。注意,该代码只是一个简单的示例,并未进行完整的测试和优化,请谨慎使用。
```cpp
#include <iostream>
#include <fstream>
#include <vector>
#include <string>
using namespace std;
// 声明货物类
class Goods {
public:
Goods(string id, string name, double price, int quantity) {
m_id = id;
m_name = name;
m_price = price;
m_quantity = quantity;
}
string get_id() {
return m_id;
}
string get_name() {
return m_name;
}
double get_price() {
return m_price;
}
int get_quantity() {
return m_quantity;
}
void set_id(string id) {
m_id = id;
}
void set_name(string name) {
m_name = name;
}
void set_price(double price) {
m_price = price;
}
void set_quantity(int quantity) {
m_quantity = quantity;
}
private:
string m_id;
string m_name;
double m_price;
int m_quantity;
};
// 声明仓库类
class Warehouse {
public:
Warehouse() {
// 从文件中读取货物信息
ifstream infile("goods.txt");
string id, name;
double price;
int quantity;
while (infile >> id >> name >> price >> quantity) {
Goods goods(id, name, price, quantity);
m_goods.push_back(goods);
}
infile.close();
}
// 添加货物
void add_goods(Goods goods) {
m_goods.push_back(goods);
}
// 删除货物
void delete_goods(string id) {
for (vector<Goods>::iterator it = m_goods.begin(); it != m_goods.end(); it++) {
if (it->get_id() == id) {
m_goods.erase(it);
break;
}
}
}
// 按编号查询货物
Goods* search_goods_by_id(string id) {
for (vector<Goods>::iterator it = m_goods.begin(); it != m_goods.end(); it++) {
if (it->get_id() == id) {
return &(*it);
}
}
return NULL;
}
// 按名称查询货物
vector<Goods*> search_goods_by_name(string name) {
vector<Goods*> result;
for (vector<Goods>::iterator it = m_goods.begin(); it != m_goods.end(); it++) {
if (it->get_name() == name) {
result.push_back(&(*it));
}
}
return result;
}
// 显示库存量不足一定值的货物信息
vector<Goods*> show_low_quantity_goods(int quantity) {
vector<Goods*> result;
for (vector<Goods>::iterator it = m_goods.begin(); it != m_goods.end(); it++) {
if (it->get_quantity() < quantity) {
result.push_back(&(*it));
}
}
return result;
}
// 显示所有货物信息
void show_all_goods() {
for (vector<Goods>::iterator it = m_goods.begin(); it != m_goods.end(); it++) {
cout << it->get_id() << " " << it->get_name() << " " << it->get_price() << " " << it->get_quantity() << endl;
}
}
// 保存货物信息到文件
void save_to_file() {
ofstream outfile("goods.txt");
for (vector<Goods>::iterator it = m_goods.begin(); it != m_goods.end(); it++) {
outfile << it->get_id() << " " << it->get_name() << " " << it->get_price() << " " << it->get_quantity() << endl;
}
outfile.close();
}
private:
vector<Goods> m_goods;
};
// 主函数
int main() {
Warehouse warehouse;
int choice;
while (true) {
cout << "1.添加货物 2.删除货物 3.按编号查询货物 4.按名称查询货物 5.显示库存量不足的货物 6.显示所有货物 7.保存货物信息到文件 8.退出" << endl;
cout << "请选择操作:";
cin >> choice;
switch (choice) {
case 1: {
string id, name;
double price;
int quantity;
cout << "请输入货物编号:";
cin >> id;
cout << "请输入货物名称:";
cin >> name;
cout << "请输入货物单价:";
cin >> price;
cout << "请输入货物数量:";
cin >> quantity;
Goods goods(id, name, price, quantity);
warehouse.add_goods(goods);
cout << "货物添加成功!" << endl;
break;
}
case 2: {
string id;
cout << "请输入货物编号:";
cin >> id;
warehouse.delete_goods(id);
cout << "货物删除成功!" << endl;
break;
}
case 3: {
string id;
cout << "请输入货物编号:";
cin >> id;
Goods* goods = warehouse.search_goods_by_id(id);
if (goods == NULL) {
cout << "找不到指定编号的货物!" << endl;
}
else {
cout << goods->get_id() << " " << goods->get_name() << " " << goods->get_price() << " " << goods->get_quantity() << endl;
}
break;
}
case 4: {
string name;
cout << "请输入货物名称:";
cin >> name;
vector<Goods*> result = warehouse.search_goods_by_name(name);
if (result.size() == 0) {
cout << "找不到指定名称的货物!" << endl;
}
else {
for (vector<Goods*>::iterator it = result.begin(); it != result.end(); it++) {
cout << (*it)->get_id() << " " << (*it)->get_name() << " " << (*it)->get_price() << " " << (*it)->get_quantity() << endl;
}
}
break;
}
case 5: {
int quantity;
cout << "请输入库存量不足的货物数量:";
cin >> quantity;
vector<Goods*> result = warehouse.show_low_quantity_goods(quantity);
if (result.size() == 0) {
cout << "没有库存量不足的货物!" << endl;
}
else {
for (vector<Goods*>::iterator it = result.begin(); it != result.end(); it++) {
cout << (*it)->get_id() << " " << (*it)->get_name() << " " << (*it)->get_price() << " " << (*it)->get_quantity() << endl;
}
}
break;
}
case 6: {
warehouse.show_all_goods();
break;
}
case 7: {
warehouse.save_to_file();
cout << "货物信息保存成功!" << endl;
break;
}
case 8: {
return 0;
}
default: {
cout << "无效的操作!" << endl;
break;
}
}
}
return 0;
}
```
该代码实现了仓库管理系统的基本功能,包括添加货物、删除货物、按编号或名称查询货物、显示库存量不足一定值的货物信息、显示所有货物信息、对所有货物信息进行读写等操作。
相关推荐
![pdf](https://img-home.csdnimg.cn/images/20210720083512.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![zip](https://img-home.csdnimg.cn/images/20210720083736.png)
![rar](https://img-home.csdnimg.cn/images/20210720083606.png)