C/C++语言程序设计题目
设计一个停车场的收费系统。停车场有3类汽车,分别用3个字母表示:C代表轿车,B代表客车, T代表卡车。收费标准如下表所示。
车辆类型 收费标准
轿车 3小时内,每小时5元。3小时后,每小时10元
客车 2小时内,每小时10元。2小时后,每小时15元
卡车 1小时内,每小时10元。1小时后,每小时15元
要求输入汽车类型和入库、出库时间,输出应交的停车费。假设停车时间不超过24小时。
记得点赞关注博主,有能力小小打赏一下。谢谢。
C语言设计编程代码一
#include <stdio.h>
int main() {
char type;
int hours,a,b;
double fee;
printf("请输入车辆类型(C代表轿车,B代表客车,T代表卡车):\n");
scanf("%c", &type);
printf("请输入入库时间点整数(小时):\n");
scanf("%d", &a);
printf("请输入出库时间点整数(小时):\n");
scanf("%d", &b);
hours=b-a;
switch (type) {
case 'C':
if (hours <= 3) {
fee = hours * 5.0;
} else {
fee = 15.0 + (hours - 3) * 10.0;
}
break;
case 'B':
if (hours <= 2) {
fee = hours * 10.0;
} else {
fee = 20.0 + (hours - 2) * 15.0;
}
break;
case 'T':
if (hours <= 1) {
fee = hours * 10.0;
} else {
fee = 10.0 + (hours - 1) * 15.0;
}
break;
default:
printf("输入的车辆类型不正确!\n");
return 1;
}
printf("应交停车费用为:%.2f元。\n", fee);
return 0;
}
运行结果
C++设计编程代码一
#include <iostream>
#include <string>
using namespace std;
class Vehicle {
protected:
string type; // 车辆类型
int hours; // 停车时间(小时)
public:
Vehicle(string t, int h) : type(t), hours(h) {
} // 构造函数
virtual float getFee() = 0; // 纯虚函数,计算停车费用
};
class Car : public Vehicle {
public:
Car(int h) : Vehicle("C", h) {
} // 构造函数
float getFee() {
// 计算停车费用
if (hours <= 3) {
return hours * 5.0;
} else {
return 3 * 5.0 + (hours - 3) * 10.0;
}
}
};
class Bus : public Vehicle {
public:
Bus(int h) : Vehicle("B", h) {
} // 构造函数
float getFee() {
// 计算停车费用
if (hours <= 2) {
return hours * 10.0;
} else {
return 2 * 10.0 + (hours - 2) * 15.0;
}
}
};
class Truck : public Vehicle {
public:
Truck(int h) : Vehicle("T", h) {
} // 构造函数
float getFee() {
// 计算停车费用
if (hours <= 1) {
return hours * 10.0;
} else {
return 1 * 10.0 + (hours - 1) * 15.0;
}
}
};
int main() {
char type;
int inTime, outTime;
cout << "欢迎使用停车场收费系统!" << endl;
cout << "请输入车辆类型(C/B/T):";
cin >> type;
cout << "请输入入库时间(小时):";
cin >> inTime;
cout << "请输入出库时间(小时):";
cin >> outTime;
Vehicle* vehicle;
switch (type) {
case 'C':
vehicle = new Car(outTime - inTime);
break;
case 'B':
vehicle = new Bus(outTime - inTime);
break;
case 'T':
vehicle = new Truck(outTime - inTime);
break;
default:
cout << "无效的车辆类型!" << endl;
return 0;
}
float fee = vehicle->getFee();
cout << "应交的停车费为:" << fee << "元。" << endl;
return 0;
}