Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- <?php
- $string = $argv[1];
- $string = preg_replace('/(([-+\/*])|^)-/', '\\1|', $string);
- $svals = array();
- foreach(explode('+', $string) as $ppart){
- $mvals = array();
- $i = 0;
- foreach(explode('-', $ppart) as $spart){
- $prod = 1;
- foreach(explode('*', $spart) as $fac){
- $prod *= (int)str_replace('|', '-', $fac);
- }
- if (0 != $i++){
- $mvals[] = -1 * $prod;
- } else {
- $mvals[] = (int)$prod;
- }
- }
- $svals[] = array_sum($mvals);
- }
- echo array_sum($svals);
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement