题目一、编写ATM机类,包括账号密码验证,存钱取钱、改密码等功能。
cpp
class AccountItem
{ public:
AccountItem();
AccountItem(string Anumber, string Password, string Owener,
const double Balance);
void Display();
void Read(ifstream& s);
// the following function was added for inventory update
void Write(ofstream& s);
short CheckNumber(string Anumber);
string GetNumber();
string GetPassword();
void UpdatePassword(string pass); //修改密码
void DeductBalance(double pay);
void SaveAccount(double count); //存钱
double GetBalance();
string GetName();
short IsNull();
private:
string m_Anumber;
string m_Password;
string m_Name;
double m_Balance;
};
增加的成员函数为:
void AccountItem::UpdatePassword(string pass) //修改密码
{ m_Password=pass;
};
void AccountItem::SaveAccount(double count) //存钱count到帐户中
{ m_Balance += count;
};
主函数修改为:
#include <stdio.h>
#include <iostream.h>
#include <fstream.h>
#include "strclass.h"
#include "accitem.h"
#include "Accbook.h"
int main()
{ ifstream InputStream("Account.in");
string AccountNo;
string AccPassword;
string newPassword1;
string newPassword2;
double AccountCount;
string ItemName;
double OldAccountbook;
Accountbook MyAccountbook;
AccountItem FoundItem;
string TransactionCode;
string Count;
double SaveCount;
MyAccountbook.LoadAccountbook(InputStream);
cout << "请输入帐号: ";
cin >> AccountNo;
FoundItem = MyAccountbook.FindItem(AccountNo);
if (FoundItem.IsNull() == true)
{ cout << "帐号不存在." << endl;
return 0;
}
cout << "请输入口令: ";
cin >> AccPassword;
if (FoundItem.GetPassword()!=AccPassword)
{ cout << "口令错误!" << endl;
return 0;
}
OldAccountbook=FoundItem.GetBalance();
cout << "请选择交易代码:" << endl;
cout << "G (取钱)" << endl;
cout << "C (查询余额)" << endl;
cout << "S (存钱)" << endl;
cout << "U (修改密码)" << endl;
cin >> TransactionCode;
if (TransactionCode == "C" || TransactionCode == "c")
{ cout << "余额是: " << FoundItem.GetBalance()<<endl;
}
else if (TransactionCode == "G" || TransactionCode == "g")
{ cout << "请选择取钱的数量: "<<endl;
cout << "1 (取100)" << endl;
cout << "2 (取200)" << endl;
cout << "5 (取500)" << endl;
cout << "A (取1000)" << endl;
cout << "B (取2000)" << endl;
cin >> Count;
if (Count=="1") AccountCount=100.;
else if (Count=="2") AccountCount=200.;
else if (Count=="5") AccountCount=500.;
else if (Count=="A") AccountCount=1000.;
else if (Count=="B") AccountCount=2000.;
else { cout<<"选择错误"<<endl; return 0;}
if (OldAccountbook<AccountCount)
{ cout<<"存款余额不够!"<<endl; }
else
{ FoundItem.DeductBalance(AccountCount);
MyAccountbook.UpdateItem(FoundItem);
cout << "请取钱!" << endl;
ofstream OutputStream("Account.in");
MyAccountbook.StoreAccountbook(OutputStream);
}
}
else if (TransactionCode == "S"|| TransactionCode == "s")
{ cout<<"请输入存钱数量:";
cin>>SaveCount;
FoundItem.SaveAccount(SaveCount);
MyAccountbook.UpdateItem(FoundItem);
ofstream OutputStream("Account.in");
MyAccountbook.StoreAccountbook(OutputStream);
}
else if (TransactionCode == "U" || TransactionCode == "u")
{ cout<<"请输入修改后的密码:";
cin>>newPassword1;
cout<<"请再次输入修改后的密码:";
cin>>newPassword2;
if (newPassword1==newPassword2)
{ FoundItem.UpdatePassword(newPassword1);
MyAccountbook.UpdateItem(FoundItem);
ofstream OutputStream("Account.in");
MyAccountbook.StoreAccountbook(OutputStream);
cout<<"密码修改成功!";
}
else
cout<<"密码修改失败!";
}
return 0;
题目二、网上购书,可以下订单。
cpp
#include <iostream.h>
#include "strclass.h"
#include "buy.h"
#include "book.h"
class order
{ public:
order()
{ buyerID=0;
ordercount++;
orderID=ordercount; //定单编号自动加1
listcount=0;
}
void setbuyid(int b_id)
{ buyerID=b_id;
}
void buy_one_book(string b_id)
{ orderlist[listcount]=b_id;
listcount++;
}
void display();
private:
static int ordercount; //自动增加定单编号
int orderID; //订单编号
int buyerID; //购书人编号
string orderlist[20]; //书号数组
int listcount; //购书数量
};
void order::display()
{ int i;
cout<<"\n定单信息\n\n订单号:"<<orderID<<"\t";
cout<<"购书人编号:"<<buyerID<<"\n";
cout<<" 所购图书书号:";
for (i=0;i<listcount;i++)
cout<<orderlist[i]<<"\t";
cout<<endl;
}
int order::ordercount=0;
void main()
{ int i=0,j=0;
int buyerid,flag;
book *c[2];
layfolk b1("林小茶",1,"北京",0);
honoured_guest b2("王遥遥",2,.6,"上海",0);
member b3("赵红艳",3,5,"广州",0);
order o1[20]; //订单数组
buyer *b[3]= {&b1,&b2,&b3};
book c1("","C++ programing","谭浩强","清华",25);
book c2("A2","data structure","许天风","北大",20);
c[0]=&c1;
c[1]=&c2;
cout<<"购书人信息:\n\n";
for (i=0;i<3;i++)
b[i]->display();
cout<<"\n图书信息:\n\n";
for (i=0;i<2;i++)
c[i]->display();
while (j<2)
{ cout<<"\n\n请输入购书人编号:";
cin>>buyerid;
flag=0;
for (i=0;i<3;i++)
if (b[i]->getid()==buyerid) { flag=1;break;}
if (!flag) { cout<<"编号不存在"<<endl;}
else
{ b[i]->setpay(c[0]->getprice());
b[i]->setpay(c[1]->getprice());
cout<<endl<<"购书人需要付费:"<<b[i]->getpay()<<"\n\n";
o1[j].setbuyid(b[i]->getid());
o1[j].buy_one_book(c[0]->getbook_ID());
o1[j].buy_one_book(c[1]->getbook_ID());
o1[j].display(); j++;
}
}
}