在停车场收费系统中,收费者会根据车型的不同按不同的单价和计费方式收取不同的停车费,其中:
轿车Car:每小时8元,超过30分钟按一小时计算,未超过30分钟不收费
客车Bus:每小时10元,30分钟以内按半小时计费,未达1小时但超过30分钟按1小时计算
货车Truck:每小时15元,未达1小时的按1小时计算
其他未知类型:按默认方法计算,见裁判程序。
请补充必要的类和函数定义,使停车场类Parker可收取停入的n辆车的费用。

裁判测试程序样例:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46

#include <iostream>
#include <string>
using namespace std;
const float Car_Price= 8;
const float Bus_Price= 10;
const float Truck_Price=15;
class Vehicle{
protected:
float price;
int minute;
public:
virtual float computeFee(){return 20.0f*minute/60;};
virtual string getType(){return "unknow"; };
float getPrice();
void setPrice(float _price);
int getMinute();
void setMinute(int _minute);
Vehicle(float _price,int _minute);
Vehicle(){
};
};
/* 你的代码将被嵌入这里 */
int main(){
int n,minute; //停车辆次,时长
char type;
Vehicle *vp;
Parker p;
cin>>n;

for(int i=1;i<=n;i++){
cin>>type>>minute;
switch(type){
case 'c':vp=new Car(minute);break;
case 't':vp=new Truck(minute);break;
case 'b':vp=new Bus(minute);break;
default: vp=new Vehicle();vp->setMinute(minute);
}
cout<<"type:"<<vp->getType() <<" minute:"<<vp->getMinute()<<" fee:"<<vp->computeFee()<<endl;
p.park(vp);
delete vp;
}
cout<<"sum="<<p.getSum()<<endl;
return 0;
}

输入样例:
输入的第一行为进入停车场的车辆总数n. 接下来有n行输入,每行为一辆车的类型及停车时长,时长单位为分钟,类型’c’代表Car,类型’b’代表Bus,类型’t’代表Truck,其他字符则为未知类型。

1
2
3
4
3
c 40
t 5
b 40

输出样例:
输出为n+1行,前n行输出每辆车的类型,停车时长,收取的费用,最后一行输出目前停车场得到的停车费总额。

1
2
3
4
type:car minute:40 fee:8
type:truck minute:5 fee:15
type:bus minute:40 fee:10
sum=33

部分代码:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
float Vehicle::getPrice(){
return price;
}
int Vehicle::getMinute(){
return minute;
}
void Vehicle::setPrice(float _price){
price=_price;
}
Vehicle::Vehicle(float _price,int _min){
price=_price;
minute=_min;
}
void Vehicle::setMinute(int _minute){
minute=_minute;
}
class Car:public Vehicle{
public:
Car(int _minute){
minute=_minute;
price=Car_Price;
};
virtual float computeFee(){
double time=(minute+30)/60;
return price*time;
};
virtual string getType(){return "car"; };
};
class Bus:public Vehicle{
public:
Bus(int _minute){
minute=_minute;
price=Bus_Price;
};
virtual float computeFee(){
double time=minute/30;
if(time*30<minute){
time+=1;
}
return (price/2)*time;
};
virtual string getType(){return "bus"; };
};
class Truck:public Vehicle{
public:
Truck(int _minute){
price=Truck_Price;
minute=_minute;
};
virtual float computeFee(){
double time=(minute)/60;
if(time*60<minute){
time+=1;
}
return price*time;
};
virtual string getType(){return "truck"; };
};
class Parker{

public:
static double sum;
void park(Vehicle *vp){
sum=sum+vp->computeFee();
}
double getSum(){
return sum;
}
};
double Parker::sum=0;

全部代码:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
#include <iostream>
#include <string>
using namespace std;
const float Car_Price= 8;
const float Bus_Price= 10;
const float Truck_Price=15;
class Vehicle{
protected:
float price;
int minute;
public:
virtual float computeFee(){return 20.0f*minute/60;};
virtual string getType(){return "unknow"; };
float getPrice();
void setPrice(float _price);
int getMinute();
void setMinute(int _minute);
Vehicle(float _price,int _minute);
Vehicle(){
};
};
float Vehicle::getPrice(){
return price;
}
int Vehicle::getMinute(){
return minute;
}
void Vehicle::setPrice(float _price){
price=_price;
}
Vehicle::Vehicle(float _price,int _min){
price=_price;
minute=_min;
}
void Vehicle::setMinute(int _minute){
minute=_minute;
}
class Car:public Vehicle{
public:
Car(int _minute){
minute=_minute;
price=Car_Price;
};
virtual float computeFee(){
double time=(minute+30)/60;
return price*time;
};
virtual string getType(){return "car"; };
};
class Bus:public Vehicle{
public:
Bus(int _minute){
minute=_minute;
price=Bus_Price;
};
virtual float computeFee(){
double time=minute/30;
if(time*30<minute){
time+=1;
}
return (price/2)*time;
};
virtual string getType(){return "bus"; };
};
class Truck:public Vehicle{
public:
Truck(int _minute){
price=Truck_Price;
minute=_minute;
};
virtual float computeFee(){
double time=(minute)/60;
if(time*60<minute){
time+=1;
}
return price*time;
};
virtual string getType(){return "truck"; };
};
class Parker{

public:
static double sum;
void park(Vehicle *vp){
sum=sum+vp->computeFee();
}
double getSum(){
return sum;
}
};
double Parker::sum=0;
int main(){
int n,minute; //停车辆次,时长
char type;
Vehicle *vp;
Parker p;
cin>>n;

for(int i=1;i<=n;i++){
cin>>type>>minute;
switch(type){
case 'c':vp=new Car(minute);break;
case 't':vp=new Truck(minute);break;
case 'b':vp=new Bus(minute);break;
default: vp=new Vehicle();vp->setMinute(minute);
}
cout<<"type:"<<vp->getType() <<" minute:"<<vp->getMinute()<<" fee:"<<vp->computeFee()<<endl;
p.park(vp);
delete vp;
}
cout<<"sum:"<<p.getSum()<<endl;
return 0;
}

pta通过测试:
在这里插入图片描述