课程设计简易提款机 出现循环问题 求高手指导一下
#include <stdlib.h>#include <stdio.h>
#include <string.h>
#include <conio.h>
#include <windows.h>
#include<time.h>
typedef struct atmsystem //定义用户资料
{
char zhanghao[9]; //用户账号
char password[11]; //用户密码
double allmoney; //总金额
}ATMSYTEM;
typedef struct deal //定义交易信息
{
char zhanghao2[9]; //交易对象
char data[50]; //交易日期
char style[10]; //交易类型
double money; //交易金额
double lastmoney; //余额
}DEAL;
void atms_ifo(); //录入用户信息
int shuru(); //输入账号,密码
void cunkuan(); //存款函数
void chaxun(); //查询函数
void qukuan(); //取款函数
void zhuanzhang(); //转账函数
void qingkuang(); //交易情况
int flag; //控制循环,判断
char zhang[10]; //当前用户
static double count; //当前用户余额
int main(void) //主函数
{
int sel,n;
atms_ifo(); //初始用户信息
system("cls"); //清屏
n = shuru(); //是否登录成功
system("cls");
while( n ) //循环主菜单
{
printf("\n \t\t\t__________________________________\n");
printf(" \t\t\t| 欢迎进入ATM银行自助系统 |\n");
printf(" \t\t\t| -------------------------------|\n");
printf(" \t\t\t| 1 存款 |\n");
printf(" \t\t\t| 2 查询余额 |\n");
printf(" \t\t\t| 3 取款 |\n");
printf(" \t\t\t| 4 转账 |\n");
printf(" \t\t\t| 5 查询交易情况 |\n");
printf(" \t\t\t| 0 退出 |\n");
printf(" \t\t\t----------------------------------\n");
while( 1 )
{
printf("请输入选项:");
scanf("%d",&sel);
if( sel >= 0 && sel < 6 )
{
break;
}
}
if( sel == 0 )
{
break;
}
switch(sel)
{
case 1: //执行存款函数
system("cls");
cunkuan();
system("cls");
break;
case 2: //执行查询函数
system("cls");
chaxun();
system("cls");
break;
case 3: //执行取款函数
system("cls");
qukuan();
system("cls");
break;
case 4: //执行转账函数
system("cls");
zhuanzhang();
system("cls");
break;
case 5: //执行交易情况函数
system("cls");
qingkuang();
system("cls");
break;
default :
printf("\n操作错误,请重新输入\n");
break;
}
}
return 0;
}
/*初始此提款机的用户信息,为取款机写入最原始的用户信息,以便以后默认所以用户信息*/
void atms_ifo()
{
FILE *fp;
ATMSYTEM at;
char ch;
flag = 0;
if(( fp = fopen( "atmsy.txt", "r" ) ) == NULL ) //创建文件
{
//flag = 1;
fp = fopen( "atmsy.txt", "w" );
}
printf("\t\t\t请为该取款机初始用户信息\n");
printf("\t==================================================================\n");
ch = fgetc(fp);
if( ch == EOF )
flag = 1;
while(flag) //如果文件为空,那么便写入初始用户信息
{
printf("\n请输入用户账号(0_退出): ");
scanf("%s",at.zhanghao);
if(at.zhanghao[0] == '0')
return;
printf("请输入用户密码:");
scanf("%s",at.password);
printf("请输入用户金额:");
scanf("%ld",&at.allmoney);
fwrite(&at,sizeof(ATMSYTEM),1,fp);
}
if(flag == 0) //文件不为空,则不执行此函数
return;
fclose(fp);
}
int shuru() //登录atm,如果3次密码错误,那么返回0,否则返回1
{
char mi[10];
int i;
FILE *fp;
ATMSYTEM at;
fp = fopen( "atmsy.txt", "r" );
flag = 0;
printf("\t\t\t\t欢迎进入登录系统\n");
printf("\t==================================================================\n");
while(1) //循环录入用户信息
{
printf("\n请输入账号(0_退出):");
scanf("%s",zhang);
if(zhang[0] == '0')
{
return 0; //退出系统,不进入主菜单
}
while(fread(&at,sizeof(ATMSYTEM),1,fp) != 0) //查找用户账号是否存在
{
if(strcmp(zhang,at.zhanghao) == 0) //该用户存在
{
count = at.allmoney;
flag = 1;
break;
}
}
rewind(fp); //文件指针回到开头
if(flag) //若是该用户存在,那么输入密码
{
for( i = 0; i < 3; i++) //3次机会输入密码
{
printf("\n请输入密码:");
scanf("%s",mi);
while(fread(&at,sizeof(ATMSYTEM),1,fp) != 0) //核对密码
{
if(strcmp(mi,at.password) == 0) //该密码存在
{
return 1;
}
}
rewind(fp); //不停返回文件开头,核对密码
}
printf("当日密码输错3次!你的银行卡已被吞!请携带本人身份证与银行联系!\n");
getch();
return 0;
}
if(flag == 0)
{
printf("输入账号错误,请重新输入\n");
getch();
}
}
}
void cunkuan()
{
FILE *fp;
DEAL d;
char ch;
time_t now; //实例化time_t结构
struct tm *timenow; //实例化tm结构指针
time(&now); //time函数读取现在的时间(国际标准时间非北京时间),然后传值给now
timenow = localtime(&now); //localtime函数把从time取得的时间now换算成你电脑中的时间(就是你设置的地区)
if( (fp = fopen("deal.txt","r")) == NULL )
{
fp = fopen("deal.txt","w");
}
printf("\t\t\t 欢迎进入存款系统\n");
printf("\t==================================================================\n");
while( !feof(fp) ) //这里,将读写头移动到文件结尾,以免覆盖上次的输入
{
fread(&d,sizeof(DEAL),1,fp);
}
flag = 1;
while(flag)
{
printf("\n请输入存入金额:");
scanf("%lf",&d.money);
strcpy(d.zhanghao2,zhang);
strcpy(d.style,"cunkuan"); //写入交易类型
strcpy(d.data,asctime(timenow)); //获取系统当前日期
count += d.money;
d.lastmoney = count;
printf("存款成功……\n");
fwrite(&d,sizeof(DEAL),1,fp);
rewind(stdin);
printf("是否退出(y/n)");
ch = getchar();
if(ch == 'y' || ch == 'Y')
flag = 0;
if(ch == 'n' || ch == 'N')
flag = 1;
}
fclose(fp);
}
void qukuan()
{
FILE *fp;
DEAL d;
char ch;
time_t now;
struct tm *timenow;
time(&now);
timenow = localtime(&now);
if( (fp = fopen("deal.txt","r")) == NULL )
{
fp = fopen("deal.txt","w");
}
while( !feof(fp) )
{
fread(&d,sizeof(DEAL),1,fp);
}
printf("\t\t\t欢迎进入取款系统\n");
printf("\t==================================================================\n");
flag = 1;
while(flag)
{
printf("\n请输入取出金额:");
scanf("%lf",&d.money);
strcpy(d.zhanghao2,zhang);
strcpy(d.style,"qukuan");
strcpy(d.data,asctime(timenow));
count = count - d.money;
d.lastmoney = count;
fwrite(&d,sizeof(DEAL),1,fp);
printf("取款成功……\n");
rewind(stdin);
printf("是否退出(y/n)");
ch = getchar();
if(ch == 'y' || ch == 'Y')
flag = 0;
if(ch == 'n' || ch == 'N')
flag = 1;
}
fclose(fp);
}
void chaxun()
{
printf("\t\t\t欢迎进入查询系统\n");
printf("\t==================================================================\n");
printf("\n目前账户余额为:%ld\n",count);
getch();
}
void zhuanzhang()
{
FILE *fp,*fp1;
ATMSYTEM at;
DEAL d;
char zhang2[10];
time_t now; //实例化time_t结构
struct tm *timenow; //实例化tm结构指针
time(&now); //time函数读取现在的时间(国际标准时间非北京时间),然后传值给now
timenow = localtime(&now); //localtime函数把从time取得的时间now换算成你电脑中的时间(就是你设置的地区)
fp = fopen( "atmsy.txt", "r" );
fp1 = fopen("deal.txt","r");
printf("\t\t\t欢迎进入转账系统\n");
printf("\t==================================================================\n");
while( !feof(fp1) )
{
fread(&d,sizeof(DEAL),1,fp1);
}
while(1)
{
printf("\n请输入你要转账的用户:");
scanf("%s",zhang2);
while( fread(&at,sizeof(ATMSYTEM),1,fp) != 0 ) //寻找用户指定转账用户
{
if(strcmp(zhang2,at.zhanghao) == 0)
flag = 1;
}
if(flag)
{
printf("\n请输入你要转账的金额:");
scanf("ld",d.money);
strcpy(d.zhanghao2,zhang2);
strcpy(d.style,"zhuanzhang");
strcpy(d.data,asctime(timenow));
count = count - d.money;
d.lastmoney = count;
printf("\n转账成功……");
fwrite(&d,sizeof(DEAL),1,fp1);
rewind(stdin);
return;
}
else
{
printf("\n没有这个用户,请重新输入\n" );
}
}
fclose(fp);
fclose(fp1);
}
void qingkuang()
{
FILE *fp;
DEAL d;
printf("\t\t\t欢迎进入交易情况查询系统\n");
printf("\t==================================================================\n");
fp = fopen("deal.txt","r");
printf("你查询的账号为:%s",zhang);
while(fread(&d,sizeof(DEAL),1,fp) != 0)
{
printf("\n交易对象:%s\n交易日期:%s\n交易类型:%s\n交易金额:%ld\n余额:%ld",d.zhanghao2,d.data,d.style,d.money,d.lastmoney);
}
rewind(stdin);
fclose(fp);
}