链表解决银行储存系统---技术支持
2016-06-14 20:22
260 查看
问题及代码:
/* *copyright (t) 2016,烟台大学计算机学院 *All rights reserved. *文件名称:test.cpp *作者:张晴晴 *完成日期:2016年6月14日 *版本号:v1.0 *问题描述:银行储存系统 *输入描述: *程序输出: */ #include <iostream> #include <string> #include <assert.h> #include <climits> using namespace std; class Accout { protected: string name; double money; string ID; string Phone; public: Accout(string name, double money=0, string ID="000", string Phone="000"):name(name),money(money),ID(ID),Phone(Phone){} string getname()const{return this->name;} virtual bool savemoney ()=0; virtual bool getoutmoney()=0; virtual bool showAccoutInfo()=0; Accout *next; }; class Normal:public Accout { public: Normal(string name, double money, string ID, string Phone):Accout(name,money,ID,Phone){} virtual bool savemoney (); virtual bool getoutmoney(); virtual bool showAccoutInfo(); }; bool Normal::savemoney() { double m; cout<<"请输入存款金额"<<endl; cin>>m; this->money+=m; return true; } bool Normal::getoutmoney() { double m; cout<<"请输入取款金额"<<endl; cin>>m; if (m>money) { cout<<"您的余额不足"<<endl; } else this->money-=m; return true; } bool Normal::showAccoutInfo() { cout<<this->name<<" "<<this->money<<" "<<this->ID<<" "<<this->Phone<<endl; return true; } class Vip:public Accout { public: Vip(string name, double money, string ID, string Phone):Accout(name,money,ID,Phone),already(0),left(5000){} virtual bool savemoney (); virtual bool getoutmoney(); virtual bool showAccoutInfo(); private: double already; double left; }; bool Vip::savemoney() { double m; cout<<"请输入存款金额"<<endl; cin>>m; this->money+=m; return true; } bool Vip::getoutmoney() { double m; cout<<"请输入取款金额"<<endl; cin>>m; if (m>this->money) { already=m-this->money; left=left-already; this->money=0; cout<<"您的余额不足,透支金额为:"<<already<<endl; cout<<"可透支的额度为:"<<left<<endl; } return true; } bool Vip::showAccoutInfo() { cout<<name<<endl<<money<<endl<<ID<<endl<<Phone<<endl<<left<<endl<<already<<endl; return true; } class Bank { public: Bank():head(NULL){} bool append(); bool deleteUser(const string &name); void view()const; Accout* find(const string &name) const; private: Accout *head; }; bool Bank::append()//创建链表 { Accout *mid; char a; string name,ID,Phone; double money; cout<<"需要办理VIP请按0,需要办理普通用户请按1"<<endl; cin>>a; cout<<"请输入用户名 存入金额 身份证号和手机号码"<<endl; cin>>name>>money>>ID>>Phone; if (this->head==NULL) { if (a=='1') { head=new Normal(name,money,ID,Phone); head->next=NULL; } else { head=new Vip(name,money,ID,Phone); head->next=NULL; } } else { mid=head; while (mid!=NULL) { mid=mid->next; //移动指针至表尾 } if(a=='1') { mid=new Normal(name,money,ID,Phone); mid->next=NULL; } else { mid=new Vip(name,money,ID,Phone); mid->next=NULL; } } return true; } Accout* Bank::find(const string&name1) const//查找节点 { Accout *mid; mid=head; while (mid->getname()!=name1&&mid!=NULL) { mid=mid->next; } if(mid==NULL) cout<<"没有找到该用户,请检查用户名是否填写正确"<<endl; assert(mid!=NULL); return mid; } bool Bank::deleteUser(const string& name2)//删除节点 { Accout *mid; mid=head; if (head->getname()==name2) { head=head->next; delete mid; mid=NULL; } else { while (mid->next!=find(name2)) { mid=mid->next; } mid->next=find(name2)->next; delete find(name2); mid=find(name2); mid=NULL; } return true; } void Bank::view()const//遍历链表 { Accout *mid; mid=head; while (mid!=NULL) { mid->showAccoutInfo(); mid=mid->next; } } int main() { char b; Bank bank; string user; while (1) { cout<<"创建账户请按1"<<endl; cout<<"存款请按2"<<endl<<"取款请按3"<<endl<<"查看用户信息请按4"<<endl<<"查看所有用户信息请按5"<<endl<<"销户请按6"<<endl; cin>>b; cin.ignore(80,'\n'); switch (b) { case '1': bank.append(); break; case '2': cout<<"请输入用户名"<<endl; cin>>user; bank.find(user)->savemoney(); break; case '3': cout<<"请输入用户名"<<endl; cin>>user; bank.find(user)->getoutmoney(); break; case '4': cout<<"请输入用户名"<<endl; cin>>user; bank.find(user)->showAccoutInfo(); break; case '5': bank.view(); break; case '6': cout<<"请输入用户名"<<endl; cin>>user; bank.deleteUser(user);; break; default: break; } } return 0; }
相关文章推荐
- 【CF679E】Bear and Bad Powers of 42
- 数组做数据成员2(改)
- C语言中关于指针的学习
- hdu 4349(Lucas定理)
- leetcode笔记—Decode Ways
- 学生信息管理系统
- 权限树问题
- 实战Walker之侧滑菜单的实现分析
- VBA Study Notes
- VBA Study Notes
- VBA Study Notes
- VBA Study Notes
- VBA Study Notes
- VBA Study Notes
- VBA Study Notes
- div嵌套后margin出现失效(转移)的问题
- TreeSet的Comparator排序
- <LeetCode OJ> 334. Increasing Triplet Subsequence
- app使用微信支付成功后,点击返回到该app却跳到另外一个app去了
- 344. Reverse String