ATM自动取款机系统 已经折磨好几天了
#include<stdio.h>
#include<stdlib.h>
#include<conio.h>
#include<string.h>
void regist();
void login();
void quit();
void zhuXiao();
void service();
void selectMoney();
void saveMoney();
void getMoney();
void zhuanzhang();
void updatePassword();
void inputPassword(char mima[]);
int find,dfind,zx=0;
struct bank
{
char name[20];//用户名
char password[7];//密码
int account;//账号
double money;//余额
}kehu, t;
FILE *fp;
//主函数
void main()
{
int t = 1;
char xz;;
for (int i = 1; i<100; i++)
{
printf(“\n\n\n\n\t\t\t\t\t\t欢迎使用青软实训ATM系统\n\n”);
printf(“\t\t\t\t\t\t正在进去主页面,请稍等”);
for (int j = 1; j<t; j++)
{
printf(“.”);
}
t++;
if (t == 10)t = 1;
printf(“\n%d%%”, i);
for (int m = 0; m<50000000; m++);
system(“cls”);//清除函数
}
while (1)
{
printf(“\n\n\n\t\t\t青软实训系统正在为你服务\n\n”);
printf(“\t\t\t服务【A】:用户注册\n\n\n”);
printf(“\t\t\t服务【B】:用户登录\n\n\n”);
printf(“\t\t\t服务【C】:退出系统\n\n\n”);
printf(“\t\t\t请选择服务:”);
fflush(stdin);//清理键盘缓冲区的回车符
scanf_s(“%c”, &xz);
if (xz == “”a”” || xz == “”A””)
{
regist();
}
else if (xz == “”b”” || xz == “”B””)
{
login();
}
else if (xz == “”c”” || xz == “”C””)
{
quit();
}
else
{
printf(“\n\n\t\t\t输入选择有误,请重新输入\n”);
}
printf(“\n\n\t\t\t按任意键继续…..”);
_getch();//不经过缓冲区的字符输入函数
system(“cls”);
}
}
void service()//服务界面函数
{
char xz = {};
while (1)
{
system(“cls”);
printf(“\n\n\t\t\t您正在使用服务功能界面”);
printf(“\n\n\t\t\ta、查询余额”);
printf(“\n\n\t\t\tb、存款”);
printf(“\n\n\t\t\tc、取款”);
printf(“\n\n\t\t\td、转账”);
printf(“\n\n\t\t\te、修改密码”);
printf(“\n\n\t\t\tf、注销”);
printf(“\n\n\t\t\tg、退出系统”);
printf(“\n\n\t\t\t请选择服务种类:”);
fflush(stdin);
scanf_s(“%c”,&xz);
switch (xz)
{
case””a””:
case””A””:selectMoney(); break;
case””b””:
case””B””:saveMoney(); break;
case””c””:
case””C””:getMoney(); break;
case””d””:
case””D””:zhuanzhang(); break;
case””e””:
case””E””:updatePassword(); break;
case””f””:
case””F””:zhuXiao(); if (zx == 1) return; break;
case””g””:
case””G””:quit(); break;
}
printf(“按任意键继续……”);
_getch();
}
}
void regist()//注册函数
{
char password1[7];//确认密码
fopen_s(&fp,”atm.txt”, “ab+”);
if (fp == NULL)
{
printf(“文件打开失败”);
return;
}
printf(“\n\t\t\t请输入用户名:”);
fflush(stdin);
gets_s(kehu.name);
printf(“\n\t\t\t这个用户名是:%s\n”, kehu.name);
while (1)
{
while (1)
{
printf(“\n\t\t\t请输入用户密码:”);
fflush(stdin);
inputPassword(kehu.password);
int length = strlen(kehu.password);
if (length == 6)break;
else
printf(“/n/n/t/t/t用户密码长度必须为6位”);
}
printf(“\n\t\t\t请输入确认密码:”);
fflush(stdin);
inputPassword(password1);
if (strcmp(kehu.password, password1) == 0)break;
else
printf(“\n\t\t\t两次密码不一致,请重新输入”);
}
rewind(fp);//将文件指针定位到文件开头
if (fread(&t, sizeof(t), 1, fp) == 1)
{
fseek(fp, -sizeof(kehu), 2);
fread(&t, sizeof(t), 1, fp);//读取最后一条记录
kehu.account = t.account + 1;
}
else
{
kehu.account = 20140001;
}
kehu.money = 0;
fseek(fp, 0, 2);
fwrite(&kehu, sizeof(struct bank), 1, fp);
fclose(fp);
printf(“\n\n\t\t\t注册成功\n”);
printf(“\n\n\t\t\t您的账号为:%d\n”, kehu.account);
printf(“\n\n\n\t\t您正在执行的是注册函数应用”);
}
void login()//登录函数
{
fopen_s(&fp, “atm.txt”, “rb+”);
if (fp = NULL)
{
printf(“\n\n\t\t\t打开失败\n”);
return;
}
printf(“\n\n\n\n\n\t\t您正在执行的是登录函数应用”);
int zhanghao, f = 0, i, j;
char password1[7];
struct bank t;
printf(“\n\n\t\t\t请输入您的账号:”);
scanf_s(“%d”, &zhanghao);
while (fread(&t, sizeof(t),1,fp)==1)
{
if (zhanghao == t.account)
{
f = 1;
break;
}
}
if (f == 1)
{
for ( j = 0; j < 3; j++)
{
printf(“\n\n\t\t\t请输入您的密码:”);
inputPassword(password1);
if (strcmp(password1, t.password) == 0)
{
printf(“\n\n\t\t\t登录成功”);
service();
break;
}
}
if (j == 3)
{
printf(“\n\n\t\t\t三次验证都不成功,吞卡中……”);
}
}
else
{
printf(“\n\n\t\t\t没有此账号”);
}
fclose(fp);
}
void quit()//退出函数
{
printf(“\n\n\n\n\n\n\t\t\t青软实训ATM为你服务”);
printf(“\n\n\n\n\n\t\t\t正在退出,请稍后”);
exit(0);//退出系统函数
}
void selectMoney()//查询函数
{
system(“cls”);
printf(“\n\n\n\n\t\t\t您正在使用的是查询功能”);
printf(“\n\n\t\t\t您的账户余额为:%.2lf”, t.money);
}
void saveMoney()//存款函数
{
system(“cls”);
int qian;
printf(“\n\n\n\n\t\t\t您现在正在执行的是存款服务”);
printf(“\n\n\t\t\t请输入存款金额:”);
scanf_s(“%d”, &qian);
t.money = t.money + qian;//修改内存
fseek(fp, -sizeof(t), 1);
fwrite(&t, sizeof(t), 1, fp);
printf(“\n\n\t\t\t存款成功”);
}
void getMoney()//取款函数
{
system(“cls”);
int qian;
printf(“\n\n\n\n\t\t\t您现在正在执行的是取款服务”);
printf(“\n\n\t\t\t请输入取款金额”);
scanf_s(“%d”, &qian);
if (t.money >= qian)
{
t.money = t.money – qian;//修改内存
fseek(fp, -sizeof(t), 1);
fwrite(&t, sizeof(t), 1, fp);
printf(“\n\n\t\t\t取款成功”);
}
else
{
printf(“\n\n\t\t\t余额不足”);
}
}
void zhuanzhang()//转账函数
{
system(“cls”);
int zhanghao, i,f=0,qian;
struct bank temp;
int n = ftell(fp);
printf(“\n\n\n\n\t\t\t您正在执行的是转账服务”);
printf(“\n\n\t\t\t请输入对方账号:”);
scanf_s(“%d”, &zhanghao);
rewind(fp);
while (fread(&temp, sizeof(temp),1,fp)==1)
{
if (zhanghao == temp.account)
{
f = 1;
break;
}
}
if (f == 1)
{
printf(“\n\n\t\t\t请输入转账金额:”);
scanf_s(“%d”, &qian);
if (t.money >= qian)
{
t.money =t.money – qian;
temp.money = temp.money + qian;
//修改文件数据
fseek(fp, -sizeof(t), 1);
fwrite(&temp, sizeof(t), 1, fp);
fseek(fp, n – sizeof(t), 0);
fwrite(&t, sizeof(t), 1, fp);
printf(“\n\n\t\t\t转账成功”);
}
else
{
printf(“\n\n\t\t\t余额不足”);
}
}
else
{
printf(“\n\n\t\t\t不存在要转账的账号”);
}
}
void updatePassword()//修改密码函数
{
int i;
system(“cls”);
char password1[20], password2[20], password3[20];
printf(“\n\n\n\n\t\t您正在执行的是修改密码功能”);
for (i = 0; i < 3; i++)
{
printf(“\n\n\t\t\t请输入旧密码:”);
inputPassword(password1);
if (strcmp(password1, t.password) == 0)
{
break;
}
else
{
printf(“\n\n\t\t\t旧密码输入有误,请重新输入”);
}
}
if (i < 3)
{
while (1)
{
printf(“\n\n\t\t\t请输入新密码:”);
inputPassword(password2);
printf(“\n\n\t\t\t请输入新密码:”);
inputPassword(password3);
if (strcmp(password2, password3) == 0)
{
strcpy_s(t.password, password2);
fseek(fp, -sizeof(t), 1);
fwrite(&t, sizeof(t), 1, fp);
printf(“修改成功”);
break;
}
else
{
printf(“\n\n\t\t\t两次密码输入不一致,请重新输入”);
}
}
}
}
void zhuXiao()//注销函数
{
system(“cls”);
char sf;
printf(“\n\n\n\n\t\t\t您现在正在执行的是注销功能”);
printf(“\n\n您要注销的客户的账号是: %d\n”, t.account);
printf(“\n\n\t\t\t您能否真的要对此账号注销? 注销【Y】,不注销【N】”);
fflush(stdin);
scanf_s(“%c”, &sf);
if (sf == “”y”” || sf == “”Y””)
{
printf(“\n\n\t\t\t正在对账号%d注销”, t.account);
zx = 1;
}
else
{
printf(“\n\n\t\t\t不对账号%d注销,返回服务界面”,t.account);
}
if (zx == 1)return;
}
//密码函数
void inputPassword(char mima[])
{
int i = 0;
char ch;
while (1)
{
ch = _getch();
if (ch != “”\r””)
{
if (ch != “”\b””)
{
mima[i] = ch;
i++;
printf(“*”);
}
else
{
if (i > 0)
{
i–;
printf(“\b \b”);
}
}
}
else
{
break;
}
}
mima[i] = “”\0″”;
printf(“\n”);
}
#include<stdio.h>
#include<stdlib.h>
#include<conio.h>
#include<string.h>
void regist();
void login();
void quit();
void zhuXiao();
void service();
void selectMoney();
void saveMoney();
void getMoney();
void zhuanzhang();
void updatePassword();
void inputPassword(char mima[]);
int find,dfind,zx=0;
struct bank
{
char name[20];//用户名
char password[7];//密码
int account;//账号
double money;//余额
}kehu, t;
FILE *fp;
//主函数
void main()
{
int t = 1;
char xz;;
for (int i = 1; i<100; i++)
{
printf(“\n\n\n\n\t\t\t\t\t\t欢迎使用青软实训ATM系统\n\n”);
printf(“\t\t\t\t\t\t正在进去主页面,请稍等”);
for (int j = 1; j<t; j++)
{
printf(“.”);
}
t++;
if (t == 10)t = 1;
printf(“\n%d%%”, i);
for (int m = 0; m<50000000; m++);
system(“cls”);//清除函数
}
while (1)
{
printf(“\n\n\n\t\t\t青软实训系统正在为你服务\n\n”);
printf(“\t\t\t服务【A】:用户注册\n\n\n”);
printf(“\t\t\t服务【B】:用户登录\n\n\n”);
printf(“\t\t\t服务【C】:退出系统\n\n\n”);
printf(“\t\t\t请选择服务:”);
fflush(stdin);//清理键盘缓冲区的回车符
scanf_s(“%c”, &xz);
if (xz == “”a”” || xz == “”A””)
{
regist();
}
else if (xz == “”b”” || xz == “”B””)
{
login();
}
else if (xz == “”c”” || xz == “”C””)
{
quit();
}
else
{
printf(“\n\n\t\t\t输入选择有误,请重新输入\n”);
}
printf(“\n\n\t\t\t按任意键继续…..”);
_getch();//不经过缓冲区的字符输入函数
system(“cls”);
}
}
void service()//服务界面函数
{
char xz = {};
while (1)
{
system(“cls”);
printf(“\n\n\t\t\t您正在使用服务功能界面”);
printf(“\n\n\t\t\ta、查询余额”);
printf(“\n\n\t\t\tb、存款”);
printf(“\n\n\t\t\tc、取款”);
printf(“\n\n\t\t\td、转账”);
printf(“\n\n\t\t\te、修改密码”);
printf(“\n\n\t\t\tf、注销”);
printf(“\n\n\t\t\tg、退出系统”);
printf(“\n\n\t\t\t请选择服务种类:”);
fflush(stdin);
scanf_s(“%c”,&xz);
switch (xz)
{
case””a””:
case””A””:selectMoney(); break;
case””b””:
case””B””:saveMoney(); break;
case””c””:
case””C””:getMoney(); break;
case””d””:
case””D””:zhuanzhang(); break;
case””e””:
case””E””:updatePassword(); break;
case””f””:
case””F””:zhuXiao(); if (zx == 1) return; break;
case””g””:
case””G””:quit(); break;
}
printf(“按任意键继续……”);
_getch();
}
}
void regist()//注册函数
{
char password1[7];//确认密码
fopen_s(&fp,”atm.txt”, “ab+”);
if (fp == NULL)
{
printf(“文件打开失败”);
return;
}
printf(“\n\t\t\t请输入用户名:”);
fflush(stdin);
gets_s(kehu.name);
printf(“\n\t\t\t这个用户名是:%s\n”, kehu.name);
while (1)
{
while (1)
{
printf(“\n\t\t\t请输入用户密码:”);
fflush(stdin);
inputPassword(kehu.password);
int length = strlen(kehu.password);
if (length == 6)break;
else
printf(“/n/n/t/t/t用户密码长度必须为6位”);
}
printf(“\n\t\t\t请输入确认密码:”);
fflush(stdin);
inputPassword(password1);
if (strcmp(kehu.password, password1) == 0)break;
else
printf(“\n\t\t\t两次密码不一致,请重新输入”);
}
rewind(fp);//将文件指针定位到文件开头
if (fread(&t, sizeof(t), 1, fp) == 1)
{
fseek(fp, -sizeof(kehu), 2);
fread(&t, sizeof(t), 1, fp);//读取最后一条记录
kehu.account = t.account + 1;
}
else
{
kehu.account = 20140001;
}
kehu.money = 0;
fseek(fp, 0, 2);
fwrite(&kehu, sizeof(struct bank), 1, fp);
fclose(fp);
printf(“\n\n\t\t\t注册成功\n”);
printf(“\n\n\t\t\t您的账号为:%d\n”, kehu.account);
printf(“\n\n\n\t\t您正在执行的是注册函数应用”);
}
void login()//登录函数
{
fopen_s(&fp, “atm.txt”, “rb+”);
if (fp = NULL)
{
printf(“\n\n\t\t\t打开失败\n”);
return;
}
printf(“\n\n\n\n\n\t\t您正在执行的是登录函数应用”);
int zhanghao, f = 0, i, j;
char password1[7];
struct bank t;
printf(“\n\n\t\t\t请输入您的账号:”);
scanf_s(“%d”, &zhanghao);
while (fread(&t, sizeof(t),1,fp)==1)
{
if (zhanghao == t.account)
{
f = 1;
break;
}
}
if (f == 1)
{
for ( j = 0; j < 3; j++)
{
printf(“\n\n\t\t\t请输入您的密码:”);
inputPassword(password1);
if (strcmp(password1, t.password) == 0)
{
printf(“\n\n\t\t\t登录成功”);
service();
break;
}
}
if (j == 3)
{
printf(“\n\n\t\t\t三次验证都不成功,吞卡中……”);
}
}
else
{
printf(“\n\n\t\t\t没有此账号”);
}
fclose(fp);
}
void quit()//退出函数
{
printf(“\n\n\n\n\n\n\t\t\t青软实训ATM为你服务”);
printf(“\n\n\n\n\n\t\t\t正在退出,请稍后”);
exit(0);//退出系统函数
}
void selectMoney()//查询函数
{
system(“cls”);
printf(“\n\n\n\n\t\t\t您正在使用的是查询功能”);
printf(“\n\n\t\t\t您的账户余额为:%.2lf”, t.money);
}
void saveMoney()//存款函数
{
system(“cls”);
int qian;
printf(“\n\n\n\n\t\t\t您现在正在执行的是存款服务”);
printf(“\n\n\t\t\t请输入存款金额:”);
scanf_s(“%d”, &qian);
t.money = t.money + qian;//修改内存
fseek(fp, -sizeof(t), 1);
fwrite(&t, sizeof(t), 1, fp);
printf(“\n\n\t\t\t存款成功”);
}
void getMoney()//取款函数
{
system(“cls”);
int qian;
printf(“\n\n\n\n\t\t\t您现在正在执行的是取款服务”);
printf(“\n\n\t\t\t请输入取款金额”);
scanf_s(“%d”, &qian);
if (t.money >= qian)
{
t.money = t.money – qian;//修改内存
fseek(fp, -sizeof(t), 1);
fwrite(&t, sizeof(t), 1, fp);
printf(“\n\n\t\t\t取款成功”);
}
else
{
printf(“\n\n\t\t\t余额不足”);
}
}
void zhuanzhang()//转账函数
{
system(“cls”);
int zhanghao, i,f=0,qian;
struct bank temp;
int n = ftell(fp);
printf(“\n\n\n\n\t\t\t您正在执行的是转账服务”);
printf(“\n\n\t\t\t请输入对方账号:”);
scanf_s(“%d”, &zhanghao);
rewind(fp);
while (fread(&temp, sizeof(temp),1,fp)==1)
{
if (zhanghao == temp.account)
{
f = 1;
break;
}
}
if (f == 1)
{
printf(“\n\n\t\t\t请输入转账金额:”);
scanf_s(“%d”, &qian);
if (t.money >= qian)
{
t.money =t.money – qian;
temp.money = temp.money + qian;
//修改文件数据
fseek(fp, -sizeof(t), 1);
fwrite(&temp, sizeof(t), 1, fp);
fseek(fp, n – sizeof(t), 0);
fwrite(&t, sizeof(t), 1, fp);
printf(“\n\n\t\t\t转账成功”);
}
else
{
printf(“\n\n\t\t\t余额不足”);
}
}
else
{
printf(“\n\n\t\t\t不存在要转账的账号”);
}
}
void updatePassword()//修改密码函数
{
int i;
system(“cls”);
char password1[20], password2[20], password3[20];
printf(“\n\n\n\n\t\t您正在执行的是修改密码功能”);
for (i = 0; i < 3; i++)
{
printf(“\n\n\t\t\t请输入旧密码:”);
inputPassword(password1);
if (strcmp(password1, t.password) == 0)
{
break;
}
else
{
printf(“\n\n\t\t\t旧密码输入有误,请重新输入”);
}
}
if (i < 3)
{
while (1)
{
printf(“\n\n\t\t\t请输入新密码:”);
inputPassword(password2);
printf(“\n\n\t\t\t请输入新密码:”);
inputPassword(password3);
if (strcmp(password2, password3) == 0)
{
strcpy_s(t.password, password2);
fseek(fp, -sizeof(t), 1);
fwrite(&t, sizeof(t), 1, fp);
printf(“修改成功”);
break;
}
else
{
printf(“\n\n\t\t\t两次密码输入不一致,请重新输入”);
}
}
}
}
void zhuXiao()//注销函数
{
system(“cls”);
char sf;
printf(“\n\n\n\n\t\t\t您现在正在执行的是注销功能”);
printf(“\n\n您要注销的客户的账号是: %d\n”, t.account);
printf(“\n\n\t\t\t您能否真的要对此账号注销? 注销【Y】,不注销【N】”);
fflush(stdin);
scanf_s(“%c”, &sf);
if (sf == “”y”” || sf == “”Y””)
{
printf(“\n\n\t\t\t正在对账号%d注销”, t.account);
zx = 1;
}
else
{
printf(“\n\n\t\t\t不对账号%d注销,返回服务界面”,t.account);
}
if (zx == 1)return;
}
//密码函数
void inputPassword(char mima[])
{
int i = 0;
char ch;
while (1)
{
ch = _getch();
if (ch != “”\r””)
{
if (ch != “”\b””)
{
mima[i] = ch;
i++;
printf(“*”);
}
else
{
if (i > 0)
{
i–;
printf(“\b \b”);
}
}
}
else
{
break;
}
}
mima[i] = “”\0″”;
printf(“\n”);
}
解决方案:40分
scanf_s使用时,需要给出读取字符数量,否则无法获取正确的值scanf_s(“%c”, &xz, 1);
LZ的login中打开文件之后的if判断有问题,是== ,而不是=号。if (fp == NULL)
LZ的login中打开文件之后的if判断有问题,是== ,而不是=号。if (fp == NULL)