ATM提款机实验
实验目的及要求:
熟悉运用C语言的知识解决实际问题,要求使用数组、结构体、函数模块等C语言基础知识来完成ATM提款机的基本功能;
实验内容:
实现ATM提款机基本要求:欢迎界面、用户注册、用户登录、用户功能(包括存款、取款、修改密码等功能),用户退出。
- 欢迎界面
- 用户注册
- 用户登录
- 功能模块
- 退出
参考程序1:
#include<stdio.h>
#include<conio.h>
#include<stdlib.h>
#include<string.h>
struct users
{
char username[20];
char password[7];
float money;
int account;//账号
}user;//定义结构体
FILE*fp;//定义数据结构
int /*注销*/logout() //注销后进入主界面
{
int isLogout;//标记注销
char choise;
system("cls");//清屏
printf("\n\n\n\t\t\t\t\t\t确定注销登陆? Y/N:");
fflush(stdin);
scanf("%c",&choise);
if(choise=='y'||choise=='Y')
{
isLogout=1;
printf("\n\n\n\t\t\t\t\t\t按任意键继续");
}
else
{isLogout=0;}
return isLogout;
}
void /*等待*/loading()//等待界面
{
int t=1;
for(int i=1;i<=100;i++)
{
printf("\n\n\n\t\t\t\t\t\t欢迎使用ATM请稍后");
printf("%d%%",i);
for( int k=1;k<=t;k++)
{
printf(".");
}
t++;
if(t==10)t=1;
for(int j=0;j<9000000;j++);//调整速度
system("cls");//清屏
}
}
void /*隐秘*/inputPass(char pass[])//隐藏密码
{
int i=0;//密码字符数组最后一位
char ch;
while(1)//获取
{
ch=getch();
if(ch!='\r') //判断输入的是否是回车
{
if(ch!='\b')//将输入的字符存入到数组的第0位
{
pass[i]=ch;
i++;
printf("*");
}
else
{
if(i>0)
{
i--;
printf("\b \b");//删除*
}
}
}
else
{
break;
}
}
pass[i]='\0';
}
void /*注册*/regist()//注册用户
{
fp=fopen("atm.txt","ab+");
if(fp==NULL)
{
printf("\n\n\t\t\t\t\t\t文件操作失败");
return;
}
system("cls");//清屏
char rePassword[7];
struct users t;//上一个注册的用户
printf("\n\n\t\t\t\t\t\t 注册界面");
printf("\n\n\n\t\t\t\t\t\t请输入用户名:");//默认不重复
fflush(stdin);//清除字符缓冲区
gets(user.username);
while(1)
{
while(1)
{
printf("\n\n\n\t\t\t\t\t\t请输入六位密码:");
fflush(stdin);//清除字符缓冲区
inputPass(user.password);
//判断密码长度
int length=strlen(user.password);
if(length==6)
{
break;
}
else
{
system("cls");//清屏
printf("\n\n\t\t\t\t\t\t\t尊敬的%s",user.username);
printf("\n\n\n\t\t\t\t\t\t您的输入有误,请您输入6位数密码");
}
}
printf("\n\n\n\t\t\t\t\t\t请确认密码:");
inputPass(rePassword);
if(strcmp(user.password,rePassword)==0)
{
rewind(fp);//将指针位置定位到文件的开头
if(fread(&t,sizeof(t),1,fp)==1)
{
//此时指针指向的是文件的末尾
//1.将指针向前移动sizeof(user)单位
fseek(fp,-sizeof(t),2);
//2.从此时的指针位置开始读取sizeof(user)单位
//3.将读取出的数据赋值给一个临时变量(上一个注册的用户)
fread(&t,sizeof(t),1,fp);
//4.取出这个临时变量的帐号
user.account=t.account+1;
}
else
{
user.account=20180001;
}
fseek(fp,0,2);
fwrite(&user,sizeof(user),1,fp);
system("cls");//清屏
printf("\n\n\n\t\t\t\t\t\t注册成功");
printf("\n\n\n\t\t\t\t\t\t信息如下:");
printf("\n\n\n\t\t\t\t\t\t用户名为:%s",user.username);
printf("\n\n\n\t\t\t\t\t\t账号是:%d",user.account);
fclose(fp);
getch();
break;
}
else
{
printf("\n\n\t\t\t\t\t\t注册两次密码不一致 请重新输入:");
}
}
}
void /*存款*/saveMoney()
{
int money;
system("cls");//清屏
printf("\n\n\n\t\t\t\t\t\t您正在使用存款功能");
printf("\n\n\n\t\t\t\t\t\t请输入存款金额:");
scanf("%d",&money);
if(money>10000)
{
printf("\n\n\n\t\t\t\t\t\t操作失败! 单次存款不能超过一万元");
}
else
{
user.money=user.money+money;
fseek(fp,-sizeof(user),1);
fwrite(&user,sizeof(user),1,fp);
printf("\n\n\n\t\t\t\t\t\t操作成功");
}
}
void /*退出*/quit() //退出程序函数
{
char choise;
printf("\n\n\n\t\t\t\t\t\t确定退出系统?Y/N");
fflush(stdin);
scanf("%c",&choise);
if(choise=='y'||choise=='Y')
exit(0);
else
return;
}
void /*注销*/selectMoney()
{
system("cls");//清屏
printf("\n\n\n\t\t\t\t\t\t您正在使用查询余额功能");
printf("\n\n\n\t\t\t\t\t\t您的余额是:");
printf("%.2fRMB",user.money);
}
void /*取款*/getMoney()
{
int money;
system("cls");//清屏
printf("\n\n\n\t\t\t\t\t\t您正在使用取款功能");
printf("\n\n\n\t\t\t\t\t\t请输入取款金额:");
scanf("%d",&money);
if(money>user.money)
{
printf("\n\n\n\t\t\t\t\t\t操作失败,您的余额不足");
}
else
{
user.money=user.money-money;//修改内存
fseek(fp,-sizeof(user),1);
fwrite(&user,sizeof(user),1,fp);
printf("\n\n\n\t\t\t\t\t\t操作成功");
}
}
void /*转账*/transferMoney()
{
int account;
int i;
int f=0;
int money;
int n=ftell(fp);
struct users t;//临时
system("cls");
printf("\n\n\n\n\t\t\t\t您正在使用转账功能");
printf("\n\n\n\t\t\t\t\t\t请输入对方账号:");
scanf("%d",&account);
fseek(fp,0,2);
int length=ftell(fp);
rewind(fp);
while(ftell(fp)<length){
fread(&t,sizeof(t),1,fp);
if(t.account==account){
f=1;
break;
}
}
if(f==1){
printf("\n\n\n\t\t\t\t\t\t请输入转账金额:");
scanf("%d",&money);
if(money>user.money){
printf("\n\n\n\t\t\t\t\t\t操作失败,余额不足!");
}else{
user.money=user.money-money;
t.money=t.money+money;
fseek(fp,-sizeof(t),1);
fwrite(&t,sizeof(t),1,fp);
fseek(fp,n-sizeof(user),0);
fwrite(&user,sizeof(user),1,fp);
printf("\n\n\n\t\t\t\t\t\t操作成功!");
}
}else{
printf("\n\n\n\t\t\t\t\t\t对方账号不存在");
}
}
void /*改密*/updatePassword() //修改密码
{
char newPassword[7];
char reNewPassword[7];
char oldPassword[7];
int i;
for(i=1;i<=3;i++){
system("cls");
printf("\n\n\n\t\t\t\t\t\t您正在使用修改密码功能");
printf("\n\n\n\t\t\t\t\t\t请输入原始密码:");
fflush(stdin);
inputPass(oldPassword);
if(strcmp(user.password,oldPassword)==0){
break;
}else{
if(i==3){
printf("\n\n\n\t\t\t\t\t\t原始密码输入错误三次,已锁定!");
return;
}
printf("\n\n\n\t\t\t\t\t\t原始密码输入错误,请重新输入!");
printf("\n\n\n\t\t\t\t\t\t按任意键继续...");
getch();
}
}
while(1){
printf("\n\n\n\t\t\t\t\t\t请输入新密码:");
fflush(stdin);
inputPass(newPassword);
if(strlen(newPassword)!=6){
printf("\n\n\n\t\t\t\t\t\t密码输入有误,请输入6位密码!");
continue;
}
printf("\n\n\n\t\t\t\t\t\t请再次输入新密码:");
fflush(stdin);
inputPass(reNewPassword);
if(strcmp(newPassword,reNewPassword)==0){
strcpy(user.password,newPassword);//修改的内存
fseek(fp,-sizeof(user),1);
fwrite(&user,sizeof(user),1,fp);
printf("\n\n\n\t\t\t\t\t\t密码修改成功");
break;
}else{
printf("\n\n\n\t\t\t\t\t\t两次密码输入不一致,请重新输入");
}
}
}
void /*服务*/service() //服务函数
{
char choise;
while(1)
{
system("cls");
int isLogout;//标记注销
printf("\n\n\n\t\t\t\t\t\t服务界面");
printf("\n\n\n\t\t\t\t\t\tA.查询余额");
printf("\n\n\n\t\t\t\t\t\tB.存款");
printf("\n\n\n\t\t\t\t\t\tC.取款");
printf("\n\n\n\t\t\t\t\t\tD.转账");
printf("\n\n\n\t\t\t\t\t\tE.修改密码");
printf("\n\n\n\t\t\t\t\t\tF.注销");
printf("\n\n\n\t\t\t\t\t\tG.退出系统");
printf("\n\n\n\t\t\t\t\t\t请选择:");
fflush(stdin);
scanf("%c",&choise);
switch(choise)
{
case 'a':
case 'A':
printf("\n\n\n\t\t\t\t\t\t正在查询余额");
selectMoney();
break;
case 'b':
case 'B':
printf("\n\n\n\t\t\t\t\t\t正在存款");
saveMoney();
break;
case 'c':
case 'C':
getMoney();
break;
case 'd':
case 'D':
transferMoney();
break;
case 'e':
case 'E':
updatePassword();
break;
case 'f':
case 'F':
isLogout=logout();
if(isLogout==1)
{
return;
}
break;
case 'g':
case 'G':
quit();
break;
default:
printf("\n\n\n\t\t\t\t\t\t输入有误,请重新输入:");
break;
}
printf("\n\n\n\t\t\t\t\t\t按任意键继续");
getch();
}
}
void /*登录*/login(){
int account;
int length;
int f=0;
char password[7];
fp=fopen("atm.txt","rb+");
if(fp==NULL)
{
printf("\n\n\n\t\t\t\t\t\t文件丢失");
return;
}
system("cls");
printf("\n\n\n\t\t\t\t\t\t登录界面");
printf("\n\n\n\t\t\t\t\t\t请输入账号:");
scanf("%d",&account);
fseek(fp,0,2);
length=ftell(fp);
rewind(fp);
while(ftell(fp)<length){//判断指针后是否还有数据
fread(&user,sizeof(user),1,fp);
//将输入的账号与文件中的对比
if(user.account==account){
f=1;
break;
}
}
if (f==1){
for(int j=1;j<=3;j++){
printf("\n\n\n\t\t\t\t\t\t请输入密码");
fflush(stdin);
inputPass(password);
if(strcmp(user.password ,password)==0){//密码正确
printf("\n\n\n\t\t\t\t\t\t登录成功");
printf("\n\n\n\t\t\t\t\t\t即将进入服务界面,按任意键继续....");
getch();
service();
break;
}else{
if(j==3){
printf("\n\n\n\t\t\t\t\t\t密码输入错误三次,已锁定....");
getch();
break;
}
printf("\n\n\n\t\t\t\t\t\t密码错误,请重新输入");
}
}
}else{
printf("\n\n\n\t\t\t\t\t\t账号不存在! 按任意键继续: ");
}
}
void main()
{
//loading();
while(1)
{
printf("\n\n\n\n\t\t\t\t\t\ta.用户注册");
printf("\n\n\t\t\t\t\t\tb.用户登陆");
printf("\n\n\t\t\t\t\t\tc.退出系统");
printf("\n\n\t\t\t\t\t\t请输入一个选项:");
char choise;
fflush(stdin);//清除字符缓冲区
scanf("%c",&choise);
if(choise=='a'||choise=='A')
{
printf("\n\n\n\t\t\t\t\t\t正在注册......");
regist();
}
else if(choise=='b'||choise=='B')
{
printf("\n\n\n\t\t\t\t\t\t正在登陆......");
login();
}
else if(choise=='c'||choise=='C')
quit();
else
printf("\n\n\n\t\t\t\t\t\t您的输入有误......");
getch();
system("cls");//清屏
}
//quiting();
}
参考程序2:
#include <stdio.h>
#include <stdlib.h>
#include <conio.h>
#include <string.h>
void regist();
void login();
void quite();
void inputpassword(char mima[]);
void service();
struct bank
{
char name[20];
char password[7];
int account;
double money;
}kehu;
int find;
int a[10];
struct bank one;
FILE *fp;
void main()
{
int i;
int t=1;
for(i=0;i<100;i++)
{
printf("\t\t\t\t\t\t欢迎使用青软ATM系统\n");
printf("\t\t\t\t\t\t正在进入主界面,请稍等");
int j;
for(j=1;j<t;j++)
{
printf(".");
}
t++;
if(t==10)
{
t=1;
}
printf("\n\t\t\t\t\t\t%d%%",i);
system("cls");
}
while(1)
{
printf("\t\t\t\t\t\t服务类型: \n");
printf("\t\t\t\t\t\t[a]: 用户注册\n");
printf("\t\t\t\t\t\t[b]: 用户登录\n");
printf("\t\t\t\t\t\t[c]: 退出系统\n");
printf("\t\t\t\t\t\t请选择服务: ");
fflush(stdin);
char xz;
scanf("%c",&xz);
if(xz=='a'||xz=='A')
{
regist();
} else if (xz=='b'||xz=='B')
{
login();
} else if(xz=='c'||xz=='C')
{
quite();
} else
{
printf("输入有误,请重新输入");
}
getch();
system("cls");
}
}
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");
}
void regist()
{
fp=fopen("atm.txt","ab+");
if(fp==NULL)
{
printf("\n\t\t\t文件打开失败!");
return;
}
system("cls");
printf("\t\t\t现在执行的是注册函数的使用\n");
printf("\t\t请输入用户名: ");
fflush(stdin);
gets(kehu.name);
char password1[7];
while(1)
{
while(1)
{
printf("\n\n\t\t请输入密码:");
fflush(stdin);
inputpassword(kehu.password);
int n=strlen(kehu.password);
if(n==6)
{
break;
}else
{
printf("\n\t\t密码必须为6位!");
}
}
printf("\n\t\t请输入正确密码!: ");
fflush(stdin);
inputpassword(password1);
if(strcmp(kehu.password,password1)==0)
{
break;
}else{
printf("\n\n\t\t两次密码必须相同!");
}
}
rewind(fp);
struct bank k;
if(fread(&k,sizeof(struct bank),1,fp)==1)
{
fseek(fp,-sizeof(k),2);
fread(&k,sizeof(k),1,fp);
kehu.account=k.account+1;
}else{
kehu.account=20170001;
}
kehu.money=0;
fseek(fp,0,2);
fwrite(&kehu,sizeof(struct bank),1,fp);
fclose(fp);
printf("\n\n\t\t开户成功! ");
printf("\n\t\t您的账号为%d!",kehu.account);
printf("\n\t\t现在请您重新登录!");
}
void searchmoney()
{
system("cls");
printf("您现在使用的是查询余额功能: \n");
printf("\n\n\t\t您的余额是%0.2lf",one.money);
}
void savemoney()
{
system("cls");
double inmoney;
printf("请您选择您要存款的金额 \n");
scanf("%lf",&inmoney);
int q;
int r=1;
for(q=0;q<100;q++)
{
int w;
for(w=1;w<r;w++)
{
printf(".");
}
r++;
if(r==10)
{
r=1;
}
printf("\n\t\t\t\t\t\t正在存款%d%%",q);
system("cls");
}
one.money=one.money+inmoney;
fseek(fp,-sizeof(one),1);
fwrite(&one,sizeof(one),1,fp);
printf("\n\n\t\t\t\t\t\t您已存款成功!");
}
void withdrawalmoney()
{
system("cls");
double outputsomemoney;
printf("请您选择您要取款的金额 \n");
scanf("%lf",&outputsomemoney);
if(one.money<outputsomemoney){
printf("您的余额已不足,请您注意!");
}else {
int q;
int r=1;
for(q=0;q<100;q++)
{
int w;
for(w=1;w<r;w++)
{
printf(".");
}
r++;
if(r==10)
{
r=1;
}
printf("\n\t\t\t\t\t\t正在取款%d%%",q);
system("cls");
}
one.money=one.money-outputsomemoney;
fseek(fp,-sizeof(one),1);
fwrite(&one,sizeof(one),1,fp);
printf("\n\n\t\t\t\t\t\t您已取款成功!请点清钞票!");
printf("\n\n\t\t\t\t\t\t您现在的余额为%lf",one.money);
}
}
void transfermoney()
{
system("cls");
int duifang;
int qian;
fflush(stdin);
printf("\n\n\n\t\t您现在使用的是转账功能");
printf("\n\t\t\t请输入您要转账的账户:");
scanf("%d",&duifang);
int n=ftell(fp);
rewind(fp);
int flag=0;
struct bank temp;
while(fread(&temp,sizeof(temp),1,fp)==1)
{
if(temp.account==duifang)
{
flag=1;
break;
}
}
if(flag==1)
{
printf("请输入转账金额:");
scanf("%d",&qian);
if(one.money>=qian)
{
int q;
int r=1;
for(q=0;q<100;q++)
{
int w;
for(w=1;w<r;w++)
{
printf(".");
}
r++;
if(r==10)
{
r=1;
}
printf("\n\t\t\t\t\t\t正在转账,请稍后!%d%%",q);
system("cls");
}
temp.money=temp.money+qian;
fseek(fp,-sizeof(temp),1);
fwrite(&temp,sizeof(temp),1,fp);
one.money=one.money-qian;
fseek(fp,n-sizeof(one),0);
fwrite(&one,sizeof(one),1,fp);
printf("\n\t\t\t\t转账成功!");
}else{
printf("\n\t\t\t\t您的余额已不足!");
}
}
}
void xiugai(){
system("cls");
printf("\n\n\t\t 现在进行的是修改密码功能\n");
char oldpassword[7];
char newpassword[7];
char newpassword1[7];
int i;
for(i=0;i<3;i++){
printf("\n\t\t\t 请输入旧密码:\n");
inputpassword(oldpassword);
if(strcmp(oldpassword,one.password)==0){
printf("\n\t\t\t 输入成功!\n");
break;
}else{
printf("\n\t\t\t 密码输入有误,请重新输入!\n");
}
}
if(i<3){
while(1){
printf("\n\t\t\t 请输入您的新密码:\n");
inputpassword(newpassword);
printf("\n\t\t\t 请输入您的确认密码:\n ");
inputpassword(newpassword1);
if(strcmp(newpassword,newpassword1)==0){
strcpy(one.password,newpassword);
fseek(fp,-sizeof(one),1);
fwrite(&one,sizeof(one),1,fp);
printf("\n\t\t\t 密码修改成功!");
break;
}else{
printf("\n\t\t\t 两次密码输入不一致!");
}
}
}else{
printf("\n\t\t\t 密码输入错误!");
}
}
int zhuxiaozhanghao()
{
system("cls");
int zhuxiaoxitong;
char sf;
printf("你要注销的账号是%d",one.account);
printf("你是否要对此账号进行注销?\n\n\t\t请您选择:注销(Y)or不注销(N):");
fflush(stdin);
scanf("%c",&sf);
if(sf=='y'||sf=='Y')
{
printf("正在为您注销!\n",one.account);
zhuxiaoxitong=1;
}else{
printf("不注销系统!\n",one.account);
}
return zhuxiaoxitong;
}
void service()
{
while(1){
system("cls");
printf("\n\n\n\t\t\t\t\t\t现在是服务系统,本系统有以下服务");
printf("\n\t\t\t\t\t\t[a] 查询余额\n");
printf("\n\t\t\t\t\t\t[b] 存款服务\n");
printf("\n\t\t\t\t\t\t[c] 转账服务\n");
printf("\n\t\t\t\t\t\t[d] 取款服务\n");
printf("\n\t\t\t\t\t\t[e] 修改密码\n");
printf("\n\t\t\t\t\t\t[f] 注销 \n");
printf("\n\t\t\t\t\t\t[g] 退出系统\n");
char e;
printf("\n\t\t\t\t\t\t您要选择的服务是:");
fflush(stdin);
scanf("%c",&e);
switch(e)
{ case'A':
case'a': searchmoney() ;break;
case'B':
case'b': savemoney() ;break;
case'C':
case'c': transfermoney() ;break;
case'D':
case'd': withdrawalmoney() ;break;
case'E':
case'e': xiugai() ;break;
case'F':
case'f': {int zhuxiaoxitong=zhuxiaozhanghao();{if(zhuxiaoxitong==1) return;}break;}
case'G':
case'g': quite();break;
}
printf("\n\n\n\t\t\t\t按任意键继续......\n");
getch();
}
}
void login()
{
fp=fopen("atm.txt","rb+");
if(fp==NULL)
{
printf("\n\n\n\t\t\t\t文件打开失败!");
return;
}
system("cls");
printf("\n\t\t\t\t\t\t现在执行的是登录函数的使用\n");
int zhanghao;
printf("\n\t\t\t\t\t\t请输入账号:");
scanf("%d",&zhanghao);
int flag=0;
while(fread(&one,sizeof(one),1,fp)==1)
{
if(zhanghao==one.account){
flag=1;
break;
}
}
char password2[7];
if(flag==1){
int h;
for(h=0;h<3;h++){
printf("\n\t\t\t\t\t\t请输入密码:");
fflush(stdin);
inputpassword(password2);
if(strcmp(password2,one.password)==0)
{
printf("\n\t\t\t\t\t\t登陆成功!");
service();
break;
}else{
printf("密码不正确!");
}
}
if(h==3){
printf("\n\t\t\t您的密码三次输入有误,返回");
}
}else{
printf("无此账号!");
}
fclose(fp);
}
void quite()
{
system("cls");
printf("\t\t\t现在执行的是退出函数的使用\n");
exit(0);
}
自己程序:修改与改进,并分组



min程序:
int main()
{
interface();
return 0;
}
ATM.h文件
#ifndef ATM_H_INCLUDE
#define ATM_H_INCLUDE
#endif // HITDOUDOU_H_INCLUDE
#include <stdio.h>
#include <stdlib.h>
#include <conio.h>
struct bank//定义用户结构体
{
int account;//银行账号
char name[20];//名字
int password[7];//密码
double money;//余额
};
struct bank user;
FILE *fp;
void interface();
void regist();
void land();
void inputpass(char pass[]);
void quite();
void searchmoney();
void savemoney();
void getmoney();
void transfermoney();
void updatepassword();
function.c文件
#include"ATM.h"
void interface()//界面函数
{
printf("\t\t\t\★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★\t\n");
printf("\t\t\t\t\t\t欢迎使用ATM存取款机\t\n");
printf("\t\t\t★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★\t\n");
printf("\t\t\t\t\t正在进入系统主界面,正在加载请稍后......");
//延时程序
int i, j, k;
for (i = 1; i < 1000; i++)
{
for (j = 1; j < 1000; j++)
{
for (k = 1; k < 1000; k++)
{
}
}
}
system("cls");//清屏
//下面是闪烁加载,有百分号
/*int t=1;
for(int i=0;i<100;i++)
{
printf("\t\t\t\★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★\t\n");
printf("\t\t\t\t\t\t欢迎使用ATM存取款机\t\n");
printf("\t\t\t★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★\t\n");
printf("\t\t\t\t\t\t正在进入系统主界面,正在加载请稍后");
for(int j=1;j<t;j++)
{
printf(".");
}
t++;
if(t==10)
{
t=1;
}
printf("\n\t\t\t\t\t\t%d%%",i);
system("cls");
}*/
while(1)
{
printf("\t\t\t★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★\t\n");
printf("\t\t\t\t\t\t请选择你的服务类型\t\n");
printf("\t\t\t★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★\t\n");
printf("\t\t\t★\t 1.注册用户 2.用户登陆 0.退出系统 \t★\n");
printf("请输入操作指令: ");
int a;
scanf("%d",&a);
if(a==1)
{
printf("\n\n\n\t\t\t\t\t\t正在注册..........");
regist();
}
else if (a==2)
{
printf("\n\n\n\t\t\t\t\t\t正在登陆..........");
land();
}
else if(a==0)
{
quite();
}
else
{
printf("您的输入有误,请重新输入");
}
getch();//等待函数,要记得加头文件
system("cls");//清屏
}
}
void quite()//退出系统程序
{
char b;
printf("\n\n\n\t\t\t\t\t\t确定退出系统?Y/N ");
fflush(stdin);
scanf("%c",&b);
if(b=='y'||b=='Y')
exit(0);
else
return;
}
void searchmoney()//查询余额
{
system("cls");
printf("您现在使用的是查询余额功能: \n");
printf("\n\n\t\t您的余额是%0.2lf",user.money);
}
void savemoney()//存款
{
system("cls");
double inmoney;
printf("请您选择您要存款的金额 \n");
scanf("%lf",&inmoney);
int q;
int r=1;
for(q=0;q<100;q++)
{
int w;
for(w=1;w<r;w++)
{
printf(".");
}
r++;
if(r==10)
{
r=1;
}
printf("\n\t\t\t\t\t\t正在存款%d%%",q);
system("cls");
}
user.money=user.money+inmoney;
fseek(fp,-sizeof(user),1);
fwrite(&user,sizeof(user),1,fp);
printf("\n\n\t\t\t\t\t\t您已存款成功!");
printf("\n\n\t\t\t\t\t\t您现在的余额为%lf",user.money);
}
void getmoney()//取款
{
system("cls");
double outputmoney;
printf("请您选择您要取款的金额 \n");
scanf("%lf",&outputmoney);
if(user.money<outputmoney){
printf("您的余额已不足,请重新输入!");
}
else
{
int q;
int r=1;
for(q=0;q<100;q++)
{
int w;
for(w=1;w<r;w++)
{
printf(".");
}
r++;
if(r==10)
{
r=1;
}
printf("\n\t\t\t\t\t\t正在取款%d%%",q);
system("cls");
}
user.money=user.money-outputmoney;
fseek(fp,-sizeof(user),1);
fwrite(&user,sizeof(user),1,fp);
printf("\n\n\t\t\t\t\t\t您已取款成功!请拿好你的钱!");
printf("\n\n\t\t\t\t\t\t您现在的余额为%lf",user.money);
}
}
void transfermoney()//转账函数
{
system("cls");
int otheraccount;
int somemoney;
fflush(stdin);
printf("\n\n\n\t\t您现在使用的是转账功能");
printf("\n\t\t\t请输入您要转账的账户:");
scanf("%d",&otheraccount);
fseek(fp,0,2);//定位到光标到文件的末尾
int n=ftell(fp);//获得当前位置相对于文件首的位移
rewind(fp);//定位到光标在文件的开头
int flag=0;
struct bank temp;
while(fread(&temp,sizeof(temp),1,fp)==1)
{
if(temp.account==otheraccount)
{
flag=1;
break;
}
}
if(flag==1)
{
printf("请输入转账金额:");
scanf("%d",&somemoney);
if(user.money>=somemoney)
{
int q;
int r=1;
for(q=0;q<100;q++)
{
int w;
for(w=1;w<r;w++)
{
printf(".");
}
r++;
if(r==10)
{
r=1;
}
printf("\n\t\t\t\t\t\t正在转账,请稍后!%d%%",q);
system("cls");
}
temp.money=temp.money+somemoney;
fseek(fp,-sizeof(temp),1);
fwrite(&temp,sizeof(temp),1,fp);
user.money=user.money-somemoney;
fseek(fp,n-sizeof(user),0);//
fwrite(&user,sizeof(user),1,fp);
printf("\n\t\t\t\t转账成功!");
}
else{
printf("\n\t\t\t\t转账失败,您的余额不足!");
}
}
else{
printf("\n\n\n\t\t\t\t\t\t对方账号不存在");
}
}
void updatepassword()//修改密码
{
char newpassword[7];//新密码
char renewpassword[7];//再次输入新密码
char oldpassword[7];//原来密码
int i;
for(i=1;i<=3;i++){
system("cls");
printf("\n\n\n\t\t\t\t\t\t您正在修改密码");
printf("\n\n\n\t\t\t\t\t\t请输入原始密码:");
inputpass(oldpassword);
if(strcmp(user.password,oldpassword)==0)//如果输入的密码和原来密码相同
{
printf("\n\n\n\t\t\t\t\t\t输入成功!\n");
break;
}else{
if(i==3){
printf("\n\n\n\t\t\t\t\t\t原始密码输入错误三次,您暂时不能修改,正在返回!");
return;
}
printf("\n\n\n\t\t\t\t\t\t原始密码输入错误,请重新输入!");
printf("\n\n\n\t\t\t\t\t\t按任意键继续...");
getch();
}
}
while(1)
{
printf("\n\n\n\t\t\t\t\t\t请输入您的新密码:");
inputpass(newpassword);
if(strlen(newpassword)!=6){
printf("\n\n\n\t\t\t\t\t\t密码输入有误,请输入6位密码!");
continue;
}
printf("\n\n\n\t\t\t\t\t\t请再次输入新密码:");
inputpass(renewpassword);
if(strcmp(newpassword,renewpassword)==0)
{
strcpy(user.password,newpassword);
fseek(fp,-sizeof(user),1);
fwrite(&user,sizeof(user),1,fp);
printf("\n\n\n\t\t\t\t\t\t密码修改成功!");
break;
}else{
printf("\n\n\n\t\t\t\t\t\t两次密码输入不一致!");
}
}
}
int chooseMenu()//选择功能函数
{
int Menu;
while(1) //循环进行选择操作
{
system("cls");
printf("\n\t\t\t★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★");
printf("\n\t\t\t★ 1.查询余额 2.存款 3.取款 \t\t\t★");
printf("\n\t\t\t★ 4.转账 5.修改密码 0.退出系统 \t\t\t★");
printf("\n\t\t\t★ \t\t\t★");
printf("\n\t\t\t\★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★\t\n");
printf("请输入操作指令: ");
scanf("%d",&Menu);
switch(Menu) //输入的指令要在0到7之间;
{
case 1:
printf("\n\n\n\t\t\t\t\t\t正在查询余额");
searchmoney();
break;
case 2:
printf("\n\n\n\t\t\t\t\t\t正在存款");
savemoney();
break;
case 3:
getmoney();
break;
case 4:
transfermoney();
break;
case 5:
updatepassword();
break;
case 0:
quite();
break;
default:
printf("\n\n\n\t\t\t\t\t\t输入错误,请重新选择!\n");
}
printf("\n\n\n\t\t\t\t\t\t按任意键继续......\n");
getch();
}
return Menu;
}
void inputpass(char pass[])//输入密码函数,要包含隐藏密码
{
int i=0;//密码数组的第一位
char ch;
while(1)
{
ch=getch();//这个函数是一个不回显函数,当用户按下某个字符时,函数自动读取,无需按回车
if(ch!='\r') //判断是否输入回车
{
if(ch!='\b')//\b是退格的意思,将输入的字符存入到数组的第0位
{
pass[i]=ch;
i++;
printf("*");
}
else
{
if(i>0)
{
i--;
printf("\b \b");//删除*
}
}
}
else
{
break;
}
}
pass[i]='\0';
}
void regist()//注册函数
{
fp=fopen("ATM.txt","ab+");
if(fp==NULL)//这个if语句可以不要
{
printf("\n\n\t\t\t\t\t\t文件请求失败");
return;
}
system("cls");//清屏
printf("\n\n\t\t\t\t\t\t 注册界面");
printf("\n\n\n\t\t\t\t\t\t请您输入要注册的用户名: ");
scanf("%s",user.name);
char password1[7];
while(1)
{
while(1)
{
printf("\n\n\n\t\t\t\t\t\t请输入六位密码:");
inputpass(user.password);
int length=strlen(user.password);
if(length==6)//判断密码的长度
{
break;
}
else
{
system("cls");//清屏
printf("\n\n\t\t\t\t\t\t\t尊敬的%s客户",user.name);
printf("\n\n\n\t\t\t\t\t\t您的输入密码有误,请您重新输入6位数密码");
}
}
printf("\n\n\n\t\t\t\t\t\t请确认输入密码:");
inputpass(password1);
if(strcmp(user.password,password1)==0)
{
rewind(fp);//定位到光标在文件的开头
struct bank temp;
if(fread(&temp,sizeof(temp),1,fp)==1)//依次读取结构体的长度
{
fseek(fp,-sizeof(temp),2);//指针指向的是文件的末尾,然后指针向前移动sizeof(temp)单位
fread(&temp,sizeof(temp),1,fp);//从此时的指针位置开始读取sizeof(temp)单位,将读取出的数据临时赋值给temp
user.account=temp.account+1;//取出这个临时变量的帐号,再加一
}
else
{
user.account=10000;
}
fseek(fp,0,2);//将指针再指向数据的末端
fwrite(&user,sizeof(user),1,fp);//接着把取出的数据写入到末端以后
system("cls");//清屏
printf("\n\n\n★★★★★★★★★★★★★★★★★★★★★★★注册成功★★★★★★★★★★★★★★★★★★★★★★");
printf("\n\n\n\t\t\t\t\t注册信息如下,请牢记:");
printf("\n\n\n\t用户名:%s",user.name);
printf("\n\n\n\t生成的账号:%d",user.account);
printf("\n\n\n\t请您保存好此账号并用此账号重新登录!");
fclose(fp);
break;
}
else
{
printf("\n\n\t\t两次密码必须相同!");
}
}
}
void land()//登陆函数
{
int account;
fp=fopen("ATM.txt","rb+");//打开文件
if(fp==NULL)
{
printf("\n\n\n\t\t\t\t\t\t文件丢失");
return;
}
system("cls");
printf("\n\n\t\t\t★★★★★★★★★★★★★★★★★★登陆界面★★★★★★★★★★★★★★★★★★★★");
printf("\n\n\n\t\t\t\t\t\t请输入账号:");
scanf("%d",&account);
int flag=0;
while(fread(&user,sizeof(user),1,fp)==1)
{
if(account==user.account)
{
flag=1;
break;
}
}
char password2[7];
if(flag==1){
int h;
for(h=0;h<3;h++)
{
printf("\n\t\t\t\t\t\t请输入密码:");
inputpass(password2);
if(strcmp(password2,user.password)==0)
{
system("cls");
printf("\n\n\t\t\t★★★★★★★★★★★★★★★★★登陆成功★★★★★★★★★★★★★★★★");
chooseMenu();
break;
}else
{
printf("\n\n\t\t\t\t\t\t密码不正确,请重新输入密码\n");
}
}
if(h==3)
{
printf("\n\n\t\t\t\t\t\t您的密码三次输入有误,返回");
}
}
else{
printf("账号不存在!");
}
fclose(fp);
}
本文作者为一木,转载请注明。
这个是c语言吗,感觉很像啊 ,哈哈
@VPS234是的呀[aru_1]