停车场的收费系统:停车场有3类汽车,每种收费也不一样,要求输入汽车类型和入库、出库时间,输出应交的停车费。假设停车时间不超过24小时。

文章提供了一个C语言和C++实现的停车场收费系统程序。用户输入车辆类型(C轿车,B客车,T卡车)、入库和出库时间,程序根据不同的车辆类型和停车时长计算费用。C++版本中使用了面向对象编程,定义了Vehicle基类和Car,Bus,Truck派生类,每个类覆盖了计算费用的方法。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

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;
}

在这里插入图片描述

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值