用C++语言写一个超市会员卡管理系统
时间: 2023-12-24 07:04:32 浏览: 36
下面是一个简单的超市会员卡管理系统的C++代码实现:
```c++
#include <iostream>
#include <string>
#include <vector>
using namespace std;
// 定义会员卡结构体
struct MemberCard {
string name; // 姓名
string phone; // 手机号
int points; // 积分
};
// 定义超市会员卡管理系统类
class Supermarket {
private:
vector<MemberCard> cards; // 存储会员卡信息的vector容器
public:
// 添加会员卡
void addCard(MemberCard card) {
cards.push_back(card);
cout << "添加成功!" << endl;
}
// 删除会员卡
void deleteCard(string phone) {
for (vector<MemberCard>::iterator it = cards.begin(); it != cards.end(); it++) {
if (it->phone == phone) {
cards.erase(it);
cout << "删除成功!" << endl;
return;
}
}
cout << "该会员卡不存在!" << endl;
}
// 查询会员卡
void queryCard(string phone) {
for (vector<MemberCard>::iterator it = cards.begin(); it != cards.end(); it++) {
if (it->phone == phone) {
cout << "姓名:" << it->name << endl;
cout << "手机号:" << it->phone << endl;
cout << "积分:" << it->points << endl;
return;
}
}
cout << "该会员卡不存在!" << endl;
}
// 修改会员卡积分
void modifyPoints(string phone, int points) {
for (vector<MemberCard>::iterator it = cards.begin(); it != cards.end(); it++) {
if (it->phone == phone) {
it->points = points;
cout << "修改成功!" << endl;
return;
}
}
cout << "该会员卡不存在!" << endl;
}
};
int main() {
Supermarket supermarket;
int choice;
while (true) {
cout << "请选择操作:1.添加会员卡 2.删除会员卡 3.查询会员卡 4.修改会员卡积分 5.退出" << endl;
cin >> choice;
if (choice == 1) {
MemberCard card;
cout << "请输入姓名、手机号、积分,用空格隔开:" << endl;
cin >> card.name >> card.phone >> card.points;
supermarket.addCard(card);
} else if (choice == 2) {
string phone;
cout << "请输入要删除的会员卡手机号:" << endl;
cin >> phone;
supermarket.deleteCard(phone);
} else if (choice == 3) {
string phone;
cout << "请输入要查询的会员卡手机号:" << endl;
cin >> phone;
supermarket.queryCard(phone);
} else if (choice == 4) {
string phone;
int points;
cout << "请输入要修改的会员卡手机号和新的积分,用空格隔开:" << endl;
cin >> phone >> points;
supermarket.modifyPoints(phone, points);
} else if (choice == 5) {
break;
} else {
cout << "输入有误,请重新输入!" << endl;
}
}
return 0;
}
```
该程序通过定义MemberCard结构体和Supermarket类来管理会员卡信息,其中Supermarket类包含了添加会员卡、删除会员卡、查询会员卡和修改会员卡积分等操作。在主函数中使用循环进行操作选择,直到用户选择退出。
相关推荐
![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)