基本上我正在尝试一个关于如何计算费用的问题。但是,我的代码非常长。我最终为我的代码使用了大量的“if”和“else”。我想知道是否有更好的方法来解决这个问题。
我最初尝试使用循环来继续累积费用,但当我的时间跨过关键时刻时,我遇到了一个问题。我在 TimeIn 中添加了时间以及我的费用,但是当它超过早上 7 点时,我需要将其重置回早上 7 点,以便正确收取我的费用。那是我放弃的时候,我的代码也变得很长。
例如。循环从0630添加到0730,费用适当增加,0700的前30分钟将被跳过并错误收取费用。
我尝试的问题:
多年来,吴先生每天都坐出租车上班。然而,出租车的票价近年来一直在快速上涨。因此,他正在考虑开车上班。开车的费用之一是停车费。吴先生工作单位停车场的停车费如下表所示。
Weekday Saturday Sunday
4am ~ 7am $2.00 per hour $2.50 per hour $5
7am ~ 6pm $1.20 per 30 minute $1.50 per 30 minutes per
6pm ~ midnight $5.00 per entry $7.00 per entry entry
Special note:
1. The car park opens at 4am and closes at midnight. All vehicles must leave
by midnight.
2. There is a grace period of 10 minutes on any day (i.e., it is completely
free to park for 10 minutes or less regardless of day and time.)
3. There is a 10% surcharge for parking more than 10 hours on a weekday and
20% for Saturday. There is no surcharge for Sunday.
4. There is an additional $3.00 fee for exiting after 10pm on any day.
(Surcharge is not applicable on this fee.)
你的程序应该读入一个整数,它是一个介于 1 和 7 之间的整数,代表星期几(1 是星期一,7 是星期日)。它还应该读入两个数字,代表 24 小时格式的超时和超时。然后它应该计算并显示停车费(保留两位小数)。您可以假设输入是有效的(即,日期在指定范围内,超时和超时都在 24 小时格式的凌晨 4 点和午夜之间,超时不早于超时)。
我的极长代码:
#include <stdio.h>
#include <math.h>
double computeFee(int, int, int);
int main(void){
int day, timeIn, timeOut;
scanf("%d %d %d", &day, &timeIn, &timeOut);
printf("Enter day: %d\n", day);
printf("Enter time-in: %d\n", timeIn);
printf("Enter time-out: %d\n", timeOut);
printf("Parking fee is $%.2lf\n", computeFee(day, timeIn, timeOut));
return 0;
}
double computeFee(int day, int timeIn, int timeOut){
double fee = 0;
double TimeIn, TimeOut;
TimeIn = 60*(floor(timeIn/100)) + (timeIn%100);
TimeOut = (60*(floor(timeOut/100)) + (timeOut%100));
if((day>=1)&&(day<=5)){
if(TimeIn<420){
if(TimeOut<420){
fee += 2*ceil((TimeOut - TimeIn)/60);
}
else{
fee += 6;
if(TimeOut>=1080){
fee += 31.4;
}
else{
fee += 1.2*ceil((TimeOut - 420)/30);
}
}
}
if(TimeIn>=420){
if(TimeIn>=1080){
fee = 5;
}
else{
if(TimeOut>=1080){
fee += (1.2*ceil((1080 - TimeIn)/30) + 5);
}
else{
fee += (1.2*ceil((TimeOut - TimeIn)/30));
}
}
}
if((TimeOut-TimeIn)>=600){
fee *= 1.1;
}
}
if(day == 6){
if(TimeIn<420){
if(TimeOut<420){
fee += (2.5*ceil((TimeOut - TimeIn)/60));
}
else{
fee += 7.5;
if(TimeOut>=1080){
fee += 40;
}
else{
fee += (1.5*ceil((TimeOut - 420)/30));
}
}
}
if(TimeIn>=420){
if(TimeIn>=1080){
fee = 7;
}
else{
if(TimeOut>=1080){
fee += (1.5*ceil((1080 - TimeIn)/30) + 7);
}
else{
fee += (1.5*ceil((TimeOut - TimeIn)/30));
}
}
}
if((TimeOut-TimeIn)>=600){
fee *= 1.2;
}
}
if(day == 7){
fee = 5;
}
if((timeOut/100)>=22){
fee += 3;
}
if((timeOut - timeIn)<=10){
fee = 0;
}
return fee;
}
有关如何计算费用的示例:
Example 1: Tuesday, 4:29am to 7:50am.
• 4:29am to 7am is charged as 3 1-hour slots: $2.00 * 3 = $6.00
• 7am to 7:50am is charged as 2 30-minute slots: $1.20 * 2 = $2.40
• Total fee = $6.00 + $2.40 = $8.40
Example 2: Saturday, 7:01am to 7:49pm.
• 7:01am to 6pm is charged as 22 30-minute slots: $1.50 * 22 = $33.00
• 6pm to 7:49pm is charged as one entry: $7.00
• 20% Surcharge for parking more than 10 hours: ($33.00 + $7.00) * 20% =
$8.00
• Total fee = $33.00 + $7.00 + $8.00 = $48.00
Example 3: Sunday, 3pm to 10:01pm.
• 3pm to 10:01pm is charged as one entry: $5.00
• Additional fee for exiting after 10pm: $3.00
• Total fee = $5.00 + $3.00 = $8.00
Example 4: Thursday, 11:49pm to 11:59pm.
• Grace period
• Total fee = $0.00
Example 5: Monday, 12pm to 10:01pm.
• 12pm to 6pm is charged as 12 30-minute slots: $1.20 * 12 = $14.40
• 6pm to 10:01pm is charged as one entry: $5.00
• 10% Surcharge for parking more than 10 hours: ($14.40 + $5.00) * 10% =
$1.94
• Additional fee for exiting after 10pm: $3.00
• Total fee = $14.40 + $5.00 + $1.94 + $3.00 = $24.34
感谢您阅读我的长问题。并提前感谢您的帮助。
注意:我还没有学习数组和除此之外的任何东西。到目前为止只学习循环和选择语句(阅读 K&R 编程教程,直到第 17 章,使用 Online GeekforGeek 作为编译器)。但是,我仍然会非常感谢使用其他方法的解决方案。