C语言通用性强的链表写一个银行管理系统用文件io多文件
时间: 2023-05-28 17:07:43 浏览: 80
这是一个基本的银行管理系统,使用了C语言中的链表数据结构和文件IO操作,可以实现基本的账户管理、存款、取款和查询等功能。
```c
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
// 定义账户结构体
typedef struct account {
char name[20]; // 账户名
char password[20]; // 账户密码
float balance; // 账户余额
struct account *next; // 链表指针
} Account;
// 全局变量,指向链表头节点
Account *head = NULL;
// 函数声明
void create_new_account();
void deposit();
void withdraw();
void query();
void save_to_file();
void load_from_file();
int main() {
int choice;
while (1) {
printf("\nWelcome to Bank Management System\n");
printf("1. Create a new account\n");
printf("2. Deposit\n");
printf("3. Withdraw\n");
printf("4. Query\n");
printf("5. Exit\n");
printf("Please enter your choice (1-5): ");
scanf("%d", &choice);
switch (choice) {
case 1:
create_new_account();
break;
case 2:
deposit();
break;
case 3:
withdraw();
break;
case 4:
query();
break;
case 5:
save_to_file();
printf("Thanks for using Bank Management System. Bye!\n");
exit(0);
default:
printf("Invalid choice. Please try again.\n");
}
}
return 0;
}
// 创建新账户
void create_new_account() {
Account *new_account = (Account *) malloc(sizeof(Account));
printf("Please enter the account name: ");
scanf("%s", new_account->name);
printf("Please enter the account password: ");
scanf("%s", new_account->password);
new_account->balance = 0;
new_account->next = NULL;
if (head == NULL) {
head = new_account;
} else {
Account *current = head;
while (current->next != NULL) {
current = current->next;
}
current->next = new_account;
}
printf("Account created successfully.\n");
}
// 存款
void deposit() {
char name[20], password[20];
float amount;
printf("Please enter the account name: ");
scanf("%s", name);
printf("Please enter the account password: ");
scanf("%s", password);
printf("Please enter the amount to deposit: ");
scanf("%f", &amount);
Account *current = head;
while (current != NULL) {
if (strcmp(current->name, name) == 0 && strcmp(current->password, password) == 0) {
current->balance += amount;
printf("Deposit successful. New balance: %.2f\n", current->balance);
return;
}
current = current->next;
}
printf("Account not found or password incorrect.\n");
}
// 取款
void withdraw() {
char name[20], password[20];
float amount;
printf("Please enter the account name: ");
scanf("%s", name);
printf("Please enter the account password: ");
scanf("%s", password);
printf("Please enter the amount to withdraw: ");
scanf("%f", &amount);
Account *current = head;
while (current != NULL) {
if (strcmp(current->name, name) == 0 && strcmp(current->password, password) == 0) {
if (current->balance >= amount) {
current->balance -= amount;
printf("Withdrawal successful. New balance: %.2f\n", current->balance);
} else {
printf("Insufficient balance.\n");
}
return;
}
current = current->next;
}
printf("Account not found or password incorrect.\n");
}
// 查询账户余额
void query() {
char name[20], password[20];
printf("Please enter the account name: ");
scanf("%s", name);
printf("Please enter the account password: ");
scanf("%s", password);
Account *current = head;
while (current != NULL) {
if (strcmp(current->name, name) == 0 && strcmp(current->password, password) == 0) {
printf("Account balance: %.2f\n", current->balance);
return;
}
current = current->next;
}
printf("Account not found or password incorrect.\n");
}
// 将账户信息保存到文件中
void save_to_file() {
FILE *fp = fopen("accounts.dat", "wb");
if (fp == NULL) {
printf("Failed to open file.\n");
return;
}
Account *current = head;
while (current != NULL) {
fwrite(current, sizeof(Account), 1, fp);
current = current->next;
}
fclose(fp);
printf("Accounts saved to file.\n");
}
// 从文件中加载账户信息
void load_from_file() {
FILE *fp = fopen("accounts.dat", "rb");
if (fp == NULL) {
printf("Failed to open file.\n");
return;
}
Account *account = (Account *) malloc(sizeof(Account));
while (fread(account, sizeof(Account), 1, fp) == 1) {
account->next = NULL;
if (head == NULL) {
head = account;
} else {
Account *current = head;
while (current->next != NULL) {
current = current->next;
}
current->next = account;
}
account = (Account *) malloc(sizeof(Account));
}
free(account);
fclose(fp);
printf("Accounts loaded from file.\n");
}
```
该程序使用链表来管理账户信息,每个账户包括账户名、密码和余额等信息。程序提供了创建新账户、存款、取款、查询余额等基本功能,并可以将账户信息保存到文件中,以便下次使用时加载。在程序启动时,可以先从文件中加载账户信息,然后在程序结束时将账户信息保存到文件中,以保证数据的持久化。
阅读全文