#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#define MAX_ACCOUNTS 100 // 设置账户上限
#define MAX_NAME_LEN 20 // 设置账户名长度上限
struct Account {
char name[MAX_NAME_LEN]; // 账户名
double balance; // 账户余额
};
// 定义全局变量
struct Account accounts[MAX_ACCOUNTS];
int num_accounts = 0;
// 添加账户
void create_account() {
if (num_accounts >= MAX_ACCOUNTS) {
printf("账户数量已达上限,无法添加新账户。\n");
return;
}
char name[MAX_NAME_LEN];
double balance;
printf("请输入要创建的账户名:\n");
scanf("%s", name);
printf("请输入账户余额:\n");
scanf("%lf", &balance);
// 创建新账户
struct Account new_account;
strcpy(new_account.name, name);
new_account.balance = balance;
accounts[num_accounts] = new_account;
num_accounts++;
printf("已成功创建账户:%s,初始余额为 %.2lf。\n", name, balance);
}
// 账户存款
void deposit() {
char name[MAX_NAME_LEN];
double amount;
printf("请输入要存款的账户名:\n");
scanf("%s", name);
// 查找账户
int account_index = -1;
for (int i = 0; i < num_accounts; i++) {
if (strcmp(accounts[i].name, name) == 0) {
account_index = i;
break;
}
}
if (account_index == -1) {
printf("未找到账户:%s。\n", name);
return;
}
printf("请输入存款金额:\n");
scanf("%lf", &amount);
accounts[account_index].balance += amount;
printf("账户:%s,存入 %.2lf 元,现有余额 %.2lf 元。\n", name, amount, accounts[account_index].balance);
}
// 账户取款
void withdraw() {
char name[MAX_NAME_LEN];
double amount;
printf("请输入要取款的账户名:\n");
scanf("%s", name);
// 查找账户
int account_index = -1;
for (int i = 0; i < num_accounts; i++) {
if (strcmp(accounts[i].name, name) == 0) {
account_index = i;
break;
}
}
if (account_index == -1) {
printf("未找到账户:%s。\n", name);
return;
}
printf("请输入取款金额:\n");
scanf("%lf", &amount);
if (accounts[account_index].balance < amount) {
printf("账户:%s,余额不足,取款失败。\n", name);
return;
}
accounts[account_index].balance -= amount;
printf("账户:%s,取出 %.2lf 元,现有余额 %.2lf 元。\n", name, amount, accounts[account_index].balance);
}
// 转账
void transfer() {
char from_name[MAX_NAME_LEN];
char to_name[MAX_NAME_LEN];
double amount;
printf("请输入转出账户名:\n");
scanf("%s", from_name);
printf("请输入转入账户名:\n");
scanf("%s", to_name);
// 查找账户
int from_index = -1;
int to_index = -1;
for (int i = 0; i < num_accounts; i++) {
if (strcmp(accounts[i].name, from_name) == 0) {
from_index = i;
}
if (strcmp(accounts[i].name, to_name) == 0) {
to_index = i;
}
}
if (from_index == -1 || to_index == -1 || from_index == to_index) {
printf("转账失败:账户名不正确或相同。\n");
return;
}
printf("请输入转账金额:\n");
scanf("%lf", &amount);
if (accounts[from_index].balance < amount) {
printf("账户:%s,余额不足,转账失败。\n", from_name);
return;
}
accounts[from_index].balance -= amount;
accounts[to_index].balance += amount;
printf("从账户:%s,将 %.2lf 元转入账户:%s,现有余额为 %.2lf 和 %.2lf 元。\n", from_name, amount, to_name, accounts[from_index].balance, accounts[to_index].balance);
}
// 显示所有账户信息
void show_accounts() {
if (num_accounts == 0) {
printf("暂无账户信息。\n");
return;
}
printf("所有账户信息如下:\n");
for (int i = 0; i < num_accounts; i++) {
printf("%d. 账户名:%s,余额:%.2lf 元。\n", i+1, accounts[i].name, accounts[i].balance);
}
}
int main() {
while (1) {
printf("欢迎来到银行管理系统,请选择要进行的操作:\n");
printf("1. 创建账户\n");
printf("2. 存款\n");
printf("3. 取款\n");
printf("4. 转账\n");
printf("5. 显示账户信息\n");
printf("6. 退出\n");
int choice;
scanf("%d", &choice);
switch (choice) {
case 1:
create_account();
break;
case 2:
deposit();
break;
case 3:
withdraw();
break;
case 4:
transfer();
break;
case 5:
show_accounts();
break;
case 6:
printf("谢谢使用银行管理系统,再见!\n");
exit(0);
default:
printf("无效的选择,请重新输入。\n");
}
}
return 0;
}
欢迎订阅专栏,数据结构实验,期末大作业,前端后端都有哦,想我发哪个方面的资源或文章可以私信我,免费的哦
标签:index,account,name,管理系统,账户,银行,accounts,printf From: https://blog.csdn.net/2301_79235379/article/details/141003233