请用C语言设计一个存储账号和密码的系统,同时具备增删改查的功能
时间: 2024-02-27 21:52:53 浏览: 32
以下是一个使用 C 语言设计的存储账号和密码的系统,同时具备增删改查的功能:
```c
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#define MAX_ACCOUNTS 100 // 最大账号数
#define MAX_NAME_LEN 20 // 账号名最大长度
#define MAX_PWD_LEN 20 // 密码最大长度
typedef struct {
char name[MAX_NAME_LEN + 1];
char password[MAX_PWD_LEN + 1];
} Account;
Account accounts[MAX_ACCOUNTS]; // 定义一个 Account 类型的数组来存储账号和密码
int num_accounts = 0; // 当前账号数
void add_account() {
if (num_accounts >= MAX_ACCOUNTS) {
printf("Error: Too many accounts!\n");
return;
}
printf("Enter account name: ");
scanf("%s", accounts[num_accounts].name);
printf("Enter password: ");
scanf("%s", accounts[num_accounts].password);
printf("Account added successfully!\n");
num_accounts++;
}
void delete_account() {
char name[MAX_NAME_LEN + 1];
printf("Enter account name to delete: ");
scanf("%s", name);
int index = -1;
for (int i = 0; i < num_accounts; i++) {
if (strcmp(accounts[i].name, name) == 0) {
index = i;
break;
}
}
if (index == -1) {
printf("Error: Account not found!\n");
return;
}
for (int i = index; i < num_accounts - 1; i++) {
accounts[i] = accounts[i + 1];
}
printf("Account deleted successfully!\n");
num_accounts--;
}
void modify_account() {
char name[MAX_NAME_LEN + 1];
printf("Enter account name to modify: ");
scanf("%s", name);
int index = -1;
for (int i = 0; i < num_accounts; i++) {
if (strcmp(accounts[i].name, name) == 0) {
index = i;
break;
}
}
if (index == -1) {
printf("Error: Account not found!\n");
return;
}
printf("Enter new password: ");
scanf("%s", accounts[index].password);
printf("Account modified successfully!\n");
}
void search_account() {
char name[MAX_NAME_LEN + 1];
printf("Enter account name to search: ");
scanf("%s", name);
int index = -1;
for (int i = 0; i < num_accounts; i++) {
if (strcmp(accounts[i].name, name) == 0) {
index = i;
break;
}
}
if (index == -1) {
printf("Error: Account not found!\n");
return;
}
printf("Account name: %s\n", accounts[index].name);
printf("Password: %s\n", accounts[index].password);
}
int main() {
int choice;
while (1) {
printf("1. Add account\n");
printf("2. Delete account\n");
printf("3. Modify account\n");
printf("4. Search account\n");
printf("5. Exit\n");
printf("Enter your choice: ");
scanf("%d", &choice);
switch (choice) {
case 1:
add_account();
break;
case 2:
delete_account();
break;
case 3:
modify_account();
break;
case 4:
search_account();
break;
case 5:
exit(0);
default:
printf("Error: Invalid choice!\n");
break;
}
}
return 0;
}
```
以上代码中,我们使用了一个结构体来存储账号和密码,定义了一个最大账号数、最大账号名长度和最大密码长度。然后使用一个 Account 类型的数组来存储账号和密码,使用一个变量来记录当前账号数。
在程序中,我们实现了四个功能函数,分别是添加账号、删除账号、修改账号和查找账号。在每个函数中,我们都使用了一个循环来遍历账号数组,查找要操作的账号。如果账号不存在,就直接返回错误。如果账号存在,就执行相应的操作,并退出循环。在删除账号时,我们使用了一个循环将要删除的账号之后的所有账号往前移动一位,然后将当前账号数减一。
在主函数中,我们使用了一个循环来不断显示菜单,根据用户的选择调用相应的功能函数。如果用户输入的选择不正确,就输出错误信息。
相关推荐
![application/x-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)