The "payload ()" function does not return the correct result

-5

I have the following statement:

  

A parking charge a minimum fee of $ 2.00 to park by   three hours. An additional $ 0.50 per hour does not necessarily   whole is charged after the first three hours. The maximum value for   any given period of 24 hours is $ 10.00. Suppose no car   stationed for more than 24 hours at a time. Write an application   calculate and display the parking fees for each customer who   I parked in this garage yesterday. You must enter the hours of   parking for each client. The program should display the charge   for the current customer and calculate and display the total received at the end of the   day. The program must use a load-valued function to determine the   charge for each customer.

This is what I tried to do:

   <?php 
    if ($_POST) {
        $t = $_POST['num-hor'];
        $total = valorApagar($t);
        echo"<br><br>Total: R$".number_format($total, 2, ',', '.');

    }
    function valorApagar($t){
        $total = 0;

        if ($t <= 3) {
            $total = 2;
        }else if ($t > 3 && $t < 24) {
            $total = 2; 
            $t -= 3;
            if(is_int($t) == true){
                $total += $t;
            }else{
                $t = ceil($t);
                $t *= 0.5;
                $total += $t;
            }
        }else {
            $total = 10;
        }

        return $total;
    }
 ?>

The function valorApagar() is not returning the correct result, how do I adjust it to return the result as it is requested in the statement?

    
asked by anonymous 20.08.2016 / 04:26

2 answers

4
  

The problem is much more mathematical than programming. In addition the question is not clear about the problem, making it almost a service outsourcing. However, since it is easy and simple to solve, I will respond, even though it may not be .

You should only use:

$total = 2 + ((ceil($t) - 3)  * 0.5);

However there are other errors in PHP. Your question says " per hour is not necessarily whole ", so does anyone parking for 23 hours and 10 minutes will pay for 24 hours ?! In my understanding is that.

So, fixing everything:

if(isset($_POST['num-hor'])){
    echo 'Total: R$'.(number_format( valorApagar( $_POST['num-hor'] ), 2, ',', '.'));
}

function valorApagar($t){
    $t = ceil($t);

    if($t <= 3){
        $total = 2;
    }else if ($t > 3 && $t < 24) {  
        $total = 2 + (($t - 3)  * 0.5); 
    }else {
        $total = 10;
    }

    return $total;
}

Try this here.

Result:

0 horas custam R$2,00
1 horas custam R$2,00
2 horas custam R$2,00
3 horas custam R$2,00
4 horas custam R$2,50
5 horas custam R$3,00
6 horas custam R$3,50
7 horas custam R$4,00
8 horas custam R$4,50
9 horas custam R$5,00
10 horas custam R$5,50
11 horas custam R$6,00
12 horas custam R$6,50
13 horas custam R$7,00
14 horas custam R$7,50
15 horas custam R$8,00
16 horas custam R$8,50
17 horas custam R$9,00
18 horas custam R$9,50
19 horas custam R$10,00
20 horas custam R$10,50
21 horas custam R$11,00
22 horas custam R$11,50
23 horas custam R$12,00
24 horas custam R$10,00
25 horas custam R$10,00

Old result (your code):

0 horas custam R$2,00
1 horas custam R$2,00
2 horas custam R$2,00
3 horas custam R$2,00
4 horas custam R$3,00
5 horas custam R$4,00
6 horas custam R$5,00
7 horas custam R$6,00
8 horas custam R$7,00
9 horas custam R$8,00
10 horas custam R$9,00
11 horas custam R$10,00
12 horas custam R$11,00
13 horas custam R$12,00
14 horas custam R$13,00
15 horas custam R$14,00
16 horas custam R$15,00
17 horas custam R$16,00
18 horas custam R$17,00
19 horas custam R$18,00
20 horas custam R$19,00
21 horas custam R$20,00
22 horas custam R$21,00
23 horas custam R$22,00
24 horas custam R$10,00
25 horas custam R$10,00

Results obtained using:

for($i = 0; $i < 26; $i++){
    echo $i.' horas custam R$'.(number_format( valorApagar( $i ), 2, ',', '.'))."\n";
}
    
20.08.2016 / 05:38
3

Follow a "short" version just to complement the post.

I did based on @inkeliz's answer (which, by the way, is much more didactic for those who are learning, and already took my +1):

function valorAPagar($t) {
    return min( 10, max( 2, ceil( $t ) / 2 + .5 ) );
};
  • We only use the formula ceil( $t ) / 2 + .5 , since the result is linear. (It's just a simplification of 2 + ( ($t - 3) * 0.5 ) )

  • min( 10, valor ) makes it less than 10

  • Similarly, max( 2, valor ) causes the value to be at least 2

See a test loop in Online PHP Functions .

    
21.08.2016 / 00:11