C 停车收费计划
编写一个程序,在提供以下信息的情况下,为在停车场停车的客户计算停车费: a。表示车辆类型的字符:C表示汽车,B表示公共汽车,T表示卡车 b。0到24之间的整数,表示车辆进入停车场的时间 c。一个介于0和60之间的整数,表示车辆进入停车场的分钟数 d。0到24之间的整数,表示车辆离开停车场的时间 e。0到60之间的整数,表示车辆离开停车场的分钟数 由于这是一个公共停车场,人们被鼓励只在短时间内停车。管理层对每种类型的车辆使用两种不同的费率 午夜过后,任何车辆不得在停车场停留;它将被拖走。停车费也有6%的商品及服务税 g。编写一个程序 i、 显示介绍信息 二,。提示用户输入相关信息。 iii.使用以下格式显示账单 h。您的计划将包括以下标准。 我验证输入和输出时间。 二,。使用switch语句区分不同类型的车辆。 iii.使用适当的循环语句,允许重复计算停车费 iv.使用表1中的适当测试数据运行程序五次C 停车收费计划,c,if-statement,switch-statement,codeblocks,C,If Statement,Switch Statement,Codeblocks,编写一个程序,在提供以下信息的情况下,为在停车场停车的客户计算停车费: a。表示车辆类型的字符:C表示汽车,B表示公共汽车,T表示卡车 b。0到24之间的整数,表示车辆进入停车场的时间 c。一个介于0和60之间的整数,表示车辆进入停车场的分钟数 d。0到24之间的整数,表示车辆离开停车场的时间 e。0到60之间的整数,表示车辆离开停车场的分钟数 由于这是一个公共停车场,人们被鼓励只在短时间内停车。管理层对每种类型的车辆使用两种不同的费率 午夜过后,任何车辆不得在停车场停留;它将被拖走。停车费也
#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;
}
#包括
#包括
内部主(空)
{
char type;//车辆类型的变量
int hourIn、minuteIn、hourOut、minuteOut、entry、exit、totalParkingTime;//时间变量
浮动总四舍五入,总收费,GST;//票价变量
printf(“欢迎来到帮助停车场!\n”);//介绍信息
printf(“输入车辆类型:%c”,type);//车辆类型:c表示汽车,T表示卡车,B表示公共汽车
scanf(“%c”,类型(&T);
开关(类型)
{
案例“C”:
如果(总停车时间您是否意识到:
if(type == 'C' && totalHourParked <= 3)
{
totalChargeFee = 0.8 * totalHourParked;
}
else
{
totalChargeFee = 1.5 * totalHourParked;
}
if(type=='C'&&totalHourParked首先你必须#包括
然后使用difftime()
,其中包括两个参数end\u time和start\u time
difftime(end\u time,start\u time);
但您必须将hourIn、minIn和hourOut、minOut转换为时间,并将它们放入start\u time变量和end\u time变量中。
在获得时差后,您可以计算总费用您是在问这个问题吗:
由于车辆不会在午夜后停留,您可以直接计算,因为hourOut将始终高于hourIn
totalHourParked = hourOut - hourIn;
if(minIn >= minOut)
totalMinParked = minIn - minOut;
else
totalMinParked = minOut - minIn;
totalHourParked
将有小时,totalMinParked
将有分钟。C库函数double difftime(time\t time1,time\t time2)返回time1和time2之间的秒差,即(time1-time2)
假设一辆车在早上6点30分进站一小时,在晚上21点30分出站一小时,那么总小时数是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...
if(type='C'&&totalHourParked 3))
{
totalChargeFee=0.8*3+1.5*(totalHourParked-3);//前3小时收费0.8,其余收费1.5
}
否则如果。。。
...
...
否则如果。。。
在这种情况下,您必须使用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': ...
}
开关(类型){
案例C:如果(TotalHourPark一小时有60分钟……这不是一个真正的编程问题。你需要先在纸上计算出来。你需要从分钟中减去分钟。如果结束分钟太少,你可以将结束小时中的一小时变成60分钟……最简单的方法是从中间开始将分钟+小时变成小时和分数night@A.nonymous你甚至没有尝试计算任何totalhours
。你在现实中会做什么?从12:45到14:30有多少个小时?你将如何计算?你的C代码中加入完全相同的计算和考虑有什么问题?我以前从另一个用户那里看到过这个问题。它似乎是mework。我们确实要求学生在帮助他们之前要付出很大的努力。评论建议他应该自己多想想。如果他已经有了小时和分钟,他为什么还要费心处理“time.h”中的函数呢?立即计算比转换任何时间值都要容易。我假设你不需要计算机就能回答。那么你将如何在纸上进行计算?哇。你说得对。这也是一个很好的信息,我不知道你也可以在switch语句中使用if。谢谢你,我稍后会将此实现到我的代码中,并告诉我f它可以工作。thanksHow我在没有使用函数的情况下做时间输入和输出吗?有几种方法可以做到。这不是编程的事情。它的逻辑。用钢笔和纸来解决问题,然后对其进行编码。我很理解这个问题。我的问题在于编码。我会再次失败我的作业。我尝试了带有开关和if语句的车辆类型但是我不知道怎么做时间部分。分钟是错误的。如果时间是14:50-15:30,你会得到1小时20分钟。应该是1小时20分钟==40分钟
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': ...
}