帮我用c语言写一个手机用户管理的程序。主要包括开通业务,销号业务,通话记录,充值业务,计费业务,查询业务。假定话费0.1元/分钟,流量10MB/元,短信0.1元/条。菜单界面清晰,系统界面改换
时间: 2024-04-22 15:21:59 浏览: 21
好的,以下是一个简单的手机用户管理的程序,实现了您所提到的功能。请注意,这只是一个示例程序,可能需要根据您的具体需求进行修改和优化。
```c
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#define MAX_USERS 100
#define MAX_NAME_LEN 20
#define MAX_PHONE_LEN 11
// 用户结构体
typedef struct {
char name[MAX_NAME_LEN];
char phone[MAX_PHONE_LEN];
int balance; // 余额(单位:分)
int data_usage; // 流量使用量(单位:MB)
int message_count; // 短信数量
int call_duration; // 通话时长(单位:秒)
} User;
int user_count = 0; // 用户数量
User users[MAX_USERS]; // 用户数组
// 显示菜单
void show_menu() {
printf("\n");
printf("手机用户管理系统\n");
printf("1. 开通业务\n");
printf("2. 销号业务\n");
printf("3. 通话记录\n");
printf("4. 充值业务\n");
printf("5. 计费业务\n");
printf("6. 查询业务\n");
printf("0. 退出系统\n");
}
// 输入字符串,并去除末尾的换行符
void get_string(char *str, int max_len) {
fgets(str, max_len, stdin);
str[strcspn(str, "\n")] = 0;
}
// 查找用户
int find_user(char *phone) {
for (int i = 0; i < user_count; i++) {
if (strcmp(users[i].phone, phone) == 0) {
return i;
}
}
return -1;
}
// 开通业务
void open_service() {
printf("请输入用户名:");
char name[MAX_NAME_LEN];
get_string(name, MAX_NAME_LEN);
printf("请输入手机号:");
char phone[MAX_PHONE_LEN];
get_string(phone, MAX_PHONE_LEN);
if (find_user(phone) != -1) {
printf("该手机号已被注册!\n");
return;
}
if (user_count >= MAX_USERS) {
printf("用户数量已达上限,无法继续开通业务!\n");
return;
}
strcpy(users[user_count].name, name);
strcpy(users[user_count].phone, phone);
users[user_count].balance = 0;
users[user_count].data_usage = 0;
users[user_count].message_count = 0;
users[user_count].call_duration = 0;
user_count++;
printf("开通业务成功!\n");
}
// 销号业务
void close_service() {
printf("请输入手机号:");
char phone[MAX_PHONE_LEN];
get_string(phone, MAX_PHONE_LEN);
int user_index = find_user(phone);
if (user_index == -1) {
printf("该手机号未注册!\n");
return;
}
for (int i = user_index; i < user_count - 1; i++) {
users[i] = users[i + 1];
}
user_count--;
printf("销号业务成功!\n");
}
// 通话记录
void show_call_records() {
printf("请输入手机号:");
char phone[MAX_PHONE_LEN];
get_string(phone, MAX_PHONE_LEN);
int user_index = find_user(phone);
if (user_index == -1) {
printf("该手机号未注册!\n");
return;
}
printf("通话记录:\n");
printf("通话时长:%d秒\n", users[user_index].call_duration);
}
// 充值业务
void recharge() {
printf("请输入手机号:");
char phone[MAX_PHONE_LEN];
get_string(phone, MAX_PHONE_LEN);
int user_index = find_user(phone);
if (user_index == -1) {
printf("该手机号未注册!\n");
return;
}
printf("请输入充值金额(单位:元):");
float amount;
scanf("%f", &amount);
getchar(); // 读取多余的换行符
int balance_change = (int)(amount * 100); // 转换为分
users[user_index].balance += balance_change;
printf("充值成功!当前余额为:%d元\n", users[user_index].balance / 100);
}
// 计费业务
void billing() {
printf("请输入手机号:");
char phone[MAX_PHONE_LEN];
get_string(phone, MAX_PHONE_LEN);
int user_index = find_user(phone);
if (user_index == -1) {
printf("该手机号未注册!\n");
return;
}
int call_duration = users[user_index].call_duration;
int data_usage = users[user_index].data_usage;
int message_count = users[user_index].message_count;
int call_fee = call_duration * 10; // 话费:0.1元/分钟
int data_fee = data_usage / 10; // 流量费:10MB/元
int message_fee = message_count; // 短信费:0.1元/条
int total_fee = call_fee + data_fee + message_fee;
if (total_fee > users[user_index].balance) {
printf("余额不足,无法完成计费!\n");
return;
}
users[user_index].balance -= total_fee;
users[user_index].call_duration = 0;
users[user_index].data_usage = 0;
users[user_index].message_count = 0;
printf("计费成功!话费:%d元,流量费:%d元,短信费:%d元,总费用:%d元,当前余额:%d元\n", call_fee / 100, data_fee, message_fee / 100, total_fee / 100, users[user_index].balance / 100);
}
// 查询业务
void show_info() {
printf("请输入手机号:");
char phone[MAX_PHONE_LEN];
get_string(phone, MAX_PHONE_LEN);
int user_index = find_user(phone);
if (user_index == -1) {
printf("该手机号未注册!\n");
return;
}
printf("用户名:%s\n", users[user_index].name);
printf("手机号:%s\n", users[user_index].phone);
printf("余额:%d元\n", users[user_index].balance / 100);
printf("流量使用量:%dMB\n", users[user_index].data_usage);
printf("短信数量:%d\n", users[user_index].message_count);
printf("通话时长:%d秒\n", users[user_index].call_duration);
}
int main() {
int choice;
while (1) {
show_menu();
printf("请输入选项:");
scanf("%d", &choice);
getchar(); // 读取多余的换行符
switch (choice) {
case 0:
printf("谢谢使用,再见!\n");
exit(0);
case 1:
open_service();
break;
case 2:
close_service();
break;
case 3:
show_call_records();
break;
case 4:
recharge();
break;
case 5:
billing();
break;
case 6:
show_info();
break;
default:
printf("无效选项,请重新输入!\n");
}
}
return 0;
}
```
在程序中,我们使用了一个 `User` 结构体来表示每个用户的信息,包括用户名、手机号、余额、流量使用量、短信数量和通话时长。我们使用一个全局数组 `users` 来存储所有用户的信息,并使用一个变量 `user_count` 来记录用户数量。
在菜单函数 `show_menu` 中,我们列出了所有可以进行的操作,让用户选择需要进行的操作。
在主函数中,我们使用一个循环来不断显示菜单,读取用户的选择,并根据用户的选择调用相应的函数来完成业务逻辑。
在开通业务函数 `open_service` 中,我们首先要求用户输入用户名和手机号,然后检查该手机号是否已被注册,如果已经注册,则提示用户重新输入;如果用户数量已达到上限,则提示用户无法继续开通业务。如果一切正常,则创建一个新的 `User` 结构体,并将其添加到 `users` 数组中。
在销号业务函数 `close_service` 中,我们要求用户输入要销号的手机号,然后查找该手机号对应的用户,如果找到了该用户,则移除该用户的信息。
在通话记录函数 `show_call_records` 中,我们要求用户输入手机号,并查找该手机号对应的用户,然后显示该用户的通话记录。
在充值业务函数 `recharge` 中,我们要求用户输入手机号和充值金额,然后查找该手机号对应的用户,并将用户的余额增加充值金额。
在计费业务函数 `billing` 中,我们要求用户输入手机号,并计算该用户的话费、流量费和短信费。如果用户的余额足够支付总费用,则从用户的余额中扣除费用,同时清零用户的通话时长、流量使用量和短信数量。否则,提示用户余额不足,无法完成计费。
在查询业务函数 `show_info` 中,我们要求用户输入手机号,并查找该手机号对应的用户,然后显示该用户的所有信息。
请注意,在程序中,所有金额单位都是分,需要在输出时转换为元。同时,我们在读取用户输入时,使用 `fgets` 函数读取一行字符串,并使用 `strcspn` 函数去除末尾的换行符。这是为了避免用户输入的字符串中包含换行符,导致读取不完整。
相关推荐
![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)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)