<?php
$input = '126+135/8-19-100*2=';
$number = 0; // Текущая цифра
$result = 0; // Результат предидущего действия
$op = ''; // предидущая операция
for($i = 0; $i < $inputLength; $i++){
if($char == '+' || $char == '-' || $char == '=' || $char == '*' || $char == '/'){
switch($op){
case '' : // При встрече первого символа заношу цифру в результат
$result = $result + $number;
$number = 0; // Обнуляю после каждой операции
break;
case '+' :
$result = $result + $number;
$number = 0;
break;
case '-':
$result = $result - $number;
$number = 0;
break;
case '*':
$result = $result * $number;
$number = 0;
break;
case '/':
$result = $result / $number;
$number = 0;
break;
}
$op = $char; // Заношу операцию после выполнения предидущей
//Когда встречаю "=" вывожу результат
if($op == '='){
echo "Ответ: $result";
}
$number = $number * 10 + floatval($char); //Привожу $char к int и клею к цифре }else{
echo "Некорректный символ $char";
}
}
PD9waHAKCiRpbnB1dCA9ICcxMjYrMTM1LzgtMTktMTAwKjI9JzsKJGlucHV0TGVuZ3RoID0gbWJfc3RybGVuKCRpbnB1dCk7CgokbnVtYmVyID0gMDsgLy8g0KLQtdC60YPRidCw0Y8g0YbQuNGE0YDQsAokcmVzdWx0ID0gMDsgLy8g0KDQtdC30YPQu9GM0YLQsNGCINC/0YDQtdC00LjQtNGD0YnQtdCz0L4g0LTQtdC50YHRgtCy0LjRjwokb3AgPSAnJzsgLy8g0L/RgNC10LTQuNC00YPRidCw0Y8g0L7Qv9C10YDQsNGG0LjRjwoKZm9yKCRpID0gMDsgJGkgPCAkaW5wdXRMZW5ndGg7ICRpKyspewoJJGNoYXIgPSBtYl9zdWJzdHIoJGlucHV0LCAkaSwgMSk7CglpZigkY2hhciA9PSAnKycgfHwgJGNoYXIgPT0gJy0nIHx8ICRjaGFyID09ICc9JyB8fCAkY2hhciA9PSAnKicgfHwgJGNoYXIgPT0gJy8nKXsKCQlzd2l0Y2goJG9wKXsKCQkJY2FzZSAnJyA6IC8vINCf0YDQuCDQstGB0YLRgNC10YfQtSDQv9C10YDQstC+0LPQviDRgdC40LzQstC+0LvQsCDQt9Cw0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