1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 | define('FORMULA', '1+(1+1)*(1+1)/1+1-1+(1+1*1)-(1-(2-3-(4-5)))+1'); echo 'RESULT=' . p(FORMULA); function p($expr, $offset=0) { $operands = array(); while ($offset < strlen($expr)) { if (count($operands) == 3) { $operands = proccess($operands); } if (isComplex($expr[$offset])) { ++$offset;// Отрезаю первую скобку выражения $complexVar = p($expr, &$offset); $operands[] = array((int)$complexVar, $expr[$offset]); if (strlen($expr) > $offset + 1 && ! isEnd($expr[$offset])) ++$offset; } elseif (isEnd($expr[$offset])) { // должно быть два элемента $left = array_shift($operands); $center = array_shift($operands); $result = eval("return {$left[0]} {$left[1]} {$center[0]};"); ++$offset;// Переходим к следующему сиволу после закрывающей скобки break; } elseif (preg_match('/(d)+/', $expr, $matches, 0, $offset)) { if (count($matches) > 1) { $operationPos = $offset + strlen($matches[1]);// Позиция операции=текущее смещение + длина if (($operationPos >= strlen($expr)) || isEnd($expr[$operationPos])) {// Конец выражения или строки if (count($operands) == 2) { $operands[] = array($matches[1], (strlen($expr) < $operationPos) ? $expr[$operationPos] : ''); $operands = proccess($operands); $result = eval("return {$operands[0][0]} {$operands[0][1]} {$operands[1][0]};"); if ($operationPos + 1 < strlen($expr)) $offset = $operationPos + 1; break; } else { $result = eval("return {$operands[0][0]} {$operands[0][1]} {$matches[1][0]};"); if ($operationPos + 1 < strlen($expr)) $offset = $operationPos + 1; break; } } else { $operands[] = array((int)$matches[1], substr($expr, $operationPos, 1)); // Сейчас позиция операции указывает на закрывающую скобку, двигаемся дальше if (strlen($expr) > $operationPos + 1) $offset = $operationPos + 1; } } else throw new Exception('?'); } else { throw new Exception('?'); } } return $result; } /* * Из трех операндов делает два */ function proccess($operands) { if (in_array($operands[0][1], array('+', '-')) && (in_array($operands[1][1], array('*', '/')))) { // Центральный с правым $right = array_pop($operands); $center = array_pop($operands); echo "processing CR {$center[0]} {$center[1]} {$right[0]};n"; $result = eval("return {$center[0]} {$center[1]} {$right[0]};"); array_push($operands, array($result, $right[1])); } else { $left = array_shift($operands); $center = array_shift($operands); $result = eval("return {$left[0]} {$left[1]} {$center[0]};"); echo "processing LC {$left[0]} {$left[1]} {$center[0]}=$result;n"; array_unshift($operands, array((int)$result, $center[1])); } return $operands; } function isComplex($symbol) { return ($symbol == '(') ? TRUE : FALSE; } function isEnd($symbol) { return ($symbol == ')') ? TRUE : FALSE; } |
define('FORMULA', '1+(1+1)*(1+1)/1+1-1+(1+1*1)-(1-(2-3-(4-5)))+1');
echo 'RESULT=' . p(FORMULA);
function p($expr, $offset=0) {
$operands = array();
while ($offset < strlen($expr)) {
if (count($operands) == 3) {
$operands = proccess($operands);
}
if (isComplex($expr[$offset])) {
++$offset;// Отрезаю первую скобку выражения
$complexVar = p($expr, &$offset);
$operands[] = array((int)$complexVar, $expr[$offset]);
if (strlen($expr) > $offset + 1 && ! isEnd($expr[$offset]))
++$offset;
} elseif (isEnd($expr[$offset])) {
// должно быть два элемента
$left = array_shift($operands);
$center = array_shift($operands);
$result = eval("return {$left[0]} {$left[1]} {$center[0]};");
++$offset;// Переходим к следующему сиволу после закрывающей скобки
break;
} elseif (preg_match('/(d)+/', $expr, $matches, 0, $offset)) {
if (count($matches) > 1) {
$operationPos = $offset + strlen($matches[1]);// Позиция операции=текущее смещение + длина
if (($operationPos >= strlen($expr)) || isEnd($expr[$operationPos])) {// Конец выражения или строки
if (count($operands) == 2) {
$operands[] = array($matches[1], (strlen($expr) < $operationPos) ? $expr[$operationPos] : '');
$operands = proccess($operands);
$result = eval("return {$operands[0][0]} {$operands[0][1]} {$operands[1][0]};");
if ($operationPos + 1 < strlen($expr))
$offset = $operationPos + 1;
break;
} else {
$result = eval("return {$operands[0][0]} {$operands[0][1]} {$matches[1][0]};");
if ($operationPos + 1 < strlen($expr))
$offset = $operationPos + 1;
break;
}
} else {
$operands[] = array((int)$matches[1], substr($expr, $operationPos, 1));
// Сейчас позиция операции указывает на закрывающую скобку, двигаемся дальше
if (strlen($expr) > $operationPos + 1)
$offset = $operationPos + 1;
}
} else
throw new Exception('?');
} else {
throw new Exception('?');
}
}
return $result;
}
/*
* Из трех операндов делает два
*/
function proccess($operands) {
if (in_array($operands[0][1], array('+', '-')) && (in_array($operands[1][1], array('*', '/')))) {
// Центральный с правым
$right = array_pop($operands);
$center = array_pop($operands);
echo "processing CR {$center[0]} {$center[1]} {$right[0]};n";
$result = eval("return {$center[0]} {$center[1]} {$right[0]};");
array_push($operands, array($result, $right[1]));
} else {
$left = array_shift($operands);
$center = array_shift($operands);
$result = eval("return {$left[0]} {$left[1]} {$center[0]};");
echo "processing LC {$left[0]} {$left[1]} {$center[0]}=$result;n";
array_unshift($operands, array((int)$result, $center[1]));
}
return $operands;
}
function isComplex($symbol) {
return ($symbol == '(') ? TRUE : FALSE;
}
function isEnd($symbol) {
return ($symbol == ')') ? TRUE : FALSE;
}
Навеяно http://habrahabr.ru/company/yandex/blog/206234/ этим