<?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;
}
elseif ($op == '-') {
$result = $result - $number;
$number = 0;
$op = $char;
}
elseif ($op == '*') {
$result = $result * $number;
$number = 0;
$op = $char;
}
else {
$op = $char;
$result = $number;
$number = 0;
}
if ($char == '=') {
echo "Результат: {$result}\n";
}
}
$number = $number * 10 + $char;
}
else {
echo "Неверный символ: '$char'\n";
}
}
PD9waHAKCi8qINCvINGDINC80LDQvNGLINC60LDQu9GM0LrRg9C70Y/RgtC+0YAgKi8KJGlucHV0ID0gJzIrMTAtMSs1KjU9JzsKJGlucHV0TGVuZ3RoID0gbWJfc3RybGVuKCRpbnB1dCk7CgokbnVtYmVyID0gMDsgLyog0KLQtdC60YPRidC10LUg0YfQuNGB0LvQviwg0LrQvtGC0L7RgNC+0LUg0L3QsNCx0LjRgNCw0LXRgiDQv9C+0LvRjNC30L7QstCw0YLQtdC70YwgKi8KJHJlc3VsdCA9IDA7IC8qINCg0LXQt9GD0LvRjNGC0LDRgiDQv9GA0LXQtNGL0LTRg9GJ0LjRhSDQtNC10LnRgdGC0LLQuNC5ICovCiRvcCA9ICcnOyAvKiDQn9GA0LXQtNGL0LTRg9GJ0LDRjyDQvtC/0LXRgNCw0YbQuNGPICgrLC0sKikgKi8KCmZvciAoJGkgPSAwOyAkaSA8ICRpbnB1dExlbmd0aDsgJGkrKykgewogICAgJGNoYXIgPSBtYl9zdWJzdHIoJGlucHV0LCAkaSwgMSk7CiAgICBpZiAoJGNoYXIgPT0gJyonIHx8ICRjaGFyID09ICcrJyB8fCAkY2hhciA9PSAnLScgfHwgJGNoYXIgPT0gJz0nKSB7CgkgICAgaW50dmFsKCRudW1iZXIpOwoJICAgIGVjaG8gIm9wPVt7JG9wfV0sIHJlc3VsdCA9IFt7JHJlc3VsdH1dLCBudW1iZXIgPSBbeyRudW1iZXJ9XSwgY2hhciA9IFt7JGNoYXJ9XVxuIjsKCSAgICBpZiAoJG9wID09ICcrJykgewoJICAgICAgICAkcmVzdWx0ID0gJHJlc3VsdCArICRudW1iZXI7CgkJCSRudW1iZXIgPSAwOwoJCQkkb3AgPSAkY2hhcjsKCSAgICB9CgkgICAgZWxzZWlmICgkb3AgPT0gJy0nKSB7CgkgICAgICAgICRyZXN1bHQgPSAkcmVzdWx0IC0gJG51bWJlcjsKCQkJJG51bWJlciA9IDA7CgkJCSRvcCA9ICRjaGFyOwoJICAgIH0KCSAgICBlbHNlaWYgKCRvcCA9PSAnKicpIHsKCSAgICAgICAgJHJlc3VsdCA9ICRyZXN1bHQgKiAkbnVtYmVyOwoJICAgICAgICAkbnVtYmVyID0gMDsKCSAgICAgICAgJG9wID0gJGNoYXI7CgkgICAgfQoJICAgIGVsc2UgewoJICAgIAkkb3AgPSAkY2hhcjsKCQkgICAgJHJlc3VsdCA9ICRudW1iZXI7CgkJICAgICRudW1iZXIgPSAwOwoJICAgIH0KCSAgICBpZiAoJGNoYXIgPT0gJz0nKSB7CgkgICAgICAgIGVjaG8gItCg0LXQt9GD0LvRjNGC0LDRgjogeyRyZXN1bHR9XG4iOwoJICAgICAgICBleGl0KCk7CgkJfQoJfSAKCWVsc2VpZiAoaXNfbnVtZXJpYygkY2hhcikpCXsKCSAgICAkbnVtYmVyID0gJG51bWJlciAqIDEwICsgJGNoYXI7Cgl9IAoJZWxzZSAgICB7CgkgICAgZWNobyAi0J3QtdCy0LXRgNC90YvQuSDRgdC40LzQstC+0Ls6ICckY2hhcidcbiI7Cgl9Cn0=
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