Advertisement
Guest User

Untitled

a guest
May 31st, 2011
220
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 2.52 KB | None | 0 0
  1. <?php
  2. $numbers = array(5, 19, 45, 6, 7, 21);
  3. $operators = array('*', '+', '-', '*', '+');
  4.  
  5. // String bauen
  6. $mathString = '';
  7. foreach ($numbers as $id => $number) {
  8.     if ($id < count($numbers)-1) {
  9.         $mathString .= $number . $operators[$id];
  10.     } else {
  11.         $mathString .= $number;
  12.     }  
  13. }  
  14. // $mathString ist nun '5*19+45-6*7+21'
  15. ##################################
  16. function flatten($ms, $ops) {
  17.     $list = array();
  18.     $len = strlen($ms);
  19.     $buf = '';
  20.    
  21.     for ($i=0;$i<$len;$i++) {
  22.         $chr = $ms[$i];
  23.         if (!array_key_exists($chr, $ops)) {
  24.             $buf .= $chr;
  25.         } else {
  26.             $list[] = $buf;
  27.             $list[] = $chr;
  28.             $buf = '';
  29.         }  
  30.     }  
  31.     $list[] = $buf;
  32.     return $list;
  33. }  
  34.  
  35. function apply($list, $ops) {
  36.     $o = $list[1];
  37.     $func = $ops[$o];
  38.     $h = $func($list[0], $list[2]);
  39.     return $h;
  40. }
  41.  
  42. function foldl($list, $ops, $prio) {
  43.     if (!isset($list[2])) {
  44.         return $list[0];
  45.     } else {
  46.         $o = $list[1];
  47.         if (in_array($o, $prio)) {
  48.             $h = apply($list, $ops);
  49.             $t = array_slice($list, 3);
  50.             array_unshift($t, $h);
  51.             return foldl($t, $ops, $prio);
  52.         } else {
  53.             if (isset($list[3])) {
  54.                 $look = $list[3];
  55.                 if (in_array($look, $prio)) {
  56.                     $tmp = array_slice($list, 2, 3);
  57.                     $m = apply($tmp, $ops);
  58.                     if (isset($list[5])) {
  59.                         $t = array_slice($list, 5);
  60.                     } else {
  61.                         $t = array();
  62.                     }
  63.                     $li = array_unshift($t, $list[0], $list[1], $m);
  64.                     return foldl($t, $ops, $prio);
  65.                 } else {
  66.                     $h = apply($list, $ops);
  67.                     $t = array_slice($list, 3);
  68.                     array_unshift($t, $h);
  69.                     return foldl($t, $ops, $prio);
  70.                 }
  71.             } else {
  72.                 $h = apply($list, $ops);
  73.                 return foldl(array($h), $ops, $prio);
  74.             }
  75.         }
  76.     }
  77. }
  78.  
  79. // prio der abarbeitung
  80. $prio = array('*');
  81. // closures, whee
  82. $ops = array(
  83.     '*' => function($a,$b) { return $a * $b; },
  84.     '+' => function($a,$b) { return $a + $b; },
  85.     '-' => function($a,$b) { return $a - $b; },
  86. );
  87. $list = flatten($mathString, $ops);
  88. echo $mathString.PHP_EOL;
  89. var_dump($list);
  90. $r = foldl($list, $ops, $prio, array());
  91. echo "----\nresult: $r".PHP_EOL;
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement