<?php
// check input
$inp = isset($_GET["inp"]) ? $_GET["inp"] : "(1+2)*(3+4)";
// calculate
$answer = calcInfix($inp);
// calculate
function calcInfix($str){
global $input_str;
$input_str = $str;
// main transaction
return plus_minus();
}
// add, minus
function plus_minus() {
$v = mul_div();
while(peek() == "+" || peek() == "-"){
$op = get();
if ($op == "+") $v += mul_div();
if ($op == "-") $v -= mul_div();
}
return $v;
}
function mul_div(){
$v = paren();
while(peek() == "*" || peek() == "/"){
$op = get();
if ($op == "*") $v *= paren();
if ($op == "/") $v /= paren();
}
return $v;
}
function paren(){
$v = get();
if ($v == '(') {
$v = plus_minus();
$t = get();
if ($t != ")") throw new Exception("カッコ未対応");
}
return $v;
}
function get(){
global $input_str;
$input_str = ltrim($input_str);
$c = substr($input_str, 0, 1);
if (strpos("+-*/()", $c) !== false){
$input_str = substr($input_str, 1);
return $c;
}
if (preg_match('#^([0-9]+)#', $input_str, $m)){
$input_str = substr($input_str, strlen($m[1]));
return intval($m[1]);
}
}
function unget($t){
global $input_str;
$input_str = $t . $input_str;
}
function peek(){
$c = get();
unget($c);
return $c;
}
$inp_ = htmlentities($inp, ENT_QUOTES);
echo <<< EOS
<!DOCTYPE html><meta charset="UTF-8">
<form>
式:<input name="inp" value="inp_" size="30">
<input type="submit" value="calculate">
</form><hr>
<div>答え: $answer</div>
EOS;