使用c语言制作
时间: 2024-01-30 07:04:12 浏览: 66
C语言制作的
好的,那么我们可以使用C语言来实现这个移动营业厅程序。在C语言中,我们可以使用结构体来表示移动用户数据的记录信息,利用文件操作来进行数据的读写,利用排序算法来对数据进行排序。
以下是一个简单的示例程序,实现了上述要求中的部分功能,你可以参考这个代码来进行开发:
```c
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#define MAX_USERS 1000
// 定义移动用户数据的结构体
typedef struct {
char username[20];
char gender[10];
int age;
char id[20];
char phone[20];
char profession[20];
char address[100];
} User;
// 定义移动用户数据的数组
User users[MAX_USERS];
int num_users = 0;
// 保存用户数据到文件中
void save_users() {
FILE *fp = fopen("users.dat", "wb");
fwrite(&num_users, sizeof(int), 1, fp);
fwrite(users, sizeof(User), num_users, fp);
fclose(fp);
}
// 从文件中读取用户数据
void load_users() {
FILE *fp = fopen("users.dat", "rb");
fread(&num_users, sizeof(int), 1, fp);
fread(users, sizeof(User), num_users, fp);
fclose(fp);
}
// 新增移动用户数据记录
void add_user() {
if (num_users >= MAX_USERS) {
printf("Error: too many users!\n");
return;
}
User user;
printf("Please enter user info:\n");
printf("Username: ");
scanf("%s", user.username);
printf("Gender: ");
scanf("%s", user.gender);
printf("Age: ");
scanf("%d", &user.age);
printf("ID: ");
scanf("%s", user.id);
printf("Phone: ");
scanf("%s", user.phone);
printf("Profession: ");
scanf("%s", user.profession);
printf("Address: ");
scanf("%s", user.address);
users[num_users++] = user;
printf("User added successfully!\n");
}
// 按用户名查找移动用户
void find_user_by_username() {
char username[20];
printf("Please enter username to search: ");
scanf("%s", username);
int found = 0;
for (int i = 0; i < num_users; i++) {
if (strcmp(users[i].username, username) == 0) {
printf("User info:\n");
printf("Username: %s\n", users[i].username);
printf("Gender: %s\n", users[i].gender);
printf("Age: %d\n", users[i].age);
printf("ID: %s\n", users[i].id);
printf("Phone: %s\n", users[i].phone);
printf("Profession: %s\n", users[i].profession);
printf("Address: %s\n", users[i].address);
found = 1;
}
}
if (!found) {
printf("User not found!\n");
}
}
// 注册手机号码
void register_phone() {
char username[20];
printf("Please enter username to register phone for: ");
scanf("%s", username);
int found = 0;
for (int i = 0; i < num_users; i++) {
if (strcmp(users[i].username, username) == 0) {
printf("Please enter new phone number: ");
scanf("%s", users[i].phone);
found = 1;
}
}
if (!found) {
printf("User not found!\n");
}
}
// 删除移动用户数据
void delete_user() {
char username[20];
printf("Please enter username to delete: ");
scanf("%s", username);
int found = 0;
for (int i = 0; i < num_users; i++) {
if (strcmp(users[i].username, username) == 0) {
if (strlen(users[i].phone) > 0) {
printf("Please unregister all phones first!\n");
return;
}
for (int j = i; j < num_users - 1; j++) {
users[j] = users[j + 1];
}
num_users--;
printf("User deleted successfully!\n");
found = 1;
}
}
if (!found) {
printf("User not found!\n");
}
}
// 按年龄排序
int cmp_age(const void *a, const void *b) {
const User *ua = (const User *)a;
const User *ub = (const User *)b;
return ua->age - ub->age;
}
// 显示用户列表
void show_users() {
if (num_users == 0) {
printf("No users found!\n");
return;
}
printf("User list:\n");
for (int i = 0; i < num_users; i++) {
printf("%s (age %d, phone %s)\n", users[i].username, users[i].age, users[i].phone);
}
}
// 菜单程序
void menu() {
while (1) {
printf("========== Mobile Business Hall ==========\n");
printf("1. Add user\n");
printf("2. Find user by username\n");
printf("3. Register phone for user\n");
printf("4. Delete user\n");
printf("5. Show user list\n");
printf("6. Save user data to file\n");
printf("7. Load user data from file\n");
printf("0. Exit\n");
printf("Please enter your choice: ");
int choice;
scanf("%d", &choice);
switch (choice) {
case 0:
return;
case 1:
add_user();
break;
case 2:
find_user_by_username();
break;
case 3:
register_phone();
break;
case 4:
delete_user();
break;
case 5:
qsort(users, num_users, sizeof(User), cmp_age);
show_users();
break;
case 6:
save_users();
break;
case 7:
load_users();
break;
default:
printf("Invalid choice!\n");
break;
}
}
}
// 主程序
int main() {
menu();
return 0;
}
```
这个示例程序实现了菜单程序、新增用户、按用户名查找用户、注册手机号码、删除用户、显示用户列表、保存用户数据到文件、从文件中读入用户数据等功能。你可以根据自己的需求进行修改和扩展。
阅读全文