超市会员积分管理系统用C语言怎么写
时间: 2023-07-08 22:13:15 浏览: 100
以下是一个简单的超市会员积分管理系统C语言示例:
```
#include <stdio.h>
#include <stdlib.h>
// 定义会员结构体
struct Member {
int id;
char name[20];
int points;
};
// 显示菜单
void showMenu() {
printf("1. 查询会员积分\n");
printf("2. 增加会员积分\n");
printf("3. 减少会员积分\n");
printf("4. 退出\n");
}
// 查询会员积分
void queryPoints(struct Member members[], int count) {
int id;
printf("请输入会员ID:");
scanf("%d", &id);
for (int i = 0; i < count; i++) {
if (members[i].id == id) {
printf("会员姓名:%s,积分:%d\n", members[i].name, members[i].points);
return;
}
}
printf("未找到该会员!\n");
}
// 增加会员积分
void addPoints(struct Member members[], int count) {
int id, points;
printf("请输入会员ID:");
scanf("%d", &id);
for (int i = 0; i < count; i++) {
if (members[i].id == id) {
printf("请输入增加的积分数:");
scanf("%d", &points);
members[i].points += points;
printf("会员姓名:%s,积分:%d\n", members[i].name, members[i].points);
return;
}
}
printf("未找到该会员!\n");
}
// 减少会员积分
void reducePoints(struct Member members[], int count) {
int id, points;
printf("请输入会员ID:");
scanf("%d", &id);
for (int i = 0; i < count; i++) {
if (members[i].id == id) {
printf("请输入减少的积分数:");
scanf("%d", &points);
if (members[i].points < points) {
printf("积分不足,减少失败!\n");
return;
}
members[i].points -= points;
printf("会员姓名:%s,积分:%d\n", members[i].name, members[i].points);
return;
}
}
printf("未找到该会员!\n");
}
int main() {
int count = 3; // 会员数量
struct Member members[3] = {
{1001, "张三", 100},
{1002, "李四", 200},
{1003, "王五", 300}
};
int choice;
while (1) {
showMenu();
printf("请输入您的选择:");
scanf("%d", &choice);
switch (choice) {
case 1:
queryPoints(members, count);
break;
case 2:
addPoints(members, count);
break;
case 3:
reducePoints(members, count);
break;
case 4:
exit(0);
default:
printf("输入有误,请重新输入!\n");
}
}
return 0;
}
```
以上代码实现了一个简单的超市会员积分管理系统,包括查询会员积分、增加会员积分、减少会员积分等功能。你可以根据需求进行修改和完善。
阅读全文