c语言atm机完整功能实现
时间: 2023-07-13 18:39:41 浏览: 48
ATM(自动取款机)是一种电子设备,它可以为客户提供自动银行服务,包括存款、取款、余额查询以及其他交易等功能。下面是一个基于C语言的ATM机完整功能实现的代码示例:
```c
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
// 定义账户结构体
typedef struct {
int account_num; // 账户号码
char name[50]; // 姓名
float balance; // 余额
char password[50]; // 密码
} account;
// 定义账户数组和账户数量
account accounts[100];
int num_of_accounts = 0;
// 函数声明
void create_account();
void login();
void deposit(int account_index);
void withdraw(int account_index);
void balance_enquiry(int account_index);
void transfer(int account_index);
void change_password(int account_index);
void close_account(int account_index);
// 主函数
int main() {
int choice;
while (1) {
printf("\nWelcome to the ATM\n");
printf("1. Create Account\n");
printf("2. Login\n");
printf("3. Exit\n");
printf("Enter your choice: ");
scanf("%d", &choice);
switch (choice) {
case 1:
create_account();
break;
case 2:
login();
break;
case 3:
printf("Thank you for using the ATM\n");
exit(0);
default:
printf("Invalid choice\n");
}
}
return 0;
}
// 创建账户
void create_account() {
account new_account;
printf("\nEnter account number: ");
scanf("%d", &new_account.account_num);
printf("Enter name: ");
scanf("%s", new_account.name);
printf("Enter initial balance: ");
scanf("%f", &new_account.balance);
printf("Enter password: ");
scanf("%s", new_account.password);
accounts[num_of_accounts] = new_account;
num_of_accounts++;
printf("\nAccount created successfully!\n");
}
// 登录
void login() {
int account_num, account_index, password_attempts = 0;
char password[50];
printf("\nEnter account number: ");
scanf("%d", &account_num);
for (int i = 0; i < num_of_accounts; i++) {
if (accounts[i].account_num == account_num) {
account_index = i;
break;
}
}
if (accounts[account_index].account_num == account_num) {
printf("Enter password: ");
scanf("%s", password);
while (strcmp(password, accounts[account_index].password) != 0) {
printf("Incorrect password. Try again.\n");
password_attempts++;
if (password_attempts == 3) {
printf("Too many incorrect attempts. Account locked.\n");
return;
}
printf("Enter password: ");
scanf("%s", password);
}
int choice;
while (1) {
printf("\nWelcome %s\n", accounts[account_index].name);
printf("1. Deposit\n");
printf("2. Withdraw\n");
printf("3. Balance Enquiry\n");
printf("4. Transfer\n");
printf("5. Change Password\n");
printf("6. Close Account\n");
printf("7. Logout\n");
printf("Enter your choice: ");
scanf("%d", &choice);
switch (choice) {
case 1:
deposit(account_index);
break;
case 2:
withdraw(account_index);
break;
case 3:
balance_enquiry(account_index);
break;
case 4:
transfer(account_index);
break;
case 5:
change_password(account_index);
break;
case 6:
close_account(account_index);
return;
case 7:
printf("Logged out successfully!\n");
return;
default:
printf("Invalid choice\n");
}
}
} else {
printf("Account not found.\n");
}
}
// 存款
void deposit(int account_index) {
float amount;
printf("\nEnter amount to deposit: ");
scanf("%f", &amount);
accounts[account_index].balance += amount;
printf("Deposit successful. New balance is %.2f\n", accounts[account_index].balance);
}
// 取款
void withdraw(int account_index) {
float amount;
printf("\nEnter amount to withdraw: ");
scanf("%f", &amount);
if (amount > accounts[account_index].balance) {
printf("Insufficient balance.\n");
} else {
accounts[account_index].balance -= amount;
printf("Withdrawal successful. New balance is %.2f\n", accounts[account_index].balance);
}
}
// 余额查询
void balance_enquiry(int account_index) {
printf("\nBalance: %.2f\n", accounts[account_index].balance);
}
// 转账
void transfer(int account_index) {
int recipient_account_num, recipient_account_index;
float amount;
printf("\nEnter recipient's account number: ");
scanf("%d", &recipient_account_num);
for (int i = 0; i < num_of_accounts; i++) {
if (accounts[i].account_num == recipient_account_num) {
recipient_account_index = i;
break;
}
}
if (accounts[recipient_account_index].account_num == recipient_account_num) {
printf("Enter amount to transfer: ");
scanf("%f", &amount);
if (amount > accounts[account_index].balance) {
printf("Insufficient balance.\n");
} else {
accounts[account_index].balance -= amount;
accounts[recipient_account_index].balance += amount;
printf("Transfer successful. New balance is %.2f\n", accounts[account_index].balance);
}
} else {
printf("Recipient account not found.\n");
}
}
// 更改密码
void change_password(int account_index) {
char new_password[50];
printf("\nEnter new password: ");
scanf("%s", new_password);
strcpy(accounts[account_index].password, new_password);
printf("Password changed successfully!\n");
}
// 关闭账户
void close_account(int account_index) {
for (int i = account_index; i < num_of_accounts - 1; i++) {
accounts[i] = accounts[i + 1];
}
num_of_accounts--;
printf("Account closed successfully!\n");
}
```
上述代码实现了ATM机的完整功能,包括创建账户、登录、存款、取款、余额查询、转账、更改密码和关闭账户等。将上述代码保存为一个文件,例如`atm.c`,然后在命令行中使用以下命令编译并运行:
```
gcc atm.c -o atm
./atm
```
然后按照提示进行操作即可。