<?php
$input = '243+6743-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 = $number;
break;
case '+':
$result = $result + $number;
break;
case '-':
$result = $result - $number;
break;
case '*':
$result = $result * $number;
break;
case '/':
$result = $result / $number;
break;
}
$op = $char;
$number = 0;
if ($char == '=') {
echo "Ответ: $input$result";
}
$number = $number * 10 + intval($char); } else {
echo "Неверный символ '$char'\n";
}
}
PD9waHAKCmVycm9yX3JlcG9ydGluZygtMSk7CgokaW5wdXQgPSAnMjQzKzY3NDMtNzgqMj0nOwokaW5wdXRMZW5ndGggPSBtYl9zdHJsZW4oJGlucHV0KTsKCiRudW1iZXIgPSAwOwokcmVzdWx0ID0gMDsKJG9wID0gJyc7Cgpmb3IgKCRpID0gMDsgJGkgPCAkaW5wdXRMZW5ndGg7ICRpKyspIHsKCQoJJGNoYXIgPSBtYl9zdWJzdHIoJGlucHV0LCAkaSwgMSk7CglpZiAoJGNoYXIgPT0gJyonIHx8ICRjaGFyID09ICcrJyB8fCAkY2hhciA9PSAnLScgfHwgJGNoYXIgPT0gJy8nIHx8ICRjaGFyID09ICc9JykgewoJCWVjaG8gIm9wPVskb3BdLCByZXN1bHQ9IFskcmVzdWx0XSwgbnVtYmVyID0gWyRudW1iZXJdLCBjaGFyID0gWyRjaGFyXVxuIjsKCQlzd2l0Y2goJG9wKSB7CgkJCWNhc2UgJyc6CgkJCQkkcmVzdWx0ID0gJG51bWJlcjsKCQkJCWJyZWFrOwoJCQljYXNlICcrJzoKCQkJCSRyZXN1bHQgPSAkcmVzdWx0ICsgJG51bWJlcjsKCQkJCWJyZWFrOwoJCQljYXNlICctJzoKCQkJCSRyZXN1bHQgPSAkcmVzdWx0IC0gJG51bWJlcjsKCQkJCWJyZWFrOwoJCQljYXNlICcqJzoKCQkJCSRyZXN1bHQgPSAkcmVzdWx0ICogJG51bWJlcjsKCQkJCWJyZWFrOwoJCQljYXNlICcvJzoKCQkJCSRyZXN1bHQgPSAkcmVzdWx0IC8gJG51bWJlcjsKCQkJCWJyZWFrOwoJCX0KCQkkb3AgPSAkY2hhcjsKCQkkbnVtYmVyID0gMDsKCQlpZiAoJGNoYXIgPT0gJz0nKSB7CgkJCWVjaG8gItCe0YLQstC10YI6ICRpbnB1dCRyZXN1bHQiOwoJCQlleGl0KCk7CgkJfQoJfSBlbHNlaWYgKGlzX251bWVyaWMoJGNoYXIpKSB7CgkJJG51bWJlciA9ICRudW1iZXIgKiAxMCArIGludHZhbCgkY2hhcik7Cgl9IGVsc2UgewoJCWVjaG8gItCd0LXQstC10YDQvdGL0Lkg0YHQuNC80LLQvtC7ICckY2hhcidcbiI7Cgl9CQp9
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