写一个c++程序,为超市会员管理系统,要求是能在代码运行界面输入会员和货物信息,根据输入的信息进行对货物流量和消费人群进行统计输出
时间: 2023-06-18 17:07:53 浏览: 64
以下是一个简单的超市会员管理系统示例代码,可以根据需求进行修改:
```c++
#include <iostream>
#include <string>
#include <map>
using namespace std;
// 定义会员结构体
struct Member {
string name;
int age;
int id;
};
// 定义货物结构体
struct Goods {
string name;
float price;
int quantity;
};
// 定义超市管理类
class Supermarket {
public:
Supermarket() {
// 初始化会员和货物信息
members[1] = {"Tom", 30, 1};
members[2] = {"Jerry", 25, 2};
members[3] = {"Alice", 28, 3};
goods[1] = {"Apple", 3.0, 100};
goods[2] = {"Banana", 2.0, 200};
goods[3] = {"Orange", 4.0, 150};
}
// 添加会员
void addMember(Member member) {
members[member.id] = member;
}
// 添加货物
void addGoods(Goods good) {
goods[goods.size() + 1] = good;
}
// 统计货物流量和消费人群
void statistics() {
int totalQuantity = 0;
map<int, int> ageCount;
map<string, int> nameCount;
for (auto& item : goods) {
totalQuantity += item.second.quantity;
}
for (auto& item : members) {
ageCount[item.second.age]++;
nameCount[item.second.name]++;
}
cout << "货物总数量:" << totalQuantity << endl;
cout << "消费人群年龄分布:" << endl;
for (auto& item : ageCount) {
cout << item.first << "岁:" << item.second << "人" << endl;
}
cout << "消费人群姓名分布:" << endl;
for (auto& item : nameCount) {
cout << item.first << ":" << item.second << "人" << endl;
}
}
private:
map<int, Member> members; // 会员信息
map<int, Goods> goods; // 货物信息
};
int main() {
Supermarket sm;
// 添加会员
sm.addMember({"Lily", 26, 4});
sm.addMember({"Bob", 32, 5});
// 添加货物
sm.addGoods({"Grape", 5.0, 120});
sm.addGoods({"Watermelon", 10.0, 80});
// 统计货物流量和消费人群
sm.statistics();
return 0;
}
```
在代码运行界面中,可以按照以下格式输入会员和货物信息:
添加会员:姓名 年龄 编号
添加货物:名称 单价 数量
例如:
```
添加会员:Lucy 29 6
添加货物:Pear 2.5 150
```
程序会自动将输入的会员和货物信息添加到管理系统中,并输出统计结果。
相关推荐
![zip](https://img-home.csdnimg.cn/images/20210720083736.png)
![application/x-rar](https://img-home.csdnimg.cn/images/20210720083606.png)
![rar](https://img-home.csdnimg.cn/images/20210720083606.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)
![](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)