#include<stdio.h>
#include<stdlib.h>
#include<Windows.h>
#include<string.h>
#define MAX_USERS 20//定义txt中账号数量
struct Account {
char name[20];
int id;
int password;
double money;
};
struct Account user[MAX_USERS];
void Init();
void Welcome();
void Loginsystem();
void Login();
void reg();
void Display(int userIndex);
void Query(int userIndex);
void loan(int userIndex);
void ChangePwd(int userIndex);
//可以加背景音乐
void music() {
// Placeholder for music function
}
void Init() {
FILE* file = fopen("C:\\Users\\沐硕\\Desktop\\沐硕.txt", "r");
if (file == NULL) {
printf("Failure to open file!\n");
exit(0);
}
for (int a = 0; a < MAX_USERS; a++) {
fscanf(file, "%s %d %d %lf\n", user[a].name, &user[a].id, &user[a].password, &user[a].money);
}
fclose(file);
}
//字体颜色闪烁;
void color()
{
system("color 1"); Sleep(200); system("color 4"); Sleep(200);
system("color 2"); Sleep(200); system("color 4"); Sleep(200);
system("color 3"); Sleep(200); system("color 4"); Sleep(200);
system("color 4"); Sleep(200); system("color 4"); Sleep(200);
system("color 5"); Sleep(200); system("color 4"); Sleep(200);
}
void Welcome() {
printf("\n\n\n\n");
printf(" ********************************\n");
printf(" ********欢迎来到沐硕银行********\n");
printf(" ********************************\n");
color();
}
//登录系统
void Loginsystem() {
system("cls");
int choice;
printf("\n");
printf("\n");
printf(" 登录系统\n");
printf("\n");
printf("\n");
printf(" 1: 登录\n");
printf(" 2: 注册\n");
printf(" 3: 退出\n");
printf("请输入选择:");
scanf("%d", &choice);
switch (choice) {
case 1:
Login();
break;
case 2:
reg();
break;
case 3:
printf("退出系统!!");
break;
default:
printf("选择错误!请重新选择!");
Loginsystem();
break;
}
}
//注册用户
void reg() {
system("cls");
struct Account newUser;
printf("欢迎注册新用户!\n");
printf("请输入用户名:");
scanf("%s", newUser.name);
printf("请输入账号:");
scanf("%d", &newUser.id);
printf("请输入密码:");
scanf("%d", &newUser.password);
printf("请输出存入金额:");
scanf("%lf", &newUser.money);
FILE* file = fopen("C:\\Users\\沐硕\\Desktop\\沐硕.txt", "a+");
if (file == NULL) {
printf("Failure to open file!\n");
exit(0);
}
fprintf(file, "%s %d %d %lf\n", newUser.name, newUser.id, newUser.password, newUser.money);
fclose(file);
printf("注册成功!\n");
system("pause");
Loginsystem();
}
//登录界面
void Login() {
system("cls");
int account, password;
FILE* file = fopen("C:\\Users\\沐硕\\Desktop\\沐硕.txt", "r");
if (file == NULL) {
printf("无法打开文件!\n");
exit(0);
}
for (int b = 1; b <= 3; b++) {
printf("温馨提示:3次输入错误退出系统!\n");
printf("\n");
printf(" 登录系统\n");
printf("\n");
printf("\n");
printf(" 账号:");
scanf("%d", &account);
printf(" 密码:");
scanf("%d", &password);
for (int a = 0; a < MAX_USERS; a++) {
if (account == user[a].id && password == user[a].password) {
printf(" 登录成功!\n");
printf(" 欢迎客户%s\n", user[a].name);
Sleep(500);
fclose(file);
Display(a);//实参返回 这个a的意义:a指的是登录账号的用户index;传参
return;
}
}
printf("账号或密码错误!请重新输入!\n");
printf("已经第%d次输入错误!\n", b);
system("pause");
system("cls");
}
printf("输入错误次数超过上限,退出系统!\n");
fclose(file);
exit(0);
}
void ChangePwd(int userIndex) {
int oldPassword, newPassword;
FILE* file = fopen("C:\\Users\\沐硕\\Desktop\\沐硕.txt", "r+");
if (file == NULL) {
printf("无法打开文件!\n");
exit(0);
}
system("cls");
printf("修改密码界面\n");
printf("请输入原密码:");
scanf("%d", &oldPassword);
if (oldPassword != user[userIndex].password) {
printf("账号或密码错误!请重新输入!\n");
system("pause");
ChangePwd(userIndex);
}
else {
printf("密码正确!\n");
printf("请输入新密码:");
scanf("%d", &newPassword);
user[userIndex].password = newPassword;
//rewind(file);//指向文件,更新文件中的数据
for (int a = 0; a < MAX_USERS; a++) {
fprintf(file, "%s %d %d %lf\n", user[a].name, user[a].id, user[a].password, user[a].money);
}
fclose(file);
printf("密码修改完成!请牢记自己的登录账号和密码哟!\n");
Sleep(1000);
Loginsystem();
}
}
void loan(int userIndex) {
system("cls");
printf(" 欢迎咨询沐硕借贷\n");
printf(" 下面一款游戏对客户进行测评\n");
Sleep(1000);
printf("恭喜顾客通过本公司基本测评!!\n");
printf("将获得10000的借贷金额\n");
printf("已打入客户账号,请注意查收!!\n");
Sleep(1000);
FILE* file = fopen("C:\\Users\\沐硕\\Desktop\\沐硕.txt", "r+");
if (file == NULL) {
printf("无法打开文件!\n");
exit(0);
}
user[userIndex].money += 10000;
rewind(file);
for (int a = 0; a < MAX_USERS; a++) {
fprintf(file, "%s %d %d %lf\n", user[a].name, user[a].id, user[a].password, user[a].money);
}
fclose(file);
Query(userIndex);
}
void Transfer(int userIndex) {
int targetAccount;
double amount;
FILE* file = fopen("C:\\Users\\沐硕\\Desktop\\沐硕.txt", "r+");
if (file == NULL) {
printf("无法打开文件!\n");
exit(0);
}
system("cls");
printf("!!!注意转账功能暂只支持本银行账号!\n");
while (1) {
printf("请输入转帐账号:");
scanf("%d", &targetAccount);
for (int a = 0; a < MAX_USERS; a++) {
if (targetAccount == user[a].id) {
double balance = user[userIndex].money;
printf("请输入转帐金额:");
scanf("%lf", &amount);
if (balance >= amount) {
user[userIndex].money -= amount;
user[a].money += amount;
rewind(file);
//将文件内部的位置 指针重新指向一个流( 数据流 / 文件)的开头
//这里流的概念:文件内部->流->屏幕显示;屏幕显示->流->文件内部
for (int a = 0; a < MAX_USERS; a++) {
fprintf(file, "%s %d %d %lf\n", user[a].name, user[a].id, user[a].password, user[a].money);
}
Query(userIndex);
fclose(file);
return;
}
else {
printf("余额不足!请重新输入!\n");
continue;
}
}
}
printf("账号不存在,请重新输入:");
}
fclose(file);
}
void Out(int userIndex) {
double amount;
FILE* file = fopen("C:\\Users\\沐硕\\Desktop\\沐硕.txt", "r+");
if (file == NULL) {
printf("无法打开文件!\n");
exit(0);
}
system("cls");
printf("请输入取款金额:");
scanf("%lf", &amount);
if (amount <= user[userIndex].money) {
user[userIndex].money -= amount;
rewind(file);
for (int a = 0; a < MAX_USERS; a++) {
fprintf(file, "%s %d %d %lf\n", user[a].name, user[a].id, user[a].password, user[a].money);
}
printf("取款成功!\n");
printf("余额为:%lf", user[userIndex].money);
system("pause");
Display(userIndex);
}
else {
printf("余额不足!请重新输入!");
system("pause");
Out(userIndex);
}
fclose(file);
system("pause");
Display(userIndex);
}
void Query(int userIndex) {
FILE* file = fopen("C:\\Users\\沐硕\\Desktop\\沐硕.txt", "r");
if (file == NULL) {
printf("无法打开文件!\n");
exit(0);
}
system("cls");
printf("余额为:%lf", user[userIndex].money);
fclose(file);
system("pause");
Display(userIndex);
}
void Display(int userIndex) {
system("cls");
int option;
printf("菜单\n");
printf("1:查询余额\n");
printf("2:取款\n");
printf("3:转账\n");
printf("4:贷款\n");
printf("5:修改密码\n");
printf("6:播放背景音乐\n");
printf("7:返回登录界面\n");
scanf("%d", &option);
switch (option) {
case 1:
Query(userIndex);
break;
case 2:
Out(userIndex);
break;
case 3:
Transfer(userIndex);
break;
case 4:
loan(userIndex);
break;
case 5:
ChangePwd(userIndex);
break;
case 6:
music();
break;
case 7:
Loginsystem();
break;
default:
printf("输入错误,请重新输入!");
Display(userIndex);
break;
}
}
int main()
{
system("title ATM");//控制台命名;
system("mode con cols=50 lines=20");//屏幕大小;
Init();
Welcome();
Loginsystem();
return 0;
}
//首先,定义了一个Account结构体,包含了账户名、账号、密码和余额等信息。
//定义了一个全局的user数组,用于存储最多10个用户的账户信息。
//实现了一些函数的声明,包括初始化函数Init()、欢迎界面函数Welcome()、登录系统函数Loginsystem()、登录函数Login()、注册函数reg()、展示用户界面函数Display()、查询余额函数Query()、贷款函数loan()、修改密码函数ChangePwd()、播放背景音乐函数music()和测试函数test()。
//Init()函数从文件中读取用户的账户信息,并存储到user数组中。
//Welcome()函数打印欢迎界面的信息。
//Loginsystem()函数显示登录系统菜单,根据用户的选择执行相应的操作,包括登录、注册和退出功能。
//reg()函数用于注册新用户,获取用户的输入信息并将其写入文件中。
//Login()函数实现用户登录功能,根据用户输入的账号和密码,在文件中查找对应的账户信息进行验证,若登录成功则显示欢迎信息并进入用户界面。
//ChangePwd()函数用于修改密码,先验证原密码的正确性,然后要求用户输入新密码,并更新文件中的账户信息。
//loan()函数模拟贷款功能,向用户提供借贷金额,将借贷金额加到用户账户余额中,并更新文件中的账户信息。
//Transfer()函数实现转账功能,要求用户输入转账目标账号和转账金额,进行验证后更新用户账户余额和文件中的账户信息。
//Out()函数实现取款功能,要求用户输入取款金额,进行余额验证后更新用户账户余额和文件中的账户信息。
//Query()函数用于查询余额,显示用户当前余额。
//Display()函数展示用户界面菜单,根据用户的选择执行相应的操作,包括查询余额、取款、转账、贷款、修改密码、播放背景音乐和返回登录界面等。
//test()函数是一个测试函数,用于测试其他函数的调用。
//main()函数是程序的入口,首先调用Init()函数初始化用户账户信息,然后显示欢迎界面,并进入登录系统。
//整体上,该程序实现了一个简单的银行账户管理系统,用户可以进行登录、注册、查询余额、取款、转账、贷款、修改密码等操作。每个函数都有特定的功能,并且通过文件来存储和读取用户的账户信息。