【发布时间】: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