标签:
/* *Copyright(c) 2015/6/3 CSDN博客 *All rights reserved. *文件名称:main.c *作 者:金叶 *完成日期:2015/6/3 *版本号:V1.0 *问题描述:小型银行应用程序 *程序输出: */ #include <stdio.h> #include "bank.h" int main() { if (ipass()==1) { downdate(); work(); update(); } return 0; }
#ifndef BANK_H_INCLUDED #define BANK_H_INCLUDED #define upNum 2000 int ipass(); //银行员工密码确认 void downdate(); //加载数据 int iChoose(); //银行员工选择功能 int search(int id); //用于查找账户对应的下标 int inputpass(); //用于输入密码 void work(); //主菜单 void open_account(); //1开户 void cancellation(); //2注销 void deposit(); //3存款 void withdraw_money(); //4取款 void query(); //5查询 void transfer_accounts(); //6转账 void loss(); //7挂失 void unloss(); //8解挂 void change(); //9该密 void update(); //保存数据,退出 struct bank { int num; char name[20]; int password; double balance; int state; }; struct bank user[upNum]; int N; //银行用户实际人数 #endif // BANK_H_INCLUDED
#include <stdio.h> #include <string.h> #include <stdlib.h> #include <ctype.h> #include <conio.h> #include "bank.h" int ipass() { int i,j=3,k; int right=0; char ch; char worker[20][20]; char ipass[20][20]; char worker1[20]; char ipass1[20]; FILE *fp; printf("+-------------------+\n"); printf("+ 欢迎光临CSDN银行 +\n"); printf("+-------------------+\n"); if ((fp=fopen("worker.txt","r"))==NULL) { printf("worker file cannot open!"); exit(0); } for (i=0; i<20; i++) { fscanf(fp,"%s %s",worker[i],ipass[i]); //读入工作人员用户名 } fclose(fp); do { printf("请输入业务员的用户名:"); scanf("%s",worker1); printf("请输入密码:"); i=0; while((ch=getch())!='\r') { ipass1[i++]=ch-1; //文件保存密码使用加密 putchar('*'); } ipass1[i]='\0'; fflush(stdin); printf("\n"); for (k=0; k<20; k++) { if (strcmp(worker1,worker[k])==0&&strcmp(ipass1,ipass[k])==0) right=1; } if (right==1) { break; } else { j--; if (j>0) { printf("超过3次将退出,你还可以尝试%d次!\n", j); } else { printf("对不起,你不能进入系统\n"); exit(0); } } } while (j); return right; }
#include <stdio.h> #include <string.h> #include <stdlib.h> #include <ctype.h> #include <conio.h> #include "bank.h" void downdate() { int i=0; FILE *fp; if ((fp=fopen("bank.txt","r"))==NULL) { printf("bank file cannot open!"); exit(0); } while(fscanf(fp,"%d %s %d %lf %d",&user[i].num,user[i].name, &user[i].password, &user[i].balance,&user[i].state ) != EOF) { i++; } N=i; fclose(fp); return; } void work() { int ichose; do { ichose=iChoose(); switch (ichose) { case 1: open_account(); //开户 break; case 2: cancellation(); //2注销 break; case 3: deposit(); //3存款 break; case 4: withdraw_money(); //4取款 break; case 5: query(); //5查询 break; case 6: transfer_accounts(); //6转账 break; case 7: loss(); //7挂失 break; case 8: unloss(); //8解挂 break; case 9: change(); //9该密 break; case 0: printf("欢迎再来\n"); update(); //保存退出 } } while (ichose); } int iChoose() { int ichose; while (1) { printf("\n"); printf("+----------------------------+\n"); printf("+ 1 开户 2 销户 3 存款 +\n"); printf("+ 4 取款 5 查询 6 转账 +\n"); printf("+ 7 挂失 8 解挂 9 改密 +\n"); printf("+ 0 退出 +\n"); printf("+----------------------------+\n"); printf("请输入操作指令:"); scanf("%d",&ichose); if (ichose>=0 && ichose<=9) { break; } else printf("输入错误,请重新选择\n\n"); } return ichose; } void open_account() { int ipass1,ipass2; if (N==upNum) { printf("银行用户达到上限,不能开户\n"); } printf("正在进行开户\n"); printf("请输入用户账号:"); scanf("%d",&user[N].num); printf("请输入用户姓名:"); scanf("%s",user[N].name); printf("请输入密码:"); ipass1=inputpass(); printf("请再次确认密码:"); ipass2=inputpass(); if (ipass1==ipass2) { user[N].password=ipass1; user[N].state=0; printf("存款金额:"); scanf("%lf",&user[N].balance); printf("开户成功!\n"); N++; } else { printf("密码错误,开户失败!\n"); } } void cancellation() { int i; int cancel; int pass; printf("输入待销户账号:"); scanf("%d",&cancel); i=search(cancel); printf("姓名:%s\n",user[i].name); printf("密码:"); pass=inputpass(); if (pass==user[i].password) { printf("余额:%.2lf",user[i].balance); printf("确认销户Y/N:"); if(tolower(getchar())=='y') { user[i].balance=0; //取款后余额变0 user[i].state=2; //状态变为注销 printf("取款 %.2f 元,销户成功!\n", user[i].balance); } else { printf("你取消了操作,销户失败!\n"); } } else { printf("密码错误,销户失败!\n"); } } void deposit() { int i; int id; int pass; double money; printf("请输入账号:"); scanf("%d",&id); i=search(id); if (i<0) { printf("用户名不存在!\n"); } else { printf("姓名:%s",user[i].name); printf("密码:"); pass=inputpass(); if (pass==user[i].password&&user[i].state==0) { printf("输入存款金额:"); scanf("%lf",&money); printf("余额总计%.2lf,存款成功",money+user[i].balance); } else if (pass==user[i].password&&user[i].state==1) { printf("卡已挂失,存款失败!\n"); } else if (pass==user[i].password&&user[i].state==2) { printf("卡已注销,存款失败!\n"); } else { printf("密码错误,存款失败!\n"); } } } void withdraw_money() { int i; int id; int pass; double money; printf("请输入账号:"); scanf("%d",&id); i=search(id); if(i<0) { printf("用户名不存在!\n"); } else { printf("姓名:%s",user[i].name); printf("密码:"); pass=inputpass(); if (pass==user[i].password&&user[i].state==0) { printf("余额 :%.2lf\n",user[i].balance); printf("取款金额:"); scanf("%lf",&money); if (money>user[i].balance) { printf("余额不足,取款失败"); } else { user[i].balance-=money; printf("余额:%.2lf",user[i].balance); } } else if (pass==user[i].password&&user[i].state==1) { printf("卡已挂失,取款失败!\n"); } else if (pass==user[i].password&&user[i].state==2) { printf("卡已注销,取款失败!\n"); } else { printf("密码错误,取款失败!\n"); } } } void query() { int i; int id; int pass; printf("请输入账号:"); scanf("%d",&id); i=search(id); printf("姓名:%s",user[i].name); printf("密码:"); pass=inputpass(); if (pass==user[i].password) { printf("余额:%.2lf\n",user[i].balance); if (user[i].state==0) { printf("当前状态:正常\n"); } else if (user[i].state==1) { printf("当前状态:挂失\n"); } else printf("当前状态:注销\n"); printf("查询完毕!\n"); } else { printf("密码错误,查询失败\n"); } } void transfer_accounts() { int id,id2; int i,i2; double money; int pass; printf("请输入转出账号:"); scanf("%d",&id); i=search(id); printf("姓名:%s",user[i].name); printf("密码:"); pass=inputpass(); if (pass==user[i].password&&user[i].state==0) { printf("请输入转入账号:"); scanf("%d",&id2); i2=search(id2); printf("转账金额:"); scanf("%lf",&money); if (money>user[i].balance) { printf("金额不足,转账失败!\n"); } else { printf("转账成功,余额 %.2lf\n",user[i].balance-money); user[i2].balance+=money; } } } void loss() { int id; int i; int pass; printf("请输入账号:"); scanf("%d",&id); i=search(id); printf("姓名:%s",user[i].name); printf("密码:"); pass=inputpass(); if (pass==user[i].password) { printf("余额:%.2lf\n",user[i].balance); if (user[i].state==0) { printf("当前状态:正常\n"); user[i].state=1; printf("挂失成功!\n"); } else if (user[i].state==1) { printf("当前状态:挂失\n"); } else printf("当前状态:注销\n"); } else { printf("密码错误,挂失失败\n"); } } void unloss() { int id; int i; int pass; printf("请输入账号:"); scanf("%d",&id); i=search(id); printf("姓名:%s",user[i].name); printf("密码:"); pass=inputpass(); if (pass==user[i].password) { printf("余额:%.2lf\n",user[i].balance); if (user[i].state==0) { printf("当前状态:正常\n"); } else if (user[i].state==1) { printf("当前状态:挂失\n"); user[i].state=0; printf("解挂成功!\n"); } else printf("当前状态:注销\n"); } else { printf("密码错误,解挂失败\n"); } } void change() { int id; int i; int pass,pass1,pass2; printf("请输入账号:"); scanf("%d",&id); i=search(id); printf("姓名:%s",user[i].name); printf("密码:"); pass=inputpass(); if (pass==user[i].password&&user[i].state!=2) { printf("新密码:"); pass1=inputpass(); printf("再次输入:"); pass2=inputpass(); if (pass1==pass2) { user[i].password=pass1; } else { printf("2次输入不正确,改密码失败\n"); } } else { printf("密码不对!\n"); } } void update() { FILE *p; int i=0; if ((p=fopen("bank.txt", "w"))==NULL) { printf("bank.txt file cannot open!"); exit(0); } for(i=0; i<N; i++) fprintf(p,"%d %s %d %lf %d\n",user[i].num, user[i].name, user[i].password, user[i].balance,user[i].state); fclose(p); exit(0); }
#include <stdio.h> #include <string.h> #include <stdlib.h> #include <ctype.h> #include <conio.h> #include "bank.h" int search(int id) { int index; index=-1; int i; for (i=0; i<N; i++) { if (user[i].num==id) { index=i; break; } } return index; } int inputpass() { char ch; //接收字符形式密码 int iPass=0; //要转换为数字 int i; while(1) { for(i=0; i<6; i++) { ch=getch(); //输入但不显示 putchar('*'); //输出* if(isdigit(ch)) iPass=iPass*10+(ch-'0'); else { iPass=0; break; //退出for循环后,再次接受 } } fflush(stdin); //清除键盘缓存区中已经有的输入 printf("\n"); if(iPass==0) //此条件成立可能由两种情况引起:输入了非数字字符被直接重置为0,或6位全0后正常退出for循环 { printf("密码要求全为数字,且不能全0!\n"); printf("请重新输入密码: "); } else break; } return iPass; }
等老师把进阶的所有实践都完成了 重新再看一遍进阶,目前在复习之前的内容
版权声明:本文为博主原创文章,未经博主允许不得转载。
标签:
原文地址:http://blog.csdn.net/richever1984/article/details/46686529