用c语言写银行储蓄代码1、开户业务 2、续存业务3、续取业务4、销户业务5、挂失业务6、批量业务7、咨询估算业务8、个人消费贷款业务9、系统管理0、返回
时间: 2023-10-31 08:06:00 浏览: 138
以下是一个简单的银行储蓄代码示例,主要实现了开户、存款、取款、查询余额、销户和修改密码等基本功能。代码中使用了结构体存储客户信息和账户信息。
```c
#include <stdio.h>
#include <string.h>
// 客户信息结构体
typedef struct {
char name[20]; // 姓名
char id[18]; // 身份证号
char phone[12]; // 手机号码
char password[7]; // 密码
} customer;
// 账户信息结构体
typedef struct {
int id; // 账户号码
double balance; // 账户余额
int status; // 账户状态(0:正常,1:挂失,2:销户)
customer owner; // 账户所有者信息
} account;
// 全局变量,存储所有账户信息
account accounts[100];
int num_accounts = 0; // 当前账户数
// 开户
void createAccount() {
if (num_accounts >= 100) {
printf("无法再创建新账户,已达到最大数量。\n");
return;
}
printf("请输入姓名:");
scanf("%s", accounts[num_accounts].owner.name);
printf("请输入身份证号:");
scanf("%s", accounts[num_accounts].owner.id);
printf("请输入手机号码:");
scanf("%s", accounts[num_accounts].owner.phone);
printf("请设置密码(6位数字):");
scanf("%s", accounts[num_accounts].owner.password);
accounts[num_accounts].id = 100000 + num_accounts; // 账户号码从 100001 开始
accounts[num_accounts].balance = 0;
accounts[num_accounts].status = 0;
num_accounts++;
printf("开户成功,您的账户号码是:%d\n", accounts[num_accounts - 1].id);
}
// 存款
void deposit() {
int account_id, amount;
char password[7];
printf("请输入账户号码:");
scanf("%d", &account_id);
printf("请输入存款金额:");
scanf("%d", &amount);
printf("请输入密码:");
scanf("%s", password);
for (int i = 0; i < num_accounts; i++) {
if (accounts[i].id == account_id) {
if (strcmp(accounts[i].owner.password, password) != 0) {
printf("密码错误,存款失败。\n");
break;
}
accounts[i].balance += amount;
printf("存款成功,当前余额为:%g\n", accounts[i].balance);
break;
}
if (i == num_accounts - 1) {
printf("账户不存在,存款失败。\n");
}
}
}
// 取款
void withdraw() {
int account_id, amount;
char password[7];
printf("请输入账户号码:");
scanf("%d", &account_id);
printf("请输入取款金额:");
scanf("%d", &amount);
printf("请输入密码:");
scanf("%s", password);
for (int i = 0; i < num_accounts; i++) {
if (accounts[i].id == account_id) {
if (strcmp(accounts[i].owner.password, password) != 0) {
printf("密码错误,取款失败。\n");
break;
}
if (accounts[i].balance < amount) {
printf("余额不足,取款失败。\n");
break;
}
accounts[i].balance -= amount;
printf("取款成功,当前余额为:%g\n", accounts[i].balance);
break;
}
if (i == num_accounts - 1) {
printf("账户不存在,取款失败。\n");
}
}
}
// 查询余额
void checkBalance() {
int account_id;
char password[7];
printf("请输入账户号码:");
scanf("%d", &account_id);
printf("请输入密码:");
scanf("%s", password);
for (int i = 0; i < num_accounts; i++) {
if (accounts[i].id == account_id) {
if (strcmp(accounts[i].owner.password, password) != 0) {
printf("密码错误,查询失败。\n");
break;
}
printf("当前余额为:%g\n", accounts[i].balance);
break;
}
if (i == num_accounts - 1) {
printf("账户不存在,查询失败。\n");
}
}
}
// 销户
void closeAccount() {
int account_id;
char password[7];
printf("请输入账户号码:");
scanf("%d", &account_id);
printf("请输入密码:");
scanf("%s", password);
for (int i = 0; i < num_accounts; i++) {
if (accounts[i].id == account_id) {
if (strcmp(accounts[i].owner.password, password) != 0) {
printf("密码错误,销户失败。\n");
break;
}
accounts[i].status = 2;
printf("销户成功,欢迎下次光临。\n");
break;
}
if (i == num_accounts - 1) {
printf("账户不存在,销户失败。\n");
}
}
}
// 修改密码
void changePassword() {
int account_id;
char old_password[7], new_password[7];
printf("请输入账户号码:");
scanf("%d", &account_id);
printf("请输入原密码:");
scanf("%s", old_password);
printf("请输入新密码:");
scanf("%s", new_password);
for (int i = 0; i < num_accounts; i++) {
if (accounts[i].id == account_id) {
if (strcmp(accounts[i].owner.password, old_password) != 0) {
printf("原密码错误,修改失败。\n");
break;
}
strcpy(accounts[i].owner.password, new_password);
printf("修改成功。\n");
break;
}
if (i == num_accounts - 1) {
printf("账户不存在,修改失败。\n");
}
}
}
int main() {
int choice;
while (1) {
printf("请选择业务:\n");
printf("1、开户业务 2、续存业务 3、续取业务 4、销户业务 5、挂失业务\n");
printf("6、批量业务 7、咨询估算业务 8、个人消费贷款业务 9、系统管理 0、返回\n");
scanf("%d", &choice);
switch (choice) {
case 1:
createAccount();
break;
case 2:
deposit();
break;
case 3:
withdraw();
break;
case 4:
closeAccount();
break;
case 5:
printf("该业务尚未开通。\n");
break;
case 6:
printf("该业务尚未开通。\n");
break;
case 7:
checkBalance();
break;
case 8:
changePassword();
break;
case 9:
printf("该业务尚未开通。\n");
break;
case 0:
return 0;
default:
printf("无效的选项,请重新输入。\n");
}
}
}
```
阅读全文