关于接近票价计算的见解

时间:2018-06-10 11:22:39

标签: c loops fee

基本上我正在尝试一个关于如何计算费用的问题。但是,我的代码非常长。我最终使用了大量的'if'和'else'代码。我想知道是否有更好的方法来处理这个问题。

我最初尝试使用循环来保持费用,但是当我的时间跨越关键时刻时,我遇到了问题。我加上TimeIn的时间以及我的费用,但是当它超过7am标记时,我需要将其重置为早上7点,以便我的费用能够正常收费。那时候,当我的代码变得很长时,我放弃了。

EG。循环增加从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小时格式的进入和超时。它 然后应计算并显示停车费(小数点后两位)。 您可以假设输入有效(即,日期在指定范围内, 进入和超时都是在凌晨4点到午夜之间的24小时格式和超时 不早于时间。)

我的代码非常长:

#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&amp; R编程教程,直到第17章,使用Online GeekforGeek作为编译器)。但是,我仍然会非常欣赏使用其他方法的解决方案。

0 个答案:

没有答案