<?php
/* Я у мамы калькулятор */
$input = '2+10-1+5*5=';
$number = 0; /* Текущее число, которое набирает пользователь */
$result = 0; /* Результат предыдущих действий */
$op = ''; /* Предыдущая операция (+,-,*) */
for ($i = 0; $i < $inputLength; $i++) {
if ($char == '*' || $char == '+' || $char == '-' || $char == '=') {
echo "op=[{$op}], result = [{$result}], number = [{$number}], char = [{$char}]\n";
if ($op == '+') {
$result = $result + $number;
$number = 0;
$op = $char;
//return $result;
}
elseif ($op == '-') {
$result = $result - $number;
$number = 0;
$op = $char;
//return $result;
}
elseif ($op == '*') {
$result = $result * $number;
$number = 0;
$op = $char;
}
else {
$op = $char;
$result = $number;
$number = 0;
}
if ($char == '=') {
echo "Результат: {$result}\n";
}
// $result = $number;
}
$number = $number * 10 + $char;
}
else {
echo "Неверный символ: '$char'\n";
}
}
PD9waHAKCi8qINCvINGDINC80LDQvNGLINC60LDQu9GM0LrRg9C70Y/RgtC+0YAgKi8KJGlucHV0ID0gJzIrMTAtMSs1KjU9JzsKJGlucHV0TGVuZ3RoID0gbWJfc3RybGVuKCRpbnB1dCk7CgokbnVtYmVyID0gMDsgLyog0KLQtdC60YPRidC10LUg0YfQuNGB0LvQviwg0LrQvtGC0L7RgNC+0LUg0L3QsNCx0LjRgNCw0LXRgiDQv9C+0LvRjNC30L7QstCw0YLQtdC70YwgKi8KJHJlc3VsdCA9IDA7IC8qINCg0LXQt9GD0LvRjNGC0LDRgiDQv9GA0LXQtNGL0LTRg9GJ0LjRhSDQtNC10LnRgdGC0LLQuNC5ICovCiRvcCA9ICcnOyAvKiDQn9GA0LXQtNGL0LTRg9GJ0LDRjyDQvtC/0LXRgNCw0YbQuNGPICgrLC0sKikgKi8KCmZvciAoJGkgPSAwOyAkaSA8ICRpbnB1dExlbmd0aDsgJGkrKykgewogICAgJGNoYXIgPSBtYl9zdWJzdHIoJGlucHV0LCAkaSwgMSk7CiAgICBpZiAoJGNoYXIgPT0gJyonIHx8ICRjaGFyID09ICcrJyB8fCAkY2hh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tYmVyICogMTAgKyAkY2hhcjsKCX0gCgllbHNlICAgIHsKCSAgICBlY2hvICLQndC10LLQtdGA0L3Ri9C5INGB0LjQvNCy0L7QuzogJyRjaGFyJ1xuIjsKCX0KfQ==
op=[], result = [0], number = [2], char = [+]
op=[+], result = [2], number = [10], char = [-]
op=[-], result = [12], number = [1], char = [+]
op=[+], result = [11], number = [5], char = [*]
op=[*], result = [16], number = [5], char = [=]
Результат: 80