<?php
$input = '243+6743/3-78*2=' ;
$number = 0 ; // Текущее число, которое набирает пользователь
$result = 0 ; // Результат предыдущих действий
$op = '' ; // Предыдущая операция
for ( $i = 0 ; $i < $inputLength ; $i ++ ) {
if ( $char == '*' || $char == '+' || $char == '-' || $char == '=' || $char == '/' ) {
echo "op = [{$op} ], result = [{$result} ], number = [{$number} ], char = [{$char} ]\n " ;
switch ( $op ) {
case '*' :
$result = $result * $number ;
break ;
case '+' :
$result = $result + $number ;
break ;
case '-' :
$result = $result - $number ;
break ;
case '/' :
$result = round ( $result / $number , 2 ) ; break ;
default :
$result = $number ;
break ;
}
$op = $char ;
$number = 0 ;
if ( $char == '=' ) {
echo "Ответ: {$input} {$result} \n " ;
}
$number = $number * 10 + intval ( $char ) ; } else {
echo "Неверный символ: '$char '\n " ;
}
}
PD9waHAKCmVycm9yX3JlcG9ydGluZygtMSk7Cm1iX2ludGVybmFsX2VuY29kaW5nKCJ1dGYtOCIpOwoKJGlucHV0ID0gJzI0Mys2NzQzLzMtNzgqMj0nOwokaW5wdXRMZW5ndGggPSBtYl9zdHJsZW4oJGlucHV0KTsKCiRudW1iZXIgPSAwIDsgCS8vINCi0LXQutGD0YnQtdC1INGH0LjRgdC70L4sINC60L7RgtC+0YDQvtC1INC90LDQsdC40YDQsNC10YIg0L/QvtC70YzQt9C+0LLQsNGC0LXQu9GMCiRyZXN1bHQgPSAwOwkvLyDQoNC10LfRg9C70YzRgtCw0YIg0L/RgNC10LTRi9C00YPRidC40YUg0LTQtdC50YHRgtCy0LjQuQokb3AgPSAnJzsJLy8g0J/RgNC10LTRi9C00YPRidCw0Y8g0L7Qv9C10YDQsNGG0LjRjwoKZm9yICgkaSA9IDA7ICRpIDwgJGlucHV0TGVuZ3RoOyAkaSsrKXsKICAkY2hhciA9IG1iX3N1YnN0cigkaW5wdXQgLCAkaSwgMSk7CiAgaWYoJGNoYXIgPT0gJyonIHx8ICRjaGFyID09ICcrJyB8fCAkY2hhciA9PSAnLScgfHwgJGNoYXIgPT0gJz0nIHx8ICRjaGFyID09ICcvJyl7CiAgZWNobyAib3AgPSBbeyRvcH1dLCByZXN1bHQgPSBbeyRyZXN1bHR9XSwgbnVtYmVyID0gW3skbnVtYmVyfV0sIGNoYXIgPSBbeyRjaGFyfV1cbiI7CiAgICBzd2l0Y2goJG9wKXsKICAgICAgY2FzZSAnKic6CgkkcmVzdWx0ID0gJHJlc3VsdCAqICRudW1iZXI7CglicmVhazsKICAgICAgY2FzZSAnKyc6CgkkcmVzdWx0ID0gJHJlc3VsdCArICRudW1iZXI7CglicmVhazsKICAgICAgY2FzZSAnLSc6CgkkcmVzdWx0ID0gJHJlc3VsdCAtICRudW1iZXI7CglicmVhazsKICAgICAgY2FzZSAnLyc6CgkkcmVzdWx0ID0gcm91bmQoJHJlc3VsdCAvICRudW1iZXIsMik7CglicmVhazsKICAgIAlkZWZhdWx0OgoJJHJlc3VsdCA9ICRudW1iZXI7CglicmVhazsKICAgIH0KICAgICRvcCA9ICRjaGFyOwoJJG51bWJlciA9IDA7CiAgICAgIGlmKCRjaGFyID09ICc9Jyl7CgkgIGVjaG8gItCe0YLQstC10YI6IHskaW5wdXR9eyRyZXN1bHR9XG4iOwoJICBleGl0KCk7CiAgICAgIH0KICB9ZWxzZWlmIChpc19udW1lcmljKCRjaGFyKSl7CiAgICAkbnVtYmVyID0gJG51bWJlcioxMCtpbnR2YWwoJGNoYXIpOwogIH1lbHNlewogICAgZWNobyAi0J3QtdCy0LXRgNC90YvQuSDRgdC40LzQstC+0Ls6ICckY2hhcidcbiI7CiAgfQp9
stdout
op = [], result = [0], number = [243], char = [+]
op = [+], result = [243], number = [6743], char = [/]
op = [/], result = [6986], number = [3], char = [-]
op = [-], result = [2328.67], number = [78], char = [*]
op = [*], result = [2250.67], number = [2], char = [=]
Ответ: 243+6743/3-78*2=4501.34