当前位置:网站首页>PTA:7-31 期刊收费
PTA:7-31 期刊收费
2022-06-23 03:47:00 【Sy_Faker】
邮局提供两种期刊的订阅:杂志和报纸。 给出下面基类的框架:
class Periodical {
protected:
string title; //名称
public:
virtual void display()=0;//打印收费
}
以Periodical为基类,构建Magazine和Newspaper类。
生成上述类并编写主函数,要求主函数中有一个基类Periodical指针数组,数组元素不超过10个。
Periodical *pp[10];
主函数根据输入的信息,相应建立Magazine, Newspaper类对象,对于Magazine给出订阅期数和每期价格,对于Newspaper给出订阅周数,每周出版次数和每份价格。
输入格式:每个测试用例占一行,第一项为类型,1为Magazine,2为Newspaper,第二项是名称,第三项是单价,Magazine的第四项是期数,Newspaper的第四项是订阅周数,第五项是每周出版次数。
输出时,依次打印各期刊的名称和收费(小数点后保留一位)。
输入样例:
1 AAA 12.8 6
1 BB 15 3
2 CCCC 2.1 16 3
2 DD 0.7 55 7
1 EEE 18 3
0
输出样例:
AAA 76.8
BB 45.0
CCCC 100.8
DD 269.5
EEE 54.0
#include<iostream>
#include<cstring>
#include<iomanip>
using namespace std;
class Periodical
{
protected:
string title;
public:
virtual void display()=0;//纯虚函数
};
class Magazine:public Periodical
{
int num;
double price;
public:
Magazine(string p,double a,int b)
{
title=p;//字符串之间的赋值,下同
price=a;
num=b;
}
virtual void display()//输出,保留小数点后一位的做法,下同
{
cout<<setiosflags(ios::fixed)<<title<<setprecision(1)<<" "<<num*price<<endl;
}
};
class Newspaper:public Periodical
{
int num;
int times;
double price;
public:
Newspaper(string p,double a,int b,int t)
{
times=t;
num=b;
price=a;
title=p;
}
virtual void display()
{
cout<<setiosflags(ios::fixed)<<title<<" "<<setprecision(1)<<num*price*times<<endl;
}
};
int main()
{
Periodical *pp[10];//创建基类指针数组
int k,b,t,count=0;
string str;
double a;
cin>>k;
getchar();//记得要把回车"消除"
while(k!=0)
{
switch(k)//用switch来判断属于哪一类
{
case 1:
cin>>str>>a>>b;
pp[count]=new Magazine(str,a,b);//派生后初始化
break;
case 2:
cin>>str>>a>>b>>t;
pp[count]=new Newspaper(str,a,b,t);
break;
}
pp[count]->display();//直接调用即可输出
cin>>k;
getchar();
count++;//使用下一个指针
}
return 0;
}
边栏推荐
- Google Earth engine (GEE) - long time series monthly VCI data extraction, analysis and area calculation (Mexico as an example)
- [leetcode] sum of two numbers II
- Efficient remote office experience | community essay solicitation
- Section 2: spingboot unit test
- Bug STM32 advanced timer (haha, to tell you the truth, the hardware timer can't reflect its strength. In fact, I want to send the kernel timer. Just think about it. Take your time)
- flutter系列之:flutter中的Wrap
- Analysis on the current situation of the Internet of things in 2022
- Pytorch---使用Pytorch的预训练模型实现四种天气分类问题
- 支持在 Kubernetes 运行,添加多种连接器,SeaTunnel 2.1.2 版本正式发布!
- Mysql, field problem
猜你喜欢

Redis启动有问题

【一起上水硕系列】Day Three - preview4

怎么用好MySQL索引

QMainWindow

两招提升硬盘存储数据的写入效率

Black horse PostgreSQL, why is it black in the end

node+express如何操作cookie

AI video cloud vs narrowband HD, who is the favorite in the video Era

A summary of PostgreSQL data types. All the people are here

Deploying Apache pulsar on kubesphere
随机推荐
Full analysis of embedded software testing tool tpt18 update
Redis启动有问题
What is the difference between redistemplate and CacheManager operation redis
P1363 幻象迷宫(dfs)
After Huawei online battle service players quickly match, different players receive different lists of players in the same room
理想汽车×OceanBase:当造车新势力遇上数据库新势力
Common events for elements
Preface
高效的远程办公经验 | 社区征文
QMainWindow
背景彩带动画插件ribbon.js
【二叉树进阶】AVLTree - 平衡二叉搜索树
Avltree - arbre de recherche binaire équilibré
node+express如何操作cookie
Analysis on the current situation of the Internet of things in 2022
PTA:7-69 数据的间距问题
Prince language under insect date category
How the innovative use of adobe international certification 𞓜 3D changes the entire industry
How to ensure application security
在 KubeSphere 上部署 Apache Pulsar