Advertisement
Guest User

calc

a guest
May 30th, 2011
206
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 0.55 KB | None | 0 0
  1. <?php
  2. $string = $argv[1];
  3. $string = preg_replace('/(([-+\/*])|^)-/', '\\1|', $string);
  4. $svals = array();
  5. foreach(explode('+', $string) as $ppart){
  6.     $mvals = array();
  7.     $i = 0;
  8.     foreach(explode('-', $ppart) as $spart){
  9.         $prod = 1;
  10.         foreach(explode('*', $spart) as $fac){
  11.             $prod *= (int)str_replace('|', '-', $fac);
  12.         }
  13.         if (0 != $i++){
  14.             $mvals[] = -1 * $prod;
  15.         } else {
  16.             $mvals[] = (int)$prod;
  17.         }
  18.     }
  19.     $svals[] = array_sum($mvals);
  20. }
  21. echo array_sum($svals);
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement