帮我用c写个记录账号和密码的程序
时间: 2024-03-01 09:52:20 浏览: 32
好的,以下是一个简单的记录账号和密码的程序示例:
```c
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#define MAX_ACCOUNTS 100 // 最多存储100个账号
typedef struct {
char username[20];
char password[20];
} Account;
void addAccount(Account accounts[], int *numAccounts);
void printAccounts(Account accounts[], int numAccounts);
int main() {
Account accounts[MAX_ACCOUNTS];
int numAccounts = 0;
int choice;
do {
printf("请选择操作:\n");
printf("1. 添加账号\n");
printf("2. 查看所有账号\n");
printf("3. 退出\n");
printf("请输入选项(1-3): ");
scanf("%d", &choice);
switch (choice) {
case 1:
addAccount(accounts, &numAccounts);
break;
case 2:
printAccounts(accounts, numAccounts);
break;
case 3:
printf("程序已退出!\n");
break;
default:
printf("无效的选项!\n");
break;
}
} while (choice != 3);
return 0;
}
void addAccount(Account accounts[], int *numAccounts) {
if (*numAccounts >= MAX_ACCOUNTS) {
printf("账号数量已达到上限!\n");
return;
}
char username[20];
char password[20];
printf("请输入账号用户名:");
scanf("%s", username);
// 检查是否已存在相同用户名的账号
for (int i = 0; i < *numAccounts; i++) {
if (strcmp(accounts[i].username, username) == 0) {
printf("该用户名已存在!\n");
return;
}
}
printf("请输入账号密码:");
scanf("%s", password);
Account newAccount;
strcpy(newAccount.username, username);
strcpy(newAccount.password, password);
accounts[*numAccounts] = newAccount;
(*numAccounts)++;
printf("账号添加成功!\n");
}
void printAccounts(Account accounts[], int numAccounts) {
if (numAccounts == 0) {
printf("还没有任何账号!\n");
return;
}
printf("所有账号:\n");
for (int i = 0; i < numAccounts; i++) {
printf("用户名:%s,密码:%s\n", accounts[i].username, accounts[i].password);
}
}
```
这个程序使用了一个结构体 `Account` 来存储每个账号的用户名和密码,并使用一个数组 `accounts` 来存储所有账号。程序提供了两个操作:添加账号和查看所有账号。在添加账号时,程序会提示用户输入用户名和密码,并检查是否已存在相同用户名的账号;在查看所有账号时,程序会遍历数组并输出所有账号的用户名和密码。
相关推荐
![zip](https://img-home.csdnimg.cn/images/20210720083736.png)
![](https://img-home.csdnimg.cn/images/20210720083646.png)
![pdf](https://img-home.csdnimg.cn/images/20210720083512.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)