#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <conio.h>
#include <windows.h>
#include <time.h>
#define guanli "XXXX"
#define guanlimima "666666"
void toxy(int x, int y)
{
COORD pos = {x, y};
HANDLE Out = GetStdHandle(STD_OUTPUT_HANDLE);
SetConsoleCursorPosition(Out, pos);
}
void color(short x)
{
if (x >= 0 && x <= 15)
{
SetConsoleTextAttribute(GetStdHandle(STD_OUTPUT_HANDLE), x);
}
else
{
SetConsoleTextAttribute(GetStdHandle(STD_OUTPUT_HANDLE), x);
}
}
void guashi()
{
long long a;
color(15);
toxy(38, 2);
printf("请输入所要挂失的银行卡号:");
toxy(38, 4);
scanf("%ld", &a);
toxy(38, 6);
printf("挂失成功!");
toxy(38, 8);
printf("按任意键继续!");
getchar();
getchar();
system("cls");
}
void jieguashi()
{
long long a;
color(15);
toxy(38, 2);
printf("请输入所要挂失的银行卡号:");
toxy(38, 4);
scanf("%ld", &a);
toxy(38, 6);
printf("解挂失成功!");
toxy(38, 8);
printf("按任意键继续!");
getchar();
getchar();
system("cls");
}
struct Account
{
char id[40];
char name[40];
char account[20];
char password[10];
float money;
int state;
} R[50];
void xitong();
void yonghu();
int Index = -1;
int R_load()
{
FILE *fp;
int i = 0;
if ((fp = fopen("ATM.txt", "rb")) == NULL)
{
fp = fopen("ATM.txt", "w");
}
else
{
do
{
fread(&R[i], sizeof(struct Account), 1, fp);
i++;
} while (feof(fp) == 0);
}
fclose(fp);
if (i == 0)
return 0;
return (i - 1);
}
void R_keep(int m)
{
int i;
FILE *fp;
if ((fp = fopen("ATM.txt", "wb+")) == NULL)
{
fp = fopen("ATM.txt", "w");
}
for (i = 0; i < m; i++)
if (fwrite(&R[i], sizeof(struct Account), 1, fp) != 1)
printf("file write error\n");
fclose(fp);
}
void print()
{
int i = 0;
int m = R_load();
toxy(25, 2);
printf("---------------------------------------------------------------------------\n");
toxy(25, 3);
printf(" 身份证号 姓名 银行卡号 密码 余额 \n");
toxy(25, 4);
printf("---------------------------------------------------------------------------\n");
for (i = 0; i < m; i++)
{
toxy(25, 5 + i);
printf("%s %s %s %s %.1f\n", R[i].id, R[i].name, R[i].account, R[i].password, R[i].money);
}
toxy(25, 6 + i);
printf("按任意键继续!");
getchar();
getchar();
system("cls");
}
void add()
{
int i, m = 1;
int n = R_load();
for (i = n; i < m + n; i++)
{
toxy(40, 2);
printf("请输入该账户信息\n", i - n + 1);
toxy(40, 4);
printf("请输入身份证号: ");
do
{
toxy(40, 6);
scanf("%s", R[i].id);
if (strlen(R[i].id) != 18)
{
toxy(40, 8);
printf("您输入的信息有误,请重新输入: ");
}
} while (strlen(R[i].id) != 18);
toxy(40, 8);
printf("请输入姓名: ");
toxy(40, 10);
scanf("%s", R[i].name);
toxy(40, 12);
printf("请输入银行9位卡号: ");
toxy(40, 14);
scanf("%s", R[i].account);
toxy(40, 16);
printf("请输入密码: ");
toxy(40, 18);
scanf("%s", R[i].password);
R[i].money = 0;
R[i].state = 1;
}
R_keep(m + n);
toxy(40, 20);
printf("新建账户完成。\n\n");
toxy(40, 22);
printf("按任意键继续!");
getchar();
getchar();
system("cls");
}
void search()
{
int flag = 1;
int i, j;
int n = R_load();
char account[20];
toxy(25, 2);
printf("请输入要查询的银行卡号:\n");
toxy(25, 4);
scanf("%s", account);
toxy(25, 6);
printf("查询到的记录如下:\n");
toxy(25, 8);
printf("--------------------------------------------------------------------------------\n");
toxy(25, 9);
printf(" 身份证号 姓名 银行卡号 密码 余额 \n");
toxy(25, 10);
printf("--------------------------------------------------------------------------------\n");
for (i = 0; i < n; i++)
{
if (strcmp(R[i].account, account) == 0)
{
flag = 0;
toxy(25, 11);
printf("%s %s %s %s %.1f\n", R[i].id, R[i].name, R[i].account, R[i].password, R[i].money);
}
}
if (flag == 1)
{
toxy(25, 8);
printf("账户信息不存在\n");
}
toxy(25, 13);
printf("按任意键继续!");
getchar();
getchar();
system("cls");
}
void change()
{
int flag = 1;
int i, j;
int n = R_load();
char account[20];
int choose = 0;
toxy(25, 2);
printf("请输入要修改账户的银行卡号:\n");
toxy(25, 4);
scanf("%s", account);
for (i = 0; i < n; i++)
{
if (strcmp(R[i].account, account) == 0)
{
flag = 0;
toxy(25, 6);
printf("---------------------------------------------------------------------------\n");
toxy(25, 7);
printf("请选择: 1.修改身份证号 2.修改姓名 3.修改密码 0.取消修改\n");
toxy(25, 8);
printf("---------------------------------------------------------------------------\n");
toxy(25, 10);
scanf("%d", &choose);
if (choose == 0)
{
system("cls");
return;
}
toxy(25, 12);
printf("修改为:");
switch (choose)
{
case 1:
scanf("%s", R[i].id);
break;
case 2:
scanf("%s", R[i].name);
break;
case 3:
scanf("%s", R[i].password);
break;
default:
break;
}
toxy(25, 14);
printf("修改成功,修改后的数据为:\n");
toxy(25, 16);
printf("%s %s %s %s %.1f %d\n", R[i].id, R[i].name, R[i].account, R[i].password, R[i].money, R[i].state);
}
}
R_keep(n);
if (flag == 1)
{
toxy(25, 6);
printf("账户信息不存在\n");
}
toxy(25, 18);
printf("按任意键继续!");
getchar();
getchar();
system("cls");
}
void Delete()
{
int flag = 1;
int i, j;
int n = R_load();
char account[20];
int choose = 0;
toxy(25, 2);
printf("请输入要注销的银行卡号:\n");
toxy(25, 4);
scanf("%s", account);
for (i = 0; i < n; i++)
{
if (strcmp(R[i].account, account) == 0)
{
flag = 0;
toxy(25, 6);
printf("确定要注销该账户信息吗?\n");
toxy(25, 8);
printf("---------------------------------------\n");
toxy(25, 9);
printf("请输入您的选择: 1.确定 2.取消\n");
toxy(25, 10);
printf("---------------------------------------\n");
toxy(25, 12);
scanf("%d", &choose);
if (choose == 1)
{
for (j = i + 1; j < n; j++)
{
R[j - 1] = R[j];
}
n--;
toxy(25, 14);
printf("已注销该账户信息");
}
else
{
toxy(25, 14);
printf("已取消注销该账户信息\n");
}
}
}
R_keep(n);
if (flag == 1)
{
toxy(25, 6);
printf("账户信息不存在\n");
}
toxy(25, 16);
printf("按任意键继续!");
getchar();
getchar();
system("cls");
}
void reset()
{
int flag = 1;
int i, j;
int n = R_load();
char account[20];
int choose = 0;
toxy(25, 2);
printf("请输入要重置账户的银行卡号:\n");
toxy(25, 4);
scanf("%s", account);
for (i = 0; i < n; i++)
{
if (strcmp(R[i].account, account) == 0)
{
flag = 0;
toxy(25, 6);
printf("确定要重置该账户密码? \n");
toxy(25, 8);
printf("---------------------------------------\n");
toxy(25, 9);
printf("请输入您的选择: 1.确定 2.取消\n");
toxy(25, 10);
printf("---------------------------------------\n");
toxy(25, 12);
scanf("%d", &choose);
if (choose == 1)
{
strcpy(R[i].password, "000000");
R[i].state = 1;
toxy(25, 14);
printf("已重置该账户密码为000000\n");
}
else
{
toxy(25, 14);
printf("已取消重置该账户密码\n");
}
}
}
R_keep(n);
if (flag == 1)
{
toxy(25, 6);
printf("账户信息不存在\n");
}
toxy(25, 16);
printf("按任意键继续!");
getchar();
getchar();
system("cls");
}
void deposit()
{
float money = 0;
int n = R_load();
toxy(45, 3);
printf("存款业务\n");
toxy(45, 5);
printf("请输入要存款的金额:\n");
toxy(45, 7);
scanf("%f", &money);
if (money <= 0)
{
toxy(45, 9);
printf("存款失败,金额需要大于0\n");
}
else
{
R[Index].money += money;
toxy(45, 9);
printf("存款成功,当前账户余额为%.1f元\n", R[Index].money);
R_keep(n);
}
toxy(45, 11);
printf("按任意键继续!");
getchar();
getchar();
system("cls");
}
void withdraw()
{
float money = 0;
int n = R_load();
toxy(45, 3);
printf("取款业务\n");
toxy(45, 5);
printf("当前存款为%.1f元\n", R[Index].money);
toxy(45, 7);
printf("请输入要取款的金额:\n");
toxy(45, 9);
scanf("%f", &money);
if (money <= 0)
{
toxy(45, 11);
printf("取款失败,金额需要大于0\n");
}
else if (R[Index].money < money)
{
toxy(45, 11);
printf("取款失败,金额不得超过存款\n");
}
else
{
R[Index].money -= money;
toxy(45, 11);
printf("取款成功,当前账户余额为%.1f元\n", R[Index].money);
R_keep(n);
}
toxy(45, 13);
printf("按任意键继续!");
getchar();
getchar();
system("cls");
}
void transfer()
{
int i = 0;
int flag = 0;
char account[20];
float money = 0;
int n = R_load();
toxy(45, 3);
printf("转账业务\n");
toxy(45, 5);
printf("当前存款为%.1f元\n", R[Index].money);
toxy(45, 7);
printf("请输入您要转入的银行卡号:\n");
toxy(45, 9);
scanf("%s", account);
toxy(45, 11);
printf("请输入您的转账金额:");
toxy(45, 13);
scanf("%f", &money);
if (money <= 0)
{
toxy(45, 15);
printf("转账失败,金额需要大于0\n");
}
else if (R[Index].money < money)
{
toxy(45, 15);
printf("转账失败,金额不得超过存款\n");
}
else
{
for (i = 0; i < n; i++)
{
if (strcmp(R[i].account, account) == 0)
{
flag = 1;
if (i == Index)
{
toxy(45, 15);
printf("无法转账给自己\n");
}
else
{
R[i].money += money;
R[Index].money -= money;
toxy(45, 15);
printf("转账成功,当前用户余额为%.1f\n", R[Index].money);
R_keep(n);
}
break;
}
}
if (flag == 0)
{
toxy(45, 15);
printf("输入的转账账户不存在\n");
}
}
toxy(45, 17);
printf("按任意键继续!");
getchar();
getchar();
system("cls");
}
void search2()
{
int n = R_load();
toxy(45, 3);
printf("余额查询\n");
toxy(45, 5);
printf("-------------------------------");
toxy(45, 6);
printf("用户%s当前的余额为%.2f元\n", R[Index].name, R[Index].money);
toxy(45, 7);
printf("-------------------------------");
toxy(45, 9);
printf("按任意键继续!");
getchar();
getchar();
system("cls");
}
void changePassword()
{
int n = R_load();
toxy(45, 3);
printf("修改密码\n");
toxy(45, 5);
printf("密码修改为:\n");
toxy(45, 7);
scanf("%s", R[Index].password);
R_keep(n);
toxy(45, 9);
printf("修改成功!\n");
toxy(45, 11);
printf("按任意键继续!");
getchar();
getchar();
system("cls");
}
void xitong()
{
char user[20];
char password[20];
toxy(45, 1);
printf("请输入管理员姓名:\n");
toxy(45, 3);
scanf("%s", user);
toxy(45, 5);
printf("请输入管理员密码:\n");
toxy(45, 7);
scanf("%s", password);
if (strcmp(user, guanli) != 0 || strcmp(password, guanlimima) != 0)
{
toxy(45, 9);
printf("账号或密码错误\n");
toxy(45, 11);
printf("\n按任意键继续!");
getchar();
getchar();
system("cls");
return;
}
system("cls");
while (1)
{
int m = 0;
color(12);
toxy(45, 1);
printf("欢迎管理员来到本ATM,倾听您吩咐!\n\n");
toxy(55, 4);
printf(" 管理员");
toxy(48, 6);
printf("---------------------------");
toxy(48, 8);
printf("| 1.开户 |");
toxy(48, 10);
printf("| 2.查询用户信息 |");
toxy(48, 12);
printf("| 3.修改用户信息 |");
toxy(48, 14);
printf("| 4.注销用户信息 |");
toxy(48, 16);
printf("| 5.重置用户密码 |");
toxy(48, 18);
printf("| 6.查看所有用户信息 |");
toxy(48, 20);
printf("| 7.挂失 |");
toxy(48, 22);
printf("| 8.解挂失 |");
toxy(48, 24);
printf("| 9.退出系统 |");
toxy(48, 26);
printf("---------------------------");
do
{
toxy(52, 28);
printf("请输入您的选择(1-9):");
scanf("%d", &m);
if (m > 9 || m < 1)
printf("输入错误,请重新输入\n");
} while (m > 9 || m < 1);
system("cls");
if (m == 9)
break;
switch (m)
{
case 1:
add();
break;
case 2:
search();
break;
case 3:
change();
break;
case 4:
Delete();
break;
case 5:
reset();
break;
case 6:
print();
break;
case 7:
guashi();
break;
case 8:
jieguashi();
break;
}
}
}
void yonghu()
{
int count = 0;
Index = -1;
int i = 0;
int n = R_load();
char account[20];
char password[20];
toxy(20, 0);
color(15);
printf("温馨提示:您在输入本人账号和密码时,确保周围无外人,保护好自己的信息!");
color(14);
toxy(45, 2);
printf("请输入您的银行卡号\n");
toxy(45, 3);
scanf("%s", account);
for (i = 0; i < n; i++)
{
if (strcmp(R[i].account, account) == 0)
{
Index = i;
}
}
if (Index < 0)
{
toxy(45, 5);
printf("账号不存在\n");
toxy(45, 7);
printf("按任意键继续!");
getchar();
getchar();
system("cls");
return;
}
else if (R[Index].state == 0)
{
toxy(45, 9);
printf("账号被冻结,请联系系统用户解除冻结\n");
toxy(45, 11);
printf("按任意键继续!");
getchar();
getchar();
system("cls");
return;
}
else
{
while (count < 3)
{
count++;
toxy(45, 5);
printf("请输入密码\n");
toxy(45, 7);
scanf("%s", password);
if (strcmp(R[Index].password, password) == 0)
{
break;
}
toxy(45, 9);
printf("输入密码错误,还有%d次尝试机会\n", 3 - count);
if (count >= 3)
{
R[Index].state = 0;
R_keep(n);
toxy(45, 11);
printf("密码输入错误达到三次,账号被冻结,请联系系统用户解除冻结\n");
toxy(45, 13);
printf("按任意键继续!");
getchar();
getchar();
system("cls");
return;
}
}
}
system("cls");
while (1)
{
int m = 0;
color(12);
toxy(38, 1);
printf("欢迎%s先生/女士来到本ATM,下面我会全心全意为您服务!\n\n", R[Index].name);
toxy(55, 4);
printf(" 用户");
toxy(48, 6);
printf("-----------------------");
toxy(48, 8);
printf("| 1.存款 |");
toxy(48, 10);
printf("| 2.取款 |");
toxy(48, 12);
printf("| 3.转账 |");
toxy(48, 14);
printf("| 4.余额查询 |");
toxy(48, 16);
printf("| 5.修改密码 |");
toxy(48, 18);
printf("| 6.退出系统 |");
toxy(48, 20);
printf("-----------------------");
do
{
printf("\n");
toxy(50, 22);
printf("您的选择为:(1-6)");
toxy(50, 24);
scanf("%d", &m);
if (m > 6 || m < 1)
printf("输入错误,请重新输入\n");
} while (m > 6 || m < 1);
system("cls");
if (m == 6)
break;
switch (m)
{
case 1:
deposit();
break;
case 2:
withdraw();
break;
case 3:
transfer();
break;
case 4:
search2();
break;
case 5:
changePassword();
break;
}
}
}
int main()
{
while (1)
{
int m = 0;
color(14);
printf("\n\n");
toxy(50, 5);
printf(" 欢迎来到ATM智能取款机");
toxy(44, 8);
printf("------------------------------------------");
toxy(45, 10);
printf("请输入(1:管理员;2:用户:3:退出ATM.)");
toxy(44, 12);
printf("------------------------------------------");
do
{
printf("\n");
toxy(44, 14);
scanf("%d", &m);
if (m > 3 || m < 1)
printf("输入错误,请重新输入\n");
} while (m > 3 || m < 1);
system("cls");
if (m == 3)
break;
switch (m)
{
case 1:
xitong();
break;
case 2:
yonghu();
break;
}
}
return 0;
}