首页 > 试题广场 >

根据如下程序,写出网上购书程序,模拟下订单的操作。每张订单的

[问答题]
根据如下程序,写出网上购书程序,模拟下订单的操作。每张订单的数据成员如下:
class order{
 public:
 .
 .
 .
 private:
 static int ordercount;//自动增加订单编号
 int orderID;//订单编号
 int buyerID;//购书人编号
 int listcount;//购书数量
 string orderlist[20];//记录书号的数组
};
//buy.h文件开始
class buyer{//基类
 protected;
 string name;//姓名
 int buyerID;//购书人编号
 string address;//地址
 double pay;//购书金额
 public:
 buyer();
 buyer(string n,int b,string a,double p);
 string getbuyname();//取姓名
 string getaddress();//取地址
 double fetpay();//取应付金额
 int fetid();//取购书人编号
 virtual void display()=0;//显示函数
 virtual void setpay(double=0)=0;//计算购书金额
};
class member:public buyer{//会员类
 int leaguer_grade;//会员级别
 public:
 member(string n,int b,int l,string a,double p):buyer(n,b,a,p)
 {leaguer_grade=1;}//构造函数
 void display();//显示函数
 void setpay(double p);
};
class honoured_guest:public buyer{//贵宾类
 double discount_rate;//折扣率
 public:
 layfolk(strring n,int b,string a,double p):buyer(n,b,a,p)
 {}//构造函数
 void display();//显示函数
 void setpay(double p);//计算够数金额
};
buyer::buyer(){//基类的构造函数
name="";
buyerID=0;
address="";
pay=0;
}
buyer::buyer(string n,int b,string a,double p){//基类的构造函数
 name=n;
 buyerID=b;
 address=a;
 pay=p;
}
double buyer::getpay(){//收购书金额
 return pay;
}
string buyer::getaddress(){//收购书人地址
 return address;
}
string buyer::getbuyname(){//收购书人名字
 return name;
}
int buyer::getid(){//收购书人编号
 return buyerID;
}
void member::display(){//会员类的显示函数
 cout<<"购书人姓名:"<<name<<"\t";
 cout<<"购书人编号:"<<buyerID<<"\t";
 cout<<"购书人为会员,级别:"<<leaguer_grade<<"\n";
 cout<<"地址:"<<address<<"\n";
}
void member::setpay(double p){//会员类的计算购书金额
 if(leaguer_grade==1)//会员级别为1
  pay=.95*p+pay;
 else if(leaguer_grade==2)//会员级别为2
  pay=.90*p+pay;
 else if(leaguer_grade==3)//会员级别为3
  pay=.85*p+pay;
 else if(leaguer_grade==4)//会员级别为4
  pay=.8*p+pay;
 else if(leaguer_grade==5)//会员级别为5
  pay=.7*p+pay;
 else
 cout<<"级别错误!";
}
void honoured_guest::display(){//贵宾类的显示函数
 cout<<"购书人姓名:"<<name<<"\t";
 cout<<"购书人编号:"<<buyerID<<"\t";
 cout<<"购书人为贵宾!折扣率为:"<<discount_rate*100<<"%\n";
 cout<<"地址:"<<address<<"\n\n";
}
void honoured_guest::setpay(double p){//贵宾类计算购书金额
 pay=pay+(1-discount_rate)*p;
}
void layfolk::display(){//普通类的显示函数
 cout<<"购书人姓名:"<<name<<"\t";
 cout<<"购书人编号:"<<buyerID<<"\t";
 cout<<"购书人为普通人<<"\n";
 cout<<"地址:"<<address<<"\n\n";
}
void layfolk::setpay(double p){//普通类计算购书金额
 pay=pay+p;
}
//buy.h文件结束
//book.h文件开始
class book{//图书类
 protected:
 string book_ID;//书号
 string book_name;//书名
 string author;//作者
 string publishing;//出版社
 double price;//定价
 public:
 book();//构造函数
 book(string b_id,string b_a,string au,string pu,double pr);//重载构造函数
 void display();
 string getbook_ID();//取书号
 string getbook_name();//取书名
 string getauthor();//取作者
 string getpublishing();//取出版社
 double getprice();//取定价
};
book::book(string b_id,string b_n,string su,string pu,double pr){
 book_ID=b_id;//书号
 book_name=b_n;//书名
 author=au;//作者
 publishing=pu;//出版社
 price=pr;//定价
}
book::book(){
 book_ID="";//书号
 book_name="";//书名
 author="";//作者
 publishing="";//出版社
 price=0;//定价
}
void book::display(){
 cout<<"书号:"<<book_ID<<"\t";
 cout<<"书名:"<<book_name<<"\t";
 cout<<"作者:"<<author<<"\t";
 cout<<"出版社:"<<publishing<<"\t";
 cout<<"定价:"<<price<<"\t";
}
string book::getbook_ID(){
 return book_ID;//取书号
}
string book::getbook_name(){
 return book_name;//取书名
}
string book::getauthor(){
 return author;//取作者
}
string book::getpublishing(){
 return publishing;//取出版社
}
double book::getprice(){
 return price;//取定价
}

推荐
程序中包括了3个文件:buy.h、book.h、和buy_book.cpp文件。buy_book.cpp程序中有main函数
本题采用C++提供的新的数据类型字符串类类型(string类类型)来编程。使用string类时,必须在程序的开头将C++标准库中的string头文件包含进来,即应加上#include<strinq>//注意头文件名不是string.h
完整程序及相应的说明如下:
//buy_book.cpp文件开始
#include<string>
#include<iostream>
using namespace std;
#include"buy.h"
#include"book.h"
class crder{
 public:
 crder(){
 buyerID=0;
 ordercount++;
 orderID=ordercount;//订单编号自动加1
 listcount=0;
 }
 void setbuyid(int b_id){
 buyerID=b_id;
 }
 void buy_one_book(string_id){
 orderlist[listcount]=b_id;
 listcount++;
 }
 void display();
 private:
 static int ordercount;//自动增加订单编号
 int orderID;//订单编号
 int buyerID;//购书人编号
 int listcount;//购书数量
 string orderlist[20];//记录书号的数组
};
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 bl("林小茶",1,"北京",0);
 honoured_guest b2("王瑶瑶",2,.6,"上海",0);
 member b3("赵红艳",3,5,"广州",0);
 order 01[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";
  c1[j].setbuyid(b[i]->getid());
  c1[j].buy_one_book(c[0]->getbook_ID());
  c1[j].buy_one_book(c[1]->getbook_ID());
  c1[j].display();j++;
  }
}
}

发表于 2018-05-05 22:17:33 回复(0)