`#include
include
include // 用于设置输出精度
using namespace std;
class Car {
private:
string brand;
string model;
string color;
double price;
public:
// 构造函数
Car(string b, string m, string c, double p)
: brand(b), model(m), color(c), price(p) {}
// 计算保险费
double calculateInsurance() {
return price * 0.05;
}
// 计算油耗
double calculateFuelConsumption(int kilometers) {
return kilometers / 100.0 * 8;
}
// 输出汽车信息
void printInfo(int kilometers) {
double insurance = calculateInsurance();
double fuelConsumption = calculateFuelConsumption(kilometers);
cout << brand << "-" << model << "汽车的保险费为" << fixed << setprecision(0) << insurance << "元,";
cout << "运行" << kilometers << "公里油耗值为" << fixed << setprecision(1) << fuelConsumption << "升" << endl;
}
};
int main() {
string brand, model, color;
double price;
int kilometers;
cout << "请输入汽车的品牌、型号、颜色和价格(空格分隔):" << endl;
cin >> brand >> model >> color >> price;
cout << "请输入驾驶公里数:" << endl;
cin >> kilometers;
// 创建Car对象
Car car(brand, model, color, price);
// 输出汽车信息,包括保险费和油耗
car.printInfo(kilometers);
return 0;
}
`