plugins/do_calculate.php
// converts infix notation to reverse polish notation
converts infix notation to reverse polish notation
converts infix notation to reverse polish notation
function mathexp_to_rpn($mathexp) {
		$precedence = array(
			'(' => 0,
			'-' => 3,
			'+' => 3,
			'*' => 6,
			'/' => 6,
			'%' => 6
		);
		$i = 0;
		$final_stack = array();
		$operator_stack = array();
		while ($i < strlen($mathexp)) {
			$char = $mathexp{$i};
			if (is_number($char)) {
				$num = readnumber($mathexp, $i);
				array_push($final_stack, $num);
				$i += strlen($num); continue;
			}
			if (is_operator($char)) {
				$top = end($operator_stack);
				if ($top && $precedence[$char] <= $precedence[$top]) {
					$oper = array_pop($operator_stack);
					array_push($final_stack, $oper);
				}
				array_push($operator_stack, $char);
				$i++; continue;
			}
			if ($char == '(') {
				array_push($operator_stack, $char);
				$i++; continue;
			}
			if ($char == ')') {
				do {
					$operator = array_pop($operator_stack);
					if ($operator == '(') break;
					array_push($final_stack, $operator);
				} while ($operator);
				$i++; continue;
			}
			$i++;
		}
		while ($oper = array_pop($operator_stack)) {
			array_push($final_stack, $oper);
		}
		return $final_stack;
	}
function mathexp_to_rpn ( $ mathexp ) { $ precedence = array ( '(' => 0 , '-' => 3 , '+' => 3 , '*' => 6 , '/' => 6 , '%' => 6 ) ; $ i = 0 ; $ final_stack = array ( ) ; $ operator_stack = array ( ) ; while ( $ i < strlen ( $ mathexp ) ) { $ char = $ mathexp { $ i } ; if ( is_number ( $ char ) ) { $ num = readnumber ( $ mathexp , $ i ) ; array_push ( $ final_stack , $ num ) ; $ i += strlen ( $ num ) ; continue ; } if ( is_operator ( $ char ) ) { $ top = end ( $ operator_stack ) ; if ( $ top && $ precedence [ $ char ] <= $ precedence [ $ top ] ) { $ oper = array_pop ( $ operator_stack ) ; array_push ( $ final_stack , $ oper ) ; } array_push ( $ operator_stack , $ char ) ; $ i ++ ; continue ; } if ( $ char == '(' ) { array_push ( $ operator_stack , $ char ) ; $ i ++ ; continue ; } if ( $ char == ')' ) { do { $ operator = array_pop ( $ operator_stack ) ; if ( $ operator == '(' ) break ; array_push ( $ final_stack , $ operator ) ; } while ( $ operator ) ; $ i ++ ; continue ; } $ i ++ ; } while ( $ oper = array_pop ( $ operator_stack ) ) { array_push ( $ final_stack , $ oper ) ; } return $ final_stack ; }
converts infix notation to reverse polish notation
converts infix notation to reverse polish notation
// transfer operators to final stack