2

Please check my code:

<?php
  $operarors = array( '+', '-', '*' );
  $randOperator = array($operarors[rand(0,2)], $operarors[rand(0,2)]);
  $num1 = rand(0,10);
  $num2 = rand(0,10);
  $num3 = rand(0,10);

  $result = $num1.$randOperator[0].$num2.$randOperator[1].$num3;
  echo "The math: $num1 $randOperator[0] $num2 $randOperator[1] $num3 = $result";
?>

In the code above, I am not getting my total number of result.

Suppose I am getting 3+4*5, the output should be 23, but it is showing the string 3+4*5.

Help me please.

Pang
  • 9,564
  • 146
  • 81
  • 122
Developer
  • 2,676
  • 8
  • 43
  • 65

3 Answers3

5

You can't just concatenate the operators like that. I suggest doing something like this:

<?php
function operate($op1, $operator, $op2) {
    switch ($operator) {
        case "+":
            return $op1 + $op2;
        case "-":
            return $op1 - $op2;
        case "*":
            return $op1 * $op2;
    }
}

$operators = array( '+', '-', '*' );

// performs calculations with correct order of operations
function calculate($str) {
    global $operators;

    // we go through each one in order of precedence
    foreach ($operators as $operator) {
        $operands = explode($operator, $str, 2);
        // if there's only one element in the array, then there wasn't that operator in the string
        if (count($operands) > 1) {
            return operate(calculate($operands[0]), $operator, calculate($operands[1]));
        }
    }

    // there weren't any operators in the string, assume it's a number and return it so it can be operated on
    return $str;
}

$randOperator = array($operators[rand(0,2)], $operators[rand(0,2)]);
$num1 = rand(0,10);
$num2 = rand(0,10);
$num3 = rand(0,10);

$str = "$num1 $randOperator[0] $num2 $randOperator[1] $num3";

echo "$str = ", calculate($str), PHP_EOL;
Andrea
  • 19,134
  • 4
  • 43
  • 65
  • 1
    but with 2+1*3 result will 2+3 = 3 * 3 => 9 ? but 5 is expected – MouradK Nov 21 '14 at 14:02
  • @MouradK you re right. i am getting error output The math: 3 - 5 * 5 = -22 but output is = -10. This is not a right answer. – Developer Nov 21 '14 at 14:49
  • @AndreaFalds your code is not working this case `3 - 6 * 5 = -15` (Wrong output). Expected Answer `-27` – Chinmay235 Nov 21 '14 at 14:57
  • 1
    @MouradK I've updated the code to handle order of operations/operator precedence. The code wasn't wrong before, I just didn't know you wanted it to handle that. – Andrea Nov 21 '14 at 15:54
  • @Chinu The code does work, it just wasn't designed to handle order of operations. I've changed it to do so. – Andrea Nov 21 '14 at 16:13
  • @MouradK your code is working but sometimes i am getting wrong output please check `3 - 2 - 9 = 10`, `1 - 1 - 1= 1`, `0 - 4 - 7 = 3`, `10 - 3 - 1 = 8`, `4 - 4 - 8 = 8` etc. – Chinmay235 Nov 21 '14 at 18:53
1

As @AndreaFaulds said, or use callbacks: (though using array_reduce and all this array pointer magic is not necessary).

<?php

$ops = [
    '+' => function ($op1, $op2) { return $op1 + $op2; },
    '*' => function ($op1, $op2) { return $op1 * $op2; },
    '-' => function ($op1, $op2) { return $op1 - $op2; }
];

$nums = [rand(0, 10), rand(0, 10), rand(0, 10)];
$operators = [array_rand($ops), array_rand($ops)];

$initial = array_shift($nums);
$result = array_reduce($nums, function ($accumulate, $num) use (&$operators, $ops) {
    return $ops[each($operators)[1]]($accumulate, $num);
}, $initial);

Note, [] short array syntax has a version requirement of PHP 5.4+.

Jimbo
  • 25,790
  • 15
  • 86
  • 131
nikita2206
  • 1,129
  • 2
  • 10
  • 17
-3

You should use the + operator instead of the . operator. The . just pastes it together as if the values were strings.

JKaan
  • 359
  • 2
  • 14