C语言通用性强的链表写一个银行管理系统用文件io多文件

时间: 2023-05-28 13:07:43 浏览: 50
这是一个基本的银行管理系统,使用了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"); } ``` 该程序使用链表来管理账户信息,每个账户包括账户名、密码和余额等信息。程序提供了创建新账户、存款、取款、查询余额等基本功能,并可以将账户信息保存到文件中,以便下次使用时加载。在程序启动时,可以先从文件中加载账户信息,然后在程序结束时将账户信息保存到文件中,以保证数据的持久化。

相关推荐

最新推荐

recommend-type

C语言实现银行管理系统

主要为大家详细介绍了C语言实现银行管理系统,具有一定的参考价值,感兴趣的小伙伴们可以参考一下
recommend-type

C语言实现食堂就餐管理系统(带链表)

主要为大家详细介绍了C语言实现食堂就餐管理系统,文中示例代码介绍的非常详细,具有一定的参考价值,感兴趣的小伙伴们可以参考一下
recommend-type

利用C语言替换文件中某一行的方法

大家都知道C语言提供了文件操作,但是替换文件的某一行比较麻烦,下面是我使用的一个方法,现在分享给大家,有需要的朋友们可以参考借鉴。
recommend-type

C语言实现简单航班管理系统

主要为大家详细介绍了C语言实现简单航班管理系统,文中示例代码介绍的非常详细,具有一定的参考价值,感兴趣的小伙伴们可以参考一下
recommend-type

连续调用多个外部系统写接口保证数据一致性的思路

今天小编就为大家分享一篇关于连续调用多个外部系统写接口保证数据一致性的思路,小编觉得内容挺不错的,现在分享给大家,具有很好的参考价值,需要的朋友一起跟随小编来看看吧
recommend-type

zigbee-cluster-library-specification

最新的zigbee-cluster-library-specification说明文档。
recommend-type

管理建模和仿真的文件

管理Boualem Benatallah引用此版本:布阿利姆·贝纳塔拉。管理建模和仿真。约瑟夫-傅立叶大学-格勒诺布尔第一大学,1996年。法语。NNT:电话:00345357HAL ID:电话:00345357https://theses.hal.science/tel-003453572008年12月9日提交HAL是一个多学科的开放存取档案馆,用于存放和传播科学研究论文,无论它们是否被公开。论文可以来自法国或国外的教学和研究机构,也可以来自公共或私人研究中心。L’archive ouverte pluridisciplinaire
recommend-type

实现实时数据湖架构:Kafka与Hive集成

![实现实时数据湖架构:Kafka与Hive集成](https://img-blog.csdnimg.cn/img_convert/10eb2e6972b3b6086286fc64c0b3ee41.jpeg) # 1. 实时数据湖架构概述** 实时数据湖是一种现代数据管理架构,它允许企业以低延迟的方式收集、存储和处理大量数据。与传统数据仓库不同,实时数据湖不依赖于预先定义的模式,而是采用灵活的架构,可以处理各种数据类型和格式。这种架构为企业提供了以下优势: - **实时洞察:**实时数据湖允许企业访问最新的数据,从而做出更明智的决策。 - **数据民主化:**实时数据湖使各种利益相关者都可
recommend-type

SPDK_NVMF_DISCOVERY_NQN是什么 有什么作用

SPDK_NVMF_DISCOVERY_NQN 是 SPDK (Storage Performance Development Kit) 中用于查询 NVMf (Non-Volatile Memory express over Fabrics) 存储设备名称的协议。NVMf 是一种基于网络的存储协议,可用于连接远程非易失性内存存储器。 SPDK_NVMF_DISCOVERY_NQN 的作用是让存储应用程序能够通过 SPDK 查询 NVMf 存储设备的名称,以便能够访问这些存储设备。通过查询 NVMf 存储设备名称,存储应用程序可以获取必要的信息,例如存储设备的IP地址、端口号、名称等,以便能
recommend-type

JSBSim Reference Manual

JSBSim参考手册,其中包含JSBSim简介,JSBSim配置文件xml的编写语法,编程手册以及一些应用实例等。其中有部分内容还没有写完,估计有生之年很难看到完整版了,但是内容还是很有参考价值的。