c++程序求修改
刚学c++,编写了一个银行存取款的小程序,由于没有学过容器和向量,我都是用类定义数组实现的,现在刚学容器,向量一类的,觉得有点懵,谁能帮我把我这个程序改成用容器和向量实现啊程序代码:
#include<iostream> #include<fstream> #include<time.h> #include<stdio.h> using namespace std; class cust { private: char card[20]; char password[20]; float rest; float money; char date[100]; public: cust() { rest=0.0; } bankin(float n) { FILE *fp; time_t t=time(0); strftime(date,sizeof(date),"%Y/%m/%d %X",localtime(&t)); money=n; rest=rest+money; cout<<"已经存入!"<<endl; fp=fopen(card,"r"); if(fp==NULL) { fp=fopen(card,"w"); } else { fclose(fp); fp=fopen(card,"a+"); } if(fp==NULL) { cout<<"打开文件失败,程序退出"<<endl; exit(0); } fprintf(fp,"%s %s 存入%.2f %.2f\n",date,card,n,rest); fclose(fp); } bankout(float n) { FILE *fp; time_t t=time(0); strftime(date,sizeof(date),"%Y/%m/%d %X",localtime(&t)); if(n>rest)cout<<"余额不足!"<<endl; else { money=-n; rest=rest+money; cout<<"取款成功!"<<endl; fp=fopen(card,"a+"); if(fp==NULL) { cout<<"打开文件失败,程序退出"<<endl; exit(0); } fprintf(fp,"%s %s 取出%.2f %.2f\n",date,card,n,rest); fclose(fp); } } view() { cout<<"您的余额为:"<<rest<<endl; } int card_same(char ca[]) { int kk=0; if(!strcmp(card,ca))kk=1; return kk; } setcard(char a[20]) { strcpy(card,a); } setpassword() { FILE *fp; char a1[20],a2[20]; cout<<"请输入密码:"<<endl; cin>>a1; cout<<"请再输一遍密码:"<<endl; cin>>a2; while(strcmp(a2,a1)) { cout<<"两次输入不一致,请重新输入!"<<endl; cout<<"请输入密码:"<<endl; cin>>a2; cout<<"请再输一遍密码:"<<endl; cin>>a1; } strcpy(password,a1); fp=fopen("用户名密码.txt","a+"); if(fp==NULL) { cout<<"打开文件失败,程序退出"<<endl; exit(0); } fprintf(fp,"%s %s\n",card,password); fclose(fp); } setrest() { FILE *fp; char n[20]; fp=fopen(card,"a+"); while(!feof(fp)) { fscanf(fp,"%s",n); } rest=atof(n); fclose(fp); } int login(char c[20],char p[20]) { int log=0; if(!strcmp(card,c)&&!strcmp(password,p)) log=1; return log; } print() { char now_time[20]; time_t t=time(0); strftime(now_time,sizeof(date),"%Y/%m/%d %X",localtime(&t)); cout<<"日期"<<'\t'<<'\t'<<'\t'<<"卡号"<<'\t'<<"余额"<<endl; printf("%s\t%s\t%.2f\n",now_time,card,rest); } load(char c1[],char p1[]) { strcpy(card,c1); strcpy(password,p1); } show() { FILE *fp; char read; cout<<"交易日期"<<'\t'<<"卡号"<<'\t'<<"交易情况"<<'\t'<<"余额"<<endl; fp=fopen(card,"r"); if(fp==NULL) { cout<<"尚未开始交易"<<endl; } else { while(!feof(fp)) { fscanf(fp,"%c",&read); printf("%c",read); } cout<<endl; fclose(fp); } } };
程序代码:
#include<iostream> #include<fstream> #include<time.h> #include<stdio.h> #include"class.h" using namespace std; int menu(char key[],int a,int b) { int n,m,i; while(1) { m=1; for(i=0;i<strlen(key);i++) { if(isdigit(key[i])==0) { m=0; break; } } if(m==0) { printf("请输入正确的选择:\n"); scanf("%s",key); } else { n=atoi(key); if(n>b||n<a)printf("请输入正确的选择:\n"),scanf("%s",key); else break; } } return n; } int main( void ) { FILE *fp; cust cm[100]; int i=0,t=0,total; char key[100],key1[100],c[20],p[20],card[20]; float n; fp=fopen("用户名密码.txt","a+"); if(fp==NULL) { cout<<"打开文件失败,程序退出"<<endl; exit(0); } while(!feof(fp)) { fscanf(fp,"%s %s",c,p); if(feof(fp))break; cm[i].load(c,p); i++; } total=i; cout<<"*******************************************************"<<endl; cout<<"*--------------------中国银行欢迎你!-----------------*"<<endl; cout<<"* *"<<endl; cout<<"* 1用户登陆 2注册 3退出系统 *"<<endl; cout<<"*******************************************************"<<endl; cin>>key; menu(key,1,3); while(menu(key,1,3)!=3) { switch(menu(key,1,3)) { case 1: do{ cout<<"请输入用户名:"<<endl; cin>>c; cout<<"请输入密码:"<<endl; cin>>p; for(i=0;i<total;i++) { if(cm[i].login(c,p))break; } if(!cm[i].login(c,p))cout<<"用户名或者密码错误,请重新输入"<<endl; t++; if(t>=3){cout<<"输入错误次数过多,系统将自动退出!"<<endl; exit(0);} }while(!cm[i].login(c,p)); t=0; cout<<"登陆成功"<<endl; cm[i].setrest(); do{ cout<<"*******************************************************"<<endl; cout<<"*--------------------中国银行欢迎你!-----------------*"<<endl; cout<<"* *"<<endl; cout<<"* 1存款 2取款 3查询余额 4查看明细 5退出系统 *"<<endl; cout<<"*******************************************************"<<endl; cin>>key1; menu(key1,1,5); switch(menu(key1,1,5)) { case 1: cout<<"请输入要存入的金额:"<<endl; cin>>n; cm[i].bankin(n); break; case 2: cout<<"请输入要取出的金额:"<<endl; cin>>n; cm[i].bankout(n); break; case 3: cm[i].print(); break; case 4: cm[i].show(); break; } }while(menu(key1,1,5)!=5); break; case 2: do{ cout<<"请输入用户名"<<endl; cin>>card; for(i=0;i<=total;i++) { if(cm[i].card_same(card)==1) { cout<<"该用户名已经存在,请重新输入:"<<endl; break; } } }while(cm[i].card_same(card)==1); cm[total].setcard(card); cm[total].setpassword(); total++; cout<<"恭喜你,注册成功!"<<endl; break; } cout<<"*******************************************************"<<endl; cout<<"*--------------------中国银行欢迎你!-----------------*"<<endl; cout<<"* *"<<endl; cout<<"* 1用户登陆 2注册 3退出系统 *"<<endl; cout<<"*******************************************************"<<endl; cin>>key; } }