用c语言写有转账、查询、修改密码、登录、取款功能的ATM取款机
时间: 2023-09-03 13:07:43 浏览: 64
好的,以下是一个简单的用C语言编写的ATM取款机程序,包含转账、查询、修改密码、登录、取款功能:
```c
#include <stdio.h>
#include <string.h>
// 定义账户结构体,包含账户名、密码、余额
struct Account {
char name[20];
char password[20];
float balance;
};
// 定义账户数组,最多存储10个账户
struct Account accounts[10];
// 定义当前登录账户的索引
int currentAccountIndex = -1;
// 定义函数,用于添加账户
void addAccount(char name[], char password[], float balance) {
struct Account account;
strcpy(account.name, name);
strcpy(account.password, password);
account.balance = balance;
accounts[currentAccountIndex + 1] = account;
currentAccountIndex++;
printf("账户创建成功!\n");
}
// 定义函数,用于查询账户
struct Account* findAccount(char name[], char password[]) {
for (int i = 0; i <= currentAccountIndex; i++) {
if (strcmp(accounts[i].name, name) == 0 && strcmp(accounts[i].password, password) == 0) {
return &accounts[i];
}
}
return NULL;
}
// 定义函数,用于转账
void transfer(struct Account* from, struct Account* to, float amount) {
if (from->balance >= amount) {
from->balance -= amount;
to->balance += amount;
printf("转账成功!\n");
} else {
printf("对不起,余额不足!\n");
}
}
// 定义函数,用于修改密码
void changePassword(struct Account* account, char newPassword[]) {
strcpy(account->password, newPassword);
printf("密码修改成功!\n");
}
int main() {
// 初始化两个账户
addAccount("张三", "123456", 10000);
addAccount("李四", "123456", 20000);
while (1) {
// 判断是否已登录
if (currentAccountIndex == -1) {
printf("欢迎使用ATM取款机,请先登录!\n");
char name[20];
char password[20];
printf("请输入用户名:");
scanf("%s", name);
printf("请输入密码:");
scanf("%s", password);
struct Account* account = findAccount(name, password);
if (account != NULL) {
printf("登录成功!\n");
currentAccountIndex = account - accounts;
} else {
printf("对不起,用户名或密码错误,请重新输入!\n");
}
} else {
printf("请选择操作:\n");
printf("1. 查询余额\n");
printf("2. 修改密码\n");
printf("3. 取款\n");
printf("4. 转账\n");
printf("5. 退出登录\n");
int choice;
scanf("%d", &choice);
switch (choice) {
case 1:
printf("您的当前余额为:%.2f元。\n", accounts[currentAccountIndex].balance);
break;
case 2:
char newPassword[20];
printf("请输入新密码:");
scanf("%s", newPassword);
changePassword(&accounts[currentAccountIndex], newPassword);
break;
case 3:
float amount;
printf("请输入取款金额:");
scanf("%f", &amount);
if (amount > accounts[currentAccountIndex].balance) {
printf("对不起,余额不足!\n");
} else {
accounts[currentAccountIndex].balance -= amount;
printf("取款成功!\n");
printf("您的当前余额为:%.2f元。\n", accounts[currentAccountIndex].balance);
}
break;
case 4:
char toName[20];
float transferAmount;
printf("请输入转账账户名:");
scanf("%s", toName);
printf("请输入转账金额:");
scanf("%f", &transferAmount);
struct Account* toAccount = findAccount(toName, "");
if (toAccount == NULL) {
printf("对不起,账户不存在!\n");
} else {
transfer(&accounts[currentAccountIndex], toAccount, transferAmount);
}
break;
case 5:
currentAccountIndex = -1;
printf("已退出登录。\n");
break;
default:
printf("无效的选择,请重新输入!\n");
}
}
}
return 0;
}
```
这个程序中,我们定义了一个账户结构体,包含账户名、密码、余额。我们使用账户数组来存储多个账户,最多存储10个账户。除此之外,我们还定义了一个变量 `currentAccountIndex`,用于存储当前登录账户的索引。
程序中包含以下几个函数:
- `addAccount()`:添加账户
- `findAccount()`:查询账户
- `transfer()`:转账
- `changePassword()`:修改密码
在主函数中,我们首先初始化两个账户,并进入一个无限循环。在循环中,如果用户未登录,则程序会提示用户输入用户名和密码,进行登录操作。如果用户已登录,则程序会提示用户选择操作,并根据用户的选择执行相应的操作。
在程序中,我们还使用了 `switch` 语句来处理用户的选择,使程序更加清晰易懂。同时,我们还对输入进行了简单的校验,以确保程序的稳定性。
相关推荐
![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)
![application/x-rar](https://img-home.csdnimg.cn/images/20210720083606.png)
![zip](https://img-home.csdnimg.cn/images/20210720083736.png)