<?php
// куркулятор
$input = '2.5+7.5/2*10=' ;
$result = 0 ; // Текущее число, которое набирает пользователь
$number = 0 ; // Результат предыдущих действий
$op = '' ; // Предыдущая операция
$resultatText = "Ответ: " ;
for ( $i = 0 ; $i < $inputLength ; $i ++ ) {
if ( $char == '*' || $char == '+' || $char == '-' || $char == '=' || $char == '/' ) {
echo "op = [$op ], result = [$result ], number = [$number ], char = [$char ]\n " ;
$resultatText .= $number . $char ;
if ( $op == '' ) {
$op = $char ;
$result = $number ;
$number = 0 ;
} else {
$result = operation( $result , $number , $op ) ;
$number = 0 ;
$op = $char ;
}
if ( $number == 0 ) {
$number = $char ;
} else {
$number .= $char ;
}
} else {
echo "Неверный символ: '$char '\n " ;
}
}
echo "$resultatText $result \n " ;
function operation ( $result , $number , $op ) {
if ( $op == '*' ) {
$result *= $number ;
} elseif ( $op == '/' ) {
$result /= $number ;
} elseif ( $op == '+' ) {
$result += $number ;
} elseif ( $op == '-' ) {
$result -= $number ;
} elseif ( $op == '=' ) {
echo "result: $result \n " ;
}
return $result ;
}
?>
PD9waHAKbWJfaW50ZXJuYWxfZW5jb2RpbmcoJ3V0Zi04Jyk7CgovLyDQutGD0YDQutGD0LvRj9GC0L7RgAokaW5wdXQgPSAnMi41KzcuNS8yKjEwPSc7CiRpbnB1dExlbmd0aCA9IG1iX3N0cmxlbiAoJGlucHV0KTsKCiRyZXN1bHQgPSAwOyAvLyDQotC10LrRg9GJ0LXQtSDRh9C40YHQu9C+LCDQutC+0YLQvtGA0L7QtSDQvdCw0LHQuNGA0LDQtdGCINC/0L7Qu9GM0LfQvtCy0LDRgtC10LvRjAokbnVtYmVyID0gMDsgLy8g0KDQtdC30YPQu9GM0YLQsNGCINC/0YDQtdC00YvQtNGD0YnQuNGFINC00LXQudGB0YLQstC40LkKJG9wID0gJyc7IC8vINCf0YDQtdC00YvQtNGD0YnQsNGPINC+0L/QtdGA0LDRhtC40Y8KJHJlc3VsdGF0VGV4dCA9ICLQntGC0LLQtdGCOiAiOwoKZm9yICgkaSA9IDA7ICRpIDwgJGlucHV0TGVuZ3RoOyAkaSsrKSB7CiAgICAkY2hhciA9IG1iX3N1YnN0cigkaW5wdXQsICRpLCAxKTsKICAgIGlmICgkY2hhciA9PScqJyB8fCAkY2hhciA9PScrJyB8fCAkY2hhciA9PSctJyB8fCAkY2hhciA9PSc9JyB8fCAkY2hhciA9PScvJykgewogICAgICAgIGVjaG8gIm9wID0gWyRvcF0sIHJlc3VsdCA9IFskcmVzdWx0XSwgbnVtYmVyID0gWyRudW1iZXJdLCBjaGFyID0gWyRjaGFyXVxuIjsKICAgICAgICAkcmVzdWx0YXRUZXh0IC49ICRudW1iZXIgLiAkY2hhcjsKCiAgICAgICAgaWYgKCRvcD09JycpIHsKICAgICAgICAgICAkb3AgPSAkY2hhcjsKICAgICAgICAgICAkcmVzdWx0ID0gJG51bWJlcjsKICAgICAgICAgICAkbnVtYmVyPTA7CiAgICAgICAgfSBlbHNlIHsKICAgICAgICAgICAgJHJlc3VsdCA9IG9wZXJhdGlvbigkcmVzdWx0LCAkbnVtYmVyLCAkb3ApOwogICAgICAgICAgICAkbnVtYmVyID0gMDsKICAgICAgICAgICAgJG9wID0gJGNoYXI7CiAgICAgICAgfSAgICAgIAoKICAgIH0gZWxzZWlmIChpc19udW1lcmljKCRjaGFyKSB8ICRjaGFyPT0nLicpIHsKICAgICAgICBpZiAoJG51bWJlcj09MCkgewogICAgICAgICAgICAkbnVtYmVyID0gJGNoYXI7CiAgICAgICAgfSBlbHNlIHsKICAgICAgICAgICAgJG51bWJlciAuPSAkY2hhcjsKICAgICAgICB9ICAgICAgICAKICAgIH0gZWxzZSB7CiAgICAgICAgZWNobyAi0J3QtdCy0LXRgNC90YvQuSDRgdC40LzQstC+0Ls6ICckY2hhcidcbiI7CiAgICB9Cn0KZWNobyAiJHJlc3VsdGF0VGV4dCRyZXN1bHRcbiI7CgpmdW5jdGlvbiBvcGVyYXRpb24gKCRyZXN1bHQsICRudW1iZXIsICRvcCkgewogICAgJG51bWJlciA9IGZsb2F0dmFsKCRudW1iZXIpOwogICAgCiAgICBpZiAoJG9wID09JyonICkgewogICAgICAgICRyZXN1bHQgKj0gJG51bWJlcjsKICAgIH0gZWxzZWlmICgkb3AgPT0nLycpIHsKICAgICAgICAkcmVzdWx0IC89ICRudW1iZXI7CiAgICB9IGVsc2VpZiAoJG9wID09JysnKSB7CiAgICAgICAgJHJlc3VsdCArPSAkbnVtYmVyOwogICAgfSBlbHNlaWYgKCRvcCA9PSctJykgewogICAgICAgICRyZXN1bHQgLT0gJG51bWJlcjsKICAgIH0gZWxzZWlmICgkb3AgPT0nPScpIHsKICAgICAgICBlY2hvICJyZXN1bHQ6ICRyZXN1bHRcbiI7CiAgICAgICAgZXhpdCgpOwogICAgfSAKICAgIHJldHVybiAkcmVzdWx0Owp9Cgo/Pg==
stdout
op = [], result = [0], number = [243], char = [+]
op = [+], result = [243], number = [6743], char = [-]
op = [-], result = [6986], number = [78], char = [*]
op = [*], result = [6908], number = [2], char = [=]
Ответ: 243+6743-78*2=13816