Description
目前,汽车的能源主要有油类燃料动力(包括汽油、柴油)、电动力两种。现在定义四个类:
Car类,包括静态整型数据成员cntOfCars,用于记录产生的汽车对象的个数。一个double型属性,表示汽车的速度。定义其构造函数,其中输出信息见样例。定义setSpeed(double)方法用于设置汽车的速度。
OilCar类,是Car类的子类,包括静态整型数据成员cntOfOilCars,记录产生的燃油动力汽车对象的个数。一个double类型的属性,表示汽车百公里油耗。定义其构造函数,其中输出信息见样例。定义show()方法,用于输出如样例所示的信息。定义setOil(double)方法,用于设置汽车的油耗。
ElecCar类,是Car类的子类,包括静态整型数据成员cntOfElecCars,记录产生的电动力汽车对象的个数。一个double类型的属性,表示汽车百公里能耗(以KW为单位)。定义其构造和析构函数,其中输出信息见样例。定义show()方法,用于输出如样例所示的信息。定义setPower(double)方法,用于设置汽车的能耗。
CarHall类,用于展示上述两类汽车。所以它应该是OilCar和ElecCar类的组合类,包含1个OilCar类的对象数组,1个ElecCar类的对象数组,每个数组的长度均为10。定义getInfo()方法,用于从标准输入中获取汽车的各种信息;定义showInfo()信息,按照样例的格式输出所有汽车的信息。
Input
输入有多行。
第1行是一个正整数M,表示之后有M行输入,0<M<21。
之后的每行输入包括3部分:第1个是一个字符,之后是2个double类型的值。
如果第1个字符是’o’,则表示之后的两个值是一辆燃油动力汽车的速度和油耗。
如果第1个字符是’e’,则表示之后的两个值是一辆电动力汽车的速度和能耗。
其他字符则不是一辆合法的汽车。
Output
见样例。
Sample Input
5
o 100.4 12.13
e 100.3 120
f 200 1.3
o 200.14 22.34
e 80.1 234.4
Sample Output
The 1th car is created.
The 2th car is created.
The 1th oil-car is created.
The 3th car is created.
The 1th electrical car is created.
The 4th car is created.
The 2th oil-car is created.
The 5th car is created.
The 3th oil-car is created.
The 6th car is created.
The 4th oil-car is created.
The 7th car is created.
The 5th oil-car is created.
The 8th car is created.
The 6th oil-car is created.
The 9th car is created.
The 7th oil-car is created.
The 10th car is created.
The 8th oil-car is created.
The 11th car is created.
The 9th oil-car is created.
The 12th car is created.
The 10th oil-car is created.
The 13th car is created.
The 11th oil-car is created.
The 14th car is created.
The 2th electrical car is created.
The 15th car is created.
The 3th electrical car is created.
The 16th car is created.
The 4th electrical car is created.
The 17th car is created.
The 5th electrical car is created.
The 18th car is created.
The 6th electrical car is created.
The 19th car is created.
The 7th electrical car is created.
The 20th car is created.
The 8th electrical car is created.
The 21th car is created.
The 9th electrical car is created.
The 22th car is created.
The 10th electrical car is created.
The 23th car is created.
The 11th electrical car is created.
We have 2 oil-cars, which are:
An oil-car at speed of 100.4km/s, with 12.13L/100km.
An oil-car at speed of 200.14km/s, with 22.34L/100km.
We have 2 electrical cars, which are:
An electrical car at speed of 100.3km/s, with 120KW/100km.
An electrical car at speed of 80.1km/s, with 234.4KW/100km.
HINT
Append Code
int main()
{
Car car;
OilCar oilcar;
ElecCar eleccar;
CarHall carhall;
carhall.getInfo();
carhall.showInfo();
return 0;
}
Accepted Code
#include <iostream>
#include <string>
#include <cstdio>
#include <vector>
using namespace std;
class Car {
protected:
static int cntOfCars;
double V;
public:
Car() {
cntOfCars++;
cout << "The " << cntOfCars << "th car is created.\n";
}
~Car() {
//cout << "The " << cntOfCars << "th car is erased.\n";
cntOfCars--;
}
Car& setSpeed(double v) { V = v; return *this; }
};
int Car::cntOfCars = 0;
class OilCar : public Car {
protected:
static int cntOfOilCars;
double O;
public:
OilCar() {
cntOfOilCars++;
cout << "The " << cntOfOilCars << "th oil-car is created.\n";
}
~OilCar() {
// cout << "The " << cntOfOilCars << "th oil-car is erased.\n";
cntOfOilCars--;
}
void show() {
cout << "An oil-car at speed of " << V << "km/s, with " << O << "L/100km.\n";
}
OilCar& setOil(double o) { O = o; return *this; }
};
int OilCar::cntOfOilCars = 0;
class ElecCar : public Car {
protected:
static int cntOfElecCars;
double W;
public:
ElecCar() {
cntOfElecCars++;
cout << "The " << cntOfElecCars << "th electrical car is created.\n";
}
~ElecCar() {
//cout << "The " << cntOfElecCars << "th electrical car is erased.\n";
cntOfElecCars--;
}
void show() {
cout << "An electrical car at speed of " << V << "km/s, with " << W << "KW/100km.\n";
}
ElecCar& setPower(double w) { W = w; return *this; }
};
int ElecCar::cntOfElecCars = 0;
int num_a = 0;
int num_b = 0;
class CarHall {
private:
OilCar a[10];
ElecCar b[10];
public:
void getInfo() {
int M;
cin >> M;
for(int i = 0; i < M; i++) {
char ch;
cin >> ch;
//cout << ch << endl;
if(ch == 'o') {
double v, o;
cin >> v >> o;
a[num_a++].setOil(o).setSpeed(v);
} else if(ch == 'e') {
double v, w;
cin >> v >> w;
b[num_b++].setPower(w).setSpeed(v);
} else { //读取掉无用信息
double v, w;
cin >> v >> w;
}
}
}
void showInfo() {
cout << "We have " << num_a << " oil-cars, which are:\n";
for(int i = 0; i < num_a; i++) {
a[i].show();
}
cout << "We have " << num_b << " electrical cars, which are:\n";
for(int i = 0; i < num_b; i++) {
b[i].show();
}
}
};
int main()
{
Car car;
OilCar oilcar;
ElecCar eleccar;
CarHall carhall;
carhall.getInfo();
carhall.showInfo();
return 0;
}