请设计一个公路收费程序。要求根据输入的车型(1:小客车 2:卡车 3:公汽)统计收费金额
时间: 2024-05-12 08:20:30 浏览: 11
输入:车型(1:小客车 2:卡车 3:公汽)
输出:收费金额
程序设计如下:
```
car_type = int(input("请输入车型(1:小客车 2:卡车 3:公汽):"))
if car_type == 1:
fee = 5
elif car_type == 2:
fee = 10
elif car_type == 3:
fee = 15
else:
print("输入有误,请重新输入")
fee = 0
print("收费金额为:", fee, "元")
```
解释:
首先通过 `input()` 函数获取用户输入的车型,使用 `int()` 函数将输入转换为整数类型。然后使用 `if...elif...else` 语句对不同的车型进行区分,并计算出相应的收费金额。如果用户输入的车型不是 1、2、3 中的任何一个,就输出提示信息并设置收费金额为 0。最后使用 `print()` 函数输出收费金额。
相关问题
汽车收费\ncar的收费公式为: 载客数*8+重量*2\n\ntruck的收费公式为:重量*5\n\nbus的收费公式为: 载客数*3\n\n输入格式:每个测试用例占一行
这个问题的意思是:给定不同类型的车(汽车,卡车,公交车),它们的收费公式是多少?
汽车的收费公式是:乘客数 × 8 + 重量 × 2。
卡车的收费公式是:重量 × 5。
公交车的收费公式是:乘客数 × 3。
你需要按照每行一个测试用例来输入。
用c加加写一个基类:过境人员,派生类:游客、卡车司机、商务旅客的长代码,要求用到封装 继承和多态
以下是基类和派生类的代码:
```c++
// 过境人员基类
class TransitPerson {
protected:
string name;
int age;
public:
TransitPerson(string n, int a) : name(n), age(a) {}
virtual void showInfo() {
cout << "Name: " << name << endl;
cout << "Age: " << age << endl;
}
};
// 游客派生类
class Tourist : public TransitPerson {
private:
string destination;
public:
Tourist(string n, int a, string d) : TransitPerson(n, a), destination(d) {}
void showInfo() {
cout << "Tourist Information:" << endl;
TransitPerson::showInfo();
cout << "Destination: " << destination << endl;
}
};
// 卡车司机派生类
class TruckDriver : public TransitPerson {
private:
string licensePlate;
public:
TruckDriver(string n, int a, string l) : TransitPerson(n, a), licensePlate(l) {}
void showInfo() {
cout << "Truck Driver Information:" << endl;
TransitPerson::showInfo();
cout << "License Plate: " << licensePlate << endl;
}
};
// 商务旅客派生类
class BusinessTraveler : public TransitPerson {
private:
string company;
public:
BusinessTraveler(string n, int a, string c) : TransitPerson(n, a), company(c) {}
void showInfo() {
cout << "Business Traveler Information:" << endl;
TransitPerson::showInfo();
cout << "Company: " << company << endl;
}
};
```
这里使用了封装、继承和多态的概念。基类 `TransitPerson` 封装了过境人员的基本信息,派生类 `Tourist`、`TruckDriver` 和 `BusinessTraveler` 继承了基类,并重写了 `showInfo()` 方法,实现了多态。