给出下面一个基类的框架:
class Publication
{
protected:
string title;//名称
float price;//原价
int day;//租期
public:
virtual void display()=0;//打印价格清单
}
以Publication为基类,构建Book和Tape类。
生成上述类并编写主函数,要求主函数中有一个基类Publication指针数组,数组元素不超过10个。
Publication pp[10];
主函数根据输入的信息,相应建立Book, Tape类对象。
它们的原始租金为:租期1.2。
另外,实际收取的租金不超过2倍的租品估价。
Book的估价为:新旧程度*原价。
Tape的估价为:原价/(1+已出租次数/3)。
输入格式:每个测试用例占一行,第一项为租品类型,1为Book,2为Tape.接下来为名称、原价、租期。最后一项Book是新旧程度(0.1至1),Tape是已出租次数。以0表示输入的结束。
要求输出名称,原始租金(小数点后保留1位小数),如果原始租金大于2倍租品估价,则同时给出实际应收取的租金(小数点后保留1位小数),并在最后标明R。
输入样例
1 AAA 19.5 3 0.5
1 BBB 9.5 2 0.1
2 AA 10 2 0
2 DDDD 12.5 2 38
1 FFF 42 3 0.1
0
输出样例
AAA 3.6
BBB 2.4 1.9 R
AA 2.4
DDDD 2.4 1.8 R
FFF 3.6
#include<iostream>
#include<stdio.h>
using namespace std;
class pu
{
protected:
string title;//名称
float price;//原价
int day;//租期
public:
pu(string title,float price,int day):title(title),price(price),day(day){
}
virtual void display()=0;//打印价格清单
};
class book:public pu{
private:
float xin;
public:
book(string title,float price,int day,float xin):pu(title,price,day),xin(xin){
}
void display(){
cout<<title<<" ";
if(xin*price*2>=day*1.2){
cout<<day*1.2<<endl;
}
else {
cout<<day*1.2<<" ";printf("%.1lf ",xin*price*2);
cout<<"R"<<endl;
}
}
};
class tape:public pu{
private:
int ci;
public:
tape(string title,float price,int day,int ci):pu(title,price,day),ci(ci){
}
void display(){
cout<<title<<" ";
if(price/(1+ci/3.0)>=day*1.2){
cout<<day*1.2<<endl;
}
else {
cout<<day*1.2<<" ";printf("%.1lf ",2*(price/(1+ci/3.0)));
cout<<"R"<<endl;
}
}
};
int main()
{
pu *p[10];
int n;string name;
float price;//原价
int day;//租期
int ci;
float xin;int i=0;
while(1){
cin>>n;
if(n==0)break;
cin>>name>>price>>day;
switch(n){
case 1:{
cin>>xin;
p[i]=new book(name,price,day,xin);
break;
}
case 2:{
cin>>ci;
p[i]=new tape(name,price,day,ci);
break;
}
}i++;
}
for(int j=0;j<i;j++){
p[j]->display();
}
}版权声明:本文为ITPluto原创文章,遵循CC 4.0 BY-SA版权协议,转载请附上原文出处链接和本声明。