您好,登录后才能下订单哦!
密码登录
登录注册
点击 登录注册 即表示同意《亿速云用户服务条款》
这篇文章主要介绍“怎么用C语言实现银行系统”,在日常操作中,相信很多人在怎么用C语言实现银行系统问题上存在疑惑,小编查阅了各式资料,整理出简单好用的操作方法,希望对大家解答”怎么用C语言实现银行系统”的疑惑有所帮助!接下来,请跟着小编一起来学习吧!
main.c
#include <stdio.h> #include <stdlib.h> #include <string.h> #include <conio.h> #include <windows.h> #define MaxNum 200 #define N_P 6 typedef struct { int AccountNumber[3]; char name[20]; char password[N_P+1]; unsigned long amount; int state; } Account; void Load(FILE *,Account *); void ShowMenu(int ); int MakeChoice(int ); void EnterPassword(char *); void CreatAccount(unsigned char *,Account *,int); //第一个参数为加密关键字,第二个参数为账户序号 int JudgePassword(char *,char *); int decrypt(int *,unsigned char *); void LogIn(unsigned char *,Account *,int); //第二个参数为当前账户数目,登陆成功返回1,失败返回0 int EnterAccountNumber(unsigned char *,int ); void EnterNum(char *,int ); void Operate(unsigned char *,Account *,int ,int ); void ShowSubMenu(char *); void AdministratorMode(unsigned char *,unsigned char *,Account *,char *,int); void ShowAdministratorMenu(); void WriteToFile(FILE *,unsigned char *,Account*,char *,int); int main() { FILE *fp; int i,total; unsigned char key[4],key_en[4]; char administrator_password[10]; Account *user; user=(Account *)malloc(sizeof(Account)*MaxNum); fp=fopen("bank.txt","r+"); if(fp==NULL) { printf("银行数据库无法打开,按任意键结束程序!\n"); getch(); exit(1); } fscanf(fp,"%d\n%s\n%s",&total,administrator_password,key); for(i=0;i<3;++i)//用key_en保存key的值 key_en[i]=key[i]; key_en[0]=(key_en[0]<<3)|(key_en[0]>>(sizeof(key_en[0])*8-3)); key_en[2]=(key_en[2]>>3)|(key_en[2]<<(sizeof(key_en[2])*8-3)); Load(fp,user);//将数据存入数组user中 while(1) { system("cls"); ShowMenu(total); switch(MakeChoice(4)) { case 1: system("cls"); LogIn(key_en,user,total); break; case 2: system("cls"); CreatAccount(key_en,user,total++); break; case 3: system("cls"); AdministratorMode(key,key_en,user,administrator_password,total); break; default: system("cls"); WriteToFile(fp,key,user,administrator_password,total); return 0; } } return 0; }
function.c
#include <stdio.h> #include <stdlib.h> #include <string.h> #include <conio.h> #include <windows.h> #define MaxNum 200 #define N_P 6 //账户状态 0为正常 1为挂失 2为冻结 3为销户 typedef struct { int AccountNumber[3]; char name[20]; char password[N_P]; unsigned long amount; int state; } Account; void Load(FILE *,Account *); void ShowMenu(int ); int MakeChoice(int ); void EnterPassword(char *); void CreatAccount(unsigned char *,Account *,int); //第一个参数为加密关键字,第二个参数为账户序号 int JudgePassword(char *,char *); int decrypt(int *,unsigned char *); void LogIn(unsigned char *,Account *,int); //第二个参数为当前账户数目,登陆成功返回1,失败返回0 int EnterAccountNumber(unsigned char *,int ); void EnterNum(char *,int ); void Operate(unsigned char *,Account *,int,int ); void ShowSubMenu(char *); void AdministratorMode(unsigned char *,unsigned char *,Account *,char *,int); void ShowAdministratorMenu(); void WriteToFile(FILE *,unsigned char *,Account*,char *,int); void Load(FILE *fp,Account *user) { int count=0; //将文件中的用户信息存到数组中 while(fscanf(fp,"\n%d%d%d %s %s %lu %d\n",&user[count].AccountNumber[0],&user[count].AccountNumber[1],\ &user[count].AccountNumber[2],user[count].name,\ user[count].password,&user[count].amount,&user[count].state)!=EOF) ++count; } void ShowMenu(int total) { printf("****中国银行烟大计163-1分行****\n"); printf("目前共有%d名注册用户*****\n",total); printf("1.登陆*************************\n"); printf("2.开户*************************\n"); printf("3.进入管理员模式***************\n"); printf("4.退出*************************\n"); } int MakeChoice(int n) { int judge; printf("请输入1~%d\n",n); scanf("%d",&judge); while(judge<1||judge>n)//确保输入的是1~n { printf("输入不合法,请输入1~%d\n",n); fflush(stdin);//如果不加这句,输入一些字母会导致函数无限循环 scanf("%d",&judge); } return judge; } void EnterPassword(char *password) { int i; char t; for(i=0; i<N_P; ++i) { t=getch(); if(t=='\b')//执行退格删除操作 { printf("\b \b"); i-=2; continue; } while(t<'0'||t>'9') t=getch(); password[i]=t; putchar('*'); } } void EnterNum(char *s,int n) { int i=0; char t; while(i<n) { t=getch(); if(t=='\b') { printf("\b \b"); --i; continue; } else if(t=='\r'||t=='\n') break; while(t<'0'||t>'9') t=getch(); s[i]=t; putchar(t); ++i; } } int JudgePassword(char *temp_password,char *password) { int i; for(i=0; i<N_P; ++i) { if(temp_password[i]!=password[i]) break; } if(i==N_P) return 0; else return 1; } void CreatAccount(unsigned char *key,Account *user,int num) { if(num>=MaxNum) { printf("银行用户数量已达到上限!\n按任意键返回主界面"); getch(); return; } int i,flag,num_de[3]; int num_en[3]; char temp_password[N_P],password[N_P]; //将num的个位十位和百位分解,num_de[2]存个位,以此类推 for(i=0; i<3; ++i) num_de[i]=0; for(i=0; i<3; ++i) num_en[i]=0; if(num<10) num_de[2]=num; else if(num<100) { num_de[2]=num%10; num_de[1]=num/10; } else { for(i=0; i<3; ++i) { num_de[i]=num%10; num/=10; } } //分解完成 while(1) { flag=0; printf("请输入用户姓名:"); fflush(stdin); scanf("%19[^\n]",user[num].name); for(i=0; i<strlen(user[num].name); ++i) { if(user[num].name[i]==' ') { printf("用户名中不能包含空格!\n"); flag=1; break; } } if(flag==0) break; } for(i=0; i<3; ++i) num_en[i]=num_de[i]^key[i]; for(i=0; i<3; ++i) user[num].AccountNumber[i]=num_en[i]; printf("您的账号是:"); for(i=0; i<3; ++i) printf("%03d ",num_en[i]); while(1) { printf("\n请设置密码:"); EnterPassword(password); printf("\n请再次确认密码:"); EnterPassword(temp_password); if(JudgePassword(temp_password,password)) printf("\n两次输入不相等,密码设置失败!"); else { strcpy(user[num].password,password); user[num].password[N_P]='\0'; break; } } user[num].amount=0; user[num].state=0; printf("\n账户创建成功!\n按任意键返回主菜单"); getch(); } int decrypt(int *num_en,unsigned char *key) { int i,num,num_de[3]; for(i=0; i<3; ++i) num_de[i]=num_en[i]^key[i]; num=num_de[0]*100+num_de[1]*10+num_de[2]; return num; } int EnterAccountNumber(unsigned char *key,int total) { int i,num; int num_en[3]; char num_en_char[3]; while(1)//输入账号 { printf("请输入账号:"); for(i=0;i<3;++i) { EnterNum(num_en_char,3); num_en[i]=atoi(num_en_char); } num=decrypt(num_en,key); if(num>total)//若num大于total则账号输入错误 printf("\n账号输入错误!\n"); else break; } return num; } void LogIn(unsigned char *key,Account *user,int total) { int num,i; char password[N_P]; num=EnterAccountNumber(key,total); //判断账户状态 if(user[num].state==3) { printf("该账户已销户,无法使用!\n"); printf("按任意键返回操作界面"); getch(); return; } else if(user[num].state==2) { printf("该账户因输入密码错误次数达三次,已冻结!\n请进入管理员模式解除异常状态!\n"); printf("按任意键返回操作界面"); getch(); return; } //判断结束 i=0; while(1)//输入密码 { printf("\n请输入密码:"); EnterPassword(password); if(JudgePassword(password,user[num].password)&&(++i<3)) printf("\n密码输入错误,您还有%d次机会\n",3-i); else if(i>=3) { user[num].state=2; printf("\n密码错误次数过多,账户已冻结!\n"); printf("即将返回主界面 "); for(i=0; i<3; ++i) { printf("%d",i); Sleep(1000); printf("\b"); } return ; } else { printf("登陆成功!\n"); break; } } Operate(key,user,num,total); } void ShowSubMenu(char *name) { printf("****欢迎%s登陆本行********\n",name); printf("1.存款*************************\n"); printf("2.取款*************************\n"); printf("3.查询*************************\n"); printf("4.转账*************************\n"); printf("5.挂失*************************\n"); printf("6.销户*************************\n"); printf("7.改密*************************\n"); printf("8.返回主菜单*******************\n"); } void Operate(unsigned char *key,Account *user,int num,int total) { int i,num_t; unsigned long amount_t,max=1; char password[N_P],temp_password[N_P],amount_judge[6]; for(i=0; i<(sizeof(unsigned long)*8-1); ++i) //计算最大存款额max max*=2; while(1) { system("cls"); ShowSubMenu(user[num].name); switch(MakeChoice(8)) { case 1: system("cls"); while(1) { printf("请输入存款金额:"); for(i=0;i<6;++i) amount_judge[i]='\0'; EnterNum(amount_judge,5); amount_t=atol(amount_judge); if(amount_t>99999) printf("\n单次存款金额为0~99999元,存款失败!\n"); else if(amount_t>=max-user[num].amount) printf("总金额超出上限,存款失败!\n"); else { user[num].amount+=amount_t; printf("\n存款成功!按任意键返回操作界面"); getch(); system("cls"); break; } } break; case 2: system("cls"); if(user[num].state==1) { printf("该账户已挂失,无法执行取款操作!\n"); printf("按任意键返回操作界面"); getch(); break; } while(1) { printf("请输入取款金额:"); for(i=0;i<6;++i) amount_judge[i]='\0'; EnterNum(amount_judge,5); amount_t=atol(amount_judge); if(amount_t>99999) printf("\n单次存款金额为0~99999元,存款失败!\n"); else if(amount_t>user[num].amount) printf("\n账户余额不足,取款失败!\n"); else { user[num].amount-=amount_t; printf("\n取款成功!按任意键返回操作界面"); getch(); system("cls"); break; } } break; case 3: system("cls"); printf("用户姓名:%s",user[num].name); printf("账户余额:%lu\n",user[num].amount); printf("账户状态:"); if(user[num].state==1) printf("已挂失\n"); else printf("正常\n"); printf("按任意键返回操作界面"); getch(); system("cls"); break; case 4: system("cls"); if(user[num].state==1) { printf("该账户已挂失,无法执行转账操作!\n"); printf("按任意键返回操作界面"); getch(); break; } num_t=EnterAccountNumber(key,total); while(1) { printf("请输入转账金额:"); for(i=0;i<6;++i) amount_judge[i]='\0'; EnterNum(amount_judge,5); amount_t=atol(amount_judge); if(amount_t>99999) printf("\n单次存款金额为0~99999元,存款失败!\n"); else if(amount_t>user[num].amount) printf("账户金额不足,转账失败!\n"); else if(amount_t>=max-user[num_t].amount) printf("\n对方总金额超出上限,转账失败!\n"); else { user[num].amount-=amount_t; user[num_t].amount+=amount_t; printf("\n转账成功!按任意键返回操作界面"); getch(); system("cls"); break; } } break; case 5: system("cls"); printf("您确定要挂失吗?\n"); printf("1.确定 2.取消\n"); if(MakeChoice(2)==1) { user[num].state=1; printf("挂失成功!\n按任意键返回主界面"); getch(); } system("cls"); break; case 6: system("cls"); printf("您确定要销户吗?销户后所有功能都将无法实现,账户内的存款将冻结!\n"); printf("1.确定 2.取消\n"); if(MakeChoice(2)==1) { user[num].state=3; printf("销户成功!\n按任意键返回主界面"); getch(); system("cls"); return; } else break; case 7: system("cls"); while(1) { printf("\n请输入原密码:"); EnterPassword(password); if(JudgePassword(password,user[num].password)) printf("\n原密码输入错误!\n"); else break; } while(1) { printf("\n请输入新密码:"); EnterPassword(password); printf("\n请再次确认密码:"); EnterPassword(temp_password); if(JudgePassword(temp_password,password)) printf("\n两次输入不相等,密码设置失败!"); else { strcpy(user[num].password,password); break; } } printf("\n密码修改成功!\n按任意键返回操作界面"); getch(); system("cls"); break; default: return; } } } void AdministratorMode(unsigned char *key_orig,unsigned char *key,Account *user,char *administrator_password,int total) { int num; char password[N_P],temp_password[N_P]; while(1)//输入密码 { printf("请输入管理员密码密码:"); EnterPassword(password); if(JudgePassword(password,administrator_password)) printf("密码输入错误!\n"); else break; } printf("\n登陆成功!按任意键继续"); getch(); while(1) { system("cls"); ShowAdministratorMenu(); switch(MakeChoice(6)) { case 1: system("cls"); num=EnterAccountNumber(key,total); printf("请设置密码:"); EnterPassword(password); printf("您设置的密码是:"); puts(user[num].password); printf("\n按任意键返回操作界面"); getch(); break; case 2: system("cls"); num=EnterAccountNumber(key,total); printf("您确定要解除此用户的挂失状态吗?\n"); printf("1.确定 2.取消\n"); if(MakeChoice(2)==1) { user[num].state=0; printf("解除挂失成功!\n按任意键返回主界面"); getch(); } break; case 3: system("cls"); num=EnterAccountNumber(key,total); printf("\n您确定要解除此用户的冻结状态吗?\n"); printf("1.确定 2.取消\n"); if(MakeChoice(2)==1) { user[num].state=0; printf("解除冻结成功!\n按任意键返回主界面"); getch(); } break; case 4: system("cls"); while(1) { printf("请输入新密码:"); EnterPassword(password); printf("\n请再次确认密码:"); EnterPassword(temp_password); if(JudgePassword(temp_password,password)) printf("\n两次输入不相等,密码设置失败!\n"); else { strcpy(administrator_password,password); break; } } printf("\n管理员密码修改成功!\n按任意键返回操作界面"); getch(); break; case 5: system("cls"); printf("当前的账号生成关键字为“%s”,更改之后所有之前创建的账户将无法打开!\n",key_orig); printf("您确定要更改账号生成关键字吗?\n"); printf("1.确定 2.取消\n"); if(MakeChoice(2)==1) { printf("请输入新的关键字:"); scanf("%3s",key_orig); printf("当前的关键字已更改为“%s”\n按任意键返回操作界面",key_orig); getch(); } break; default: return; } } } void ShowAdministratorMenu() { printf("****中国银行烟大计163-1分行****\n"); printf("**********管理员模式***********\n"); printf("1.修改用户密码*****************\n"); printf("2.解除挂失*********************\n"); printf("3.解除冻结*********************\n"); printf("4.修改管理员密码***************\n"); printf("5.修改账号生成关键字***********\n"); printf("6.返回主界面*******************\n"); } void WriteToFile(FILE *fp,unsigned char *key,Account *user,char *administrator_password,int total) { int i,j; rewind(fp); fprintf(fp,"%d\n%6s\n%3s\n",total,administrator_password,key); for(i=0; i<total; ++i) { for(j=0; j<3; ++j) fprintf(fp,"%03d ",user[i].AccountNumber[j]); fprintf(fp,"%s %6s %lu %d\n",user[i].name,user[i].password,\ user[i].amount,user[i].state); } fclose(fp); }
到此,关于“怎么用C语言实现银行系统”的学习就结束了,希望能够解决大家的疑惑。理论与实践的搭配能更好的帮助大家学习,快去试试吧!若想继续学习更多相关知识,请继续关注亿速云网站,小编会继续努力为大家带来更多实用的文章!
免责声明:本站发布的内容(图片、视频和文字)以原创、转载和分享为主,文章观点不代表本网站立场,如果涉及侵权请联系站长邮箱:is@yisu.com进行举报,并提供相关证据,一经查实,将立刻删除涉嫌侵权内容。