【问题标题】:Parking Fee Program停车费计划
【发布时间】:2017-03-02 06:40:32
【问题描述】:

在给出以下信息时,编写一个程序来计算将汽车停在停车场的客户的停车费:

一个。显示车辆类型的字符:C 代表汽车,B 代表公共汽车,T 代表卡车

b.一个介于 0 到 24 之间的整数,表示车辆进入停车场的时间。

c。一个介于 0 到 60 之间的整数,表示车辆进入停车场的分钟数。

d。一个介于 0 到 24 之间的整数,表示车辆离开停车场的时间。

e。一个介于 0 到 60 之间的整数,表示车辆离开停车场的分钟数。

因为这是一个公共地段,所以我们鼓励人们只在很短的时间内停车。管理层对每种类型的车辆使用两种不同的费率。

午夜过后,停车场内不允许有车辆停留;它会被拖走。停车费还有 6% 的 GST。

g.编写一个程序 i. 介绍信息的显示 ii.提示用户输入相关信息。 iii.使用以下格式显示帐单。

h。您的计划将包括以下标准。 一世。验证时间进入和超时。 ii.使用 switch 语句来区分不同类型的车辆。 iii.使用适当的循环语句允许重复计算停车费 iv.使用表 1 使用适当的测试数据运行您的程序五次

#include <stdio.h>
#include <stdlib.h>

int main(void)
{
    char type; //Variable for vehicle types
    int hourIn, minuteIn, hourOut, minuteOut, entry, exit, totalParkingTime; //Variable for time
    float totalRounded, totalChargeFee, GST; //Variable for fare

    printf("Welcome to Help Parking Lot!\n"); //Introduction message

    printf("Enter type of vehicle: %c", type); //Type of vehicles: C for car, T for truck, B for bus
    scanf("%c", &type);

    switch(type)
    {
        case 'C':
            if(totalParkingTime <= 3)
                totalChargeFee = 0.8 * totalParkingTime;
            else
                totalChargeFee = 0.8 * 3 + 1.5 * (totalParkingTime - 3);
            break;

        case 'T':
            if(totalParkingTime <= 2)
                totalChargeFee = 1.5 * totalParkingTime;
            else
                totalChargeFee = 1.5 * 2 + 2.3 * (totalParkingTime - 2);
            break;

        case 'B':
            if(totalParkingTime <= 1)
                totalChargeFee = 2 * totalParkingTime;
            else
                totalChargeFee = 2 * 1 + 3.4 * (totalParkingTime - 1);
            break;
    }
    scanf("%f", &totalChargeFee);

    printf("Enter an integer between 0 and 24 showing the hour the vehicle entered the lot: %d", hourIn); //The hour of veicle enter in military format
    scanf("%d", &hourIn);
    printf("Enter an integer between 0 and 60 showing the minute the vehicle entered the lot: %d", minuteIn); //The minute of vehicle enter in military format
    scanf("%d", &minuteIn);
    printf("Enter an integer between 0 and 24 showing the hour the vehicle exited the lot: %d", hourOut); //The hour of vehicle exit in military format
    scanf("%d", &hourOut);
    printf("Enter an integer between 0 and 60 showing the minute the vehicle exited the lot: %d", minuteOut); //The minute of vehicle exit in military format
    scanf("%d", &minuteOut);

    entry = hourIn + minuteIn;
    scanf("%d", &entry);
    exit = hourOut + minuteOut;
    scanf("%d", &exit);
    totalParkingTime = exit - entry;

     //User's bill is shown here
    printf("HELP PARKING LOT CHARGE\n Type of vehicle: %c\n TIME-IN\n \t\t\t %d:%d\n TIME-OUT\n \t\t\t %d:%d\n \t\t\t------\n PARKING TIME %d:%d\n ROUNDED TOTAL \t\t\t%f\n \t\t\t------\n TOTAL CHARGE \t\t RM%.2f\n GST \t\t\t RM%.2f\n TOTAL \t\t\t RM%.2f");

    return 0;
}

我不知道如何用小时和分钟的差异以及车辆的类型来计算进出时间。当我运行程序时,时间的输入有错误。但是显示格式是正确的。

【问题讨论】:

  • 一个小时有60分钟......
  • 这不是一个真正的编程问题。你需要先在纸上弄清楚。您需要从分钟中减去分钟。如果结束时间太小,您可以将结束时间中的一小时变成 60 分钟...
  • 最简单的方法是将分钟 + 小时更改为自午夜以来的小时和分数
  • @A.nonymous 你甚至没有尝试为totalhours 计算任何东西。你会在现实中做什么?从 12:45 到 14:30 是几个小时?你将如何计算这个?将完全相同的计算和考虑因素放入 C 代码中有什么问题?
  • 我之前从另一个用户那里看到过这个问题。这似乎是家庭作业。我们确实要求学生在帮助他们之前付出很大的努力。 cmets 建议他自己多思考一下。

标签: c if-statement switch-statement codeblocks


【解决方案1】:

你是否意识到:

if(type == 'C' && totalHourParked <= 3)
{
    totalChargeFee = 0.8 * totalHourParked;
}
else
{
     totalChargeFee = 1.5 * totalHourParked;
}

所有B&T类型都会进入else语句?

在尝试编码之前,我鼓励您拿起纸和铅笔并尝试编写某种伪代码。之后,尝试手动测试它。如果您对它有信心,请对其进行编码。测试一下。如果这些都不起作用,那么将您的问题暴露给stackoverflow。

【讨论】:

    【解决方案2】:

    首先你必须#include&lt;time.h&gt; 然后使用 difftime() 其中包括两个参数 end_time 和 start_time difftime(end_time, start_time); 但您必须将您的 hourIn、minIn 和 hourOut、minOut 转换为时间并将它们放入 start_time 变量和 end_time 变量中。 得到时差后就可以计算出totalChargeFee

    【讨论】:

    • 如果他已经有了小时和分钟,他为什么还要操心“time.h”中的函数呢?立即计算比转换任何时间值更容易
    • 我假设您可以在没有计算机的情况下回答它。那么你将如何在纸上做呢?
    【解决方案3】:

    C 库函数 double difftime(time_t time1, time_t time2) 返回 time1 和 time2 之间的秒差,即 (time1 - time2)。

    double difftime(time_t time1, time_t time2)
    

    假设如果汽车在 0630 小时进出,在 2130 小时出,那么 totalHourParked 是 15 对吗?

    if(type == 'C' && totalHourParked <= 3)
    {
      totalChargeFee = 0.8 * totalHourParked;
    }
    else if((type == 'C' && totalHourParked > 3))
    {
      totalChargeFee = 0.8 * 3 + 1.5 * (totalHourParked-3);    //first 3 hours charge 0.8 and the rest is charged with 1.5      
    }
    else if...
    ...
    ...
    else if...
    

    在这种情况下,您必须使用 elseif elseif elseif 才能进入正确的分支,因为您使用的是 && ,它仅在两个条件都满足时才执行 true 部分。

    改用开关盒。

    switch(type){
    case 'C': if(totalHourParked <= 3)
                totalChargeFee = 0.8 * totalHourParked;   
              else
                totalChargeFee = 0.8 * 3 + 1.5 * (totalHourParked-3);
              break;
    case 'T': ...
    case 'B': ...
    }
    

    【讨论】:

    • 哇。你是对的 kuharan bhowmilk。这也是一个很好的信息,我不知道你也可以在 switch 语句中使用 if。谢谢我稍后会在我的代码中实现它并告诉它是否有效。谢谢
    • 在不使用函数的情况下如何进行时间进出?
    • 有几种方法可以做到这一点。它不是编程的东西。它的逻辑。用笔和纸解决,然后编码。
    • 我很理解这个问题。我的问题在于编码。我又要失败了
    • 我尝试了带有 switch 和 if 语句的车辆类型,但我不知道如何做时间部分
    【解决方案4】:

    你在问这个: 由于车辆不会在午夜后停留,因此您可以直接计算,因为 hourOut 将始终高于 hourIn

    totalHourParked = hourOut - hourIn;
    totalMinParked = minOut - minIn;
    
    if(totalMinParked < 0)
    {
        totalHourParked --;
        totalMinParked = 60 - totalMinParked;
    }
    

    totalHourParked 有小时数,totalMinParked 有分钟数。

    【讨论】:

    • 分钟有误。如果时间是 14:50 - 15:30,您将获得 1 小时 20 分钟。应该是 1 小时 -20 分钟 == 40 分钟
    猜你喜欢
    • 2019-08-11
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2016-11-10
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2014-07-08
    相关资源
    最近更新 更多