6-158 停车场收费问题 (20分)

东软学习小组成员:夜枫

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

裁判测试程序样例:


#include 
#include 
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,其他字符则为未知类型。

3
c 40
t 5
b 40

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

type:car minute:40 fee:8
type:truck minute:5 fee:15
type:bus minute:40 fee:10
sum=33

部分代码:

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;

全部代码:

#include 
#include 
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通过测试:
6-158 停车场收费问题 (20分)_第1张图片

你可能感兴趣的:(c++PTA练习)