<?php
$input = "55/5*1=";
$number = 0; //actual number
$result = 0; //of previous actions
$op = ""; //last unused operation
for ($i = 0; $i < $inputLength; $i++) {
if ($char == "*" || $char == "+" || $char == "-" || $char == "/") {
switch($op) {
case(""):
$op = $char;
$result = $number;
$number = 0;
break;
case ("*"):
$result = $result * $number;
$op = $char;
$number = 0;
break;
case ("+"):
$result = $result + $number;
$op = $char;
$number = 0;
break;
case ("-"):
$result = $result - $number;
$op = $char;
$number = 0;
break;
case ("/"):
$result = $result / $number;
$op = $char;
$number = 0;
break;
}
$number = $number * 10 + intval($char);
} elseif ($char == "=") {
switch($op) {
case ("*"):
$result = $result * $number;
$op = $char;
$number = 0;
break;
case ("+"):
$result = $result + $number;
$op = $char;
$number = 0;
break;
case ("-"):
$result = $result - $number;
$op = $char;
$number = 0;
break;
case ("/"):
$result = $result / $number;
$op = $char;
$number = 0;
break;
}
echo $result;
} else {
echo "Error. Error. Error. **{$char}** is not a valid symbol. Retry.\n";
}
}
PD9waHAKCiRpbnB1dCA9ICI1NS81KjE9IjsKJGlucHV0TGVuZ3RoID0gbWJfc3RybGVuKCRpbnB1dCk7CgokbnVtYmVyID0gMDsgLy9hY3R1YWwgbnVtYmVyCiRyZXN1bHQgPSAwOyAvL29mIHByZXZpb3VzIGFjdGlvbnMKJG9wID0gIiI7IC8vbGFzdCB1bnVzZWQgb3BlcmF0aW9uCmZvciAoJGkgPSAwOyAkaSA8ICRpbnB1dExlbmd0aDsgJGkrKykgewogICAgJGNoYXIgPSBtYl9zdWJzdHIoJGlucHV0LCAkaSwgMSk7CgogICAgaWYgKCRjaGFyID09ICIqIiB8fCAkY2hhciA9PSAiKyIgfHwgJGNoYXIgPT0gIi0iIHx8ICRjaGFyID09ICIvIikgewogICAgICAgIHN3aXRjaCgkb3ApIHsKICAgICAgICAgICAgY2FzZSgiIik6CiAgICAgICAgICAgICAgICAkb3AgPSAkY2hhcjsKICAgICAgICAgICAgICAgICRyZXN1bHQgPSAkbnVtYmVyOwogICAgICAgICAgICAgICAgJG51bWJlciA9IDA7CiAgICAgICAgICAgICAgICBicmVhazsKICAgICAgICAgICAgY2FzZSAoIioiKToKICAgICAgICAgICAgICAgICRyZXN1bHQgPSAkcmVzdWx0ICogJG51bWJlcjsKICAgICAgICAgICAgICAgICRvcCA9ICRjaGFyOwogICAgICAgICAgICAgICAgJG51bWJlciA9IDA7CiAgICAgICAgICAgICAgICBicmVhazsKICAgICAgICAgICAgY2FzZSAoIisiKToKICAgICAgICAgICAgICAgICRyZXN1bHQgPSAkcmVzdWx0ICsgJG51bWJlcjsKICAgICAgICAgICAgICAgICRvcCA9ICRjaGFyOwogICAgICAgICAgICAgICAgJG51bWJlciA9IDA7CiAgICAgICAgICAgICAgICBicmVhazsKICAgICAgICAgICAgY2FzZSAoIi0iKToKICAgICAgICAgICAgICAgICRyZXN1bHQgPSAkcmVzdWx0IC0gJG51bWJlcjsKICAgICAgICAgICAgICAgICRvcCA9ICRjaGFyOwogICAgICAgICAgICAgICAgJG51bWJlciA9IDA7CiAgICAgICAgICAgICAgICBicmVhazsKICAgICAgICAgICAgY2FzZSAoIi8iKToKICAgICAgICAgICAgICAgICRyZXN1bHQgPSAkcmVzdWx0IC8gJG51bWJlcjsKICAgICAgICAgICAgICAgICRvcCA9ICRjaGFyOwogICAgICAgICAgICAgICAgJG51bWJlciA9IDA7CiAgICAgICAgICAgICAgICBicmVhazsKCiAgICAgICAgfQoKICAgIH0gZWxzZWlmIChpc19udW1lcmljKCRjaGFyKSkgewogICAgICAgICRudW1iZXIgPSAkbnVtYmVyICogMTAgKyBpbnR2YWwoJGNoYXIpOwoKICAgIH0gZWxzZWlmICgkY2hhciA9PSAiPSIpIHsKICAgICAgICBzd2l0Y2goJG9wKSB7CiAgICAgICAgICAgIGNhc2UgKCIqIik6CiAgICAgICAgICAgICAgICAkcmVzdWx0ID0gJHJlc3VsdCAqICRudW1iZXI7CiAgICAgICAgICAgICAgICAkb3AgPSAkY2hhcjsKICAgICAgICAgICAgICAgICRudW1iZXIgPSAwOwogICAgICAgICAgICAgICAgYnJlYWs7CiAgICAgICAgICAgIGNhc2UgKCIrIik6CiAgICAgICAgICAgICAgICAkcmVzdWx0ID0gJHJlc3VsdCArICRudW1iZXI7CiAgICAgICAgICAgICAgICAkb3AgPSAkY2hhcjsKICAgICAgICAgICAgICAgICRudW1iZXIgPSAwOwogICAgICAgICAgICAgICAgYnJlYWs7CiAgICAgICAgICAgIGNhc2UgKCItIik6CiAgICAgICAgICAgICAgICAkcmVzdWx0ID0gJHJlc3VsdCAtICRudW1iZXI7CiAgICAgICAgICAgICAgICAkb3AgPSAkY2hhcjsKICAgICAgICAgICAgICAgICRudW1iZXIgPSAwOwogICAgICAgICAgICAgICAgYnJlYWs7CiAgICAgICAgICAgIGNhc2UgKCIvIik6CiAgICAgICAgICAgICAgICAkcmVzdWx0ID0gJHJlc3VsdCAvICRudW1iZXI7CiAgICAgICAgICAgICAgICAkb3AgPSAkY2hhcjsKICAgICAgICAgICAgICAgICRudW1iZXIgPSAwOwogICAgICAgICAgICAgICAgYnJlYWs7CiAgICAgICAgfQogICAgICAgIGVjaG8gJHJlc3VsdDsKICAgICAgICBleGl0KCk7CiAgICB9IGVsc2UgewogICAgICAgIGVjaG8gIkVycm9yLiBFcnJvci4gIEVycm9yLiAqKnskY2hhcn0qKiBpcyBub3QgYSB2YWxpZCBzeW1ib2wuIFJldHJ5LlxuIjsKICAgICAgICBleGl0KCk7CiAgICB9Cgp9