Guest User

CalcNumbers

a guest
May 30th, 2011
164
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. $numbers = array(5, 19, 45, 6, 7, 21);
  2. $operators = array('*', '+', '-', '*', '+');
  3. $numOperators =count($operators);
  4. $calc = array(
  5.     '+' => 'bcadd',
  6.     '-' => 'bcsub',
  7.     '*' => 'bcmul'
  8. );
  9. for ($i = 0; $i< $numOperators; $i++) {
  10.     if ($operators[$i] == '*') {
  11.         $numbers[$i] = call_user_func('bcmul', $numbers[$i], $numbers[$i+1]);
  12.         $numbers[$i+1] = 0;
  13.         $operators[$i] = '+';
  14.     }
  15. }
  16. $sum = $numbers[0];
  17. for ($i = 0; $i< $numOperators; $i++) {
  18.     $sum = call_user_func($calc[$operators[$i]], $sum, $numbers[$i+1]);
  19. }
  20. echo "Result: ".$sum;
RAW Paste Data