停车费计划

时间:2017-03-02 06:40:32

标签: c if-statement switch-statement codeblocks

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

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

湾0到24之间的整数,表示车辆进入该批次的小时数。

℃。 0到60之间的整数,表示车辆进入该批次的分钟数。

d。 0到24之间的整数,表示车辆退出该地段的小时数。

即0到60之间的整数,表示车辆离开该地段的时间。

由于这是一个公共场所,人们被鼓励停留很短的时间。管理层对每种类型的车辆使用两种不同的费率。

enter image description here

午夜之后,禁止车辆停留在停车场;它会被拖走。停车费还有6%的商品及服务税。

克。写一个程序 i。显示介绍消息 II。提示用户输入相关信息。 III。使用以下格式显示帐单。

小时。您的计划将包括以下标准。 一世。验证进入时间和超时时间。 II。使用switch语句来区分不同类型的车辆。 III。使用适当的循环语句允许重复计算停车费 IV。使用表1

使用适当的测试数据运行程序五次

enter image description here

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

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

4 个答案:

答案 0 :(得分:0)

你在这里意识到:

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

所有B&amp; T类型会输入else语句吗?

在尝试编码之前,我鼓励你拿起纸和笔,然后尝试做某种伪代码。之后,尝试手动测试。如果您对此感到满意,请对其进行编码。测试一下。如果没有这个工作,那么将你的问题暴露给stackoverflow。

答案 1 :(得分:0)

首先你必须RuntimeError at /apps/37f63340-2984-40b1-a728-1cf3d0820ae6/ threads can only be started once 然后使用#include<time.h>,其中包括两个参数end_time和start_time difftime()但您必须将hourIn,minIn和hourOut,minOut转换为time并将它们放在start_time变量和end_time变量中。 获得时差后,您可以计算totalChargeFee

答案 2 :(得分:0)

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

totalHourParked = hourOut - hourIn;
if(minIn >= minOut)
   totalMinParked = minIn - minOut;
else
   totalMinParked = minOut - minIn;

totalHourParked将有时间,totalMinParked将有分钟。

答案 3 :(得分:0)

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

double difftime(time_t time1, time_t time2)

假设如果在06:30有车时,在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进入正确的分支,因为你正在使用&amp;&amp;只有在满足两个条件时才执行真正的部分。

请改用开关盒。

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': ...
}