plugins/do_calculate.php
// calculates the result of an expression in reverse polish notation
calculates the result of an expression in reverse polish notation
calculates the result of an expression in reverse polish notation
function calculate_rpn($rpnexp) {
		$stack = array();
		foreach($rpnexp as $item) {
			if (is_operator($item)) {
				if ($item == '+') {
					$j = array_pop($stack);
					$i = array_pop($stack);
					array_push($stack, $i + $j);
				}
				if ($item == '-') {
					$j = array_pop($stack);
					$i = array_pop($stack);
					array_push($stack, $i - $j);
				}
				if ($item == '*') {
					$j = array_pop($stack);
					$i = array_pop($stack);
					array_push($stack, $i * $j);
				}
				if ($item == '/') {
					$j = array_pop($stack);
					$i = array_pop($stack);
					array_push($stack, $i / $j);
				}
				if ($item == '%') {
					$j = array_pop($stack);
					$i = array_pop($stack);
					array_push($stack, $i % $j);
				}
			} else {
				array_push($stack, $item);
			}
		}
		return $stack[0];
	}
function calculate_rpn ( $ rpnexp ) { $ stack = array ( ) ; foreach ( $ rpnexp as $ item ) { if ( is_operator ( $ item ) ) { if ( $ item == '+' ) { $ j = array_pop ( $ stack ) ; $ i = array_pop ( $ stack ) ; array_push ( $ stack , $ i + $ j ) ; } if ( $ item == '-' ) { $ j = array_pop ( $ stack ) ; $ i = array_pop ( $ stack ) ; array_push ( $ stack , $ i - $ j ) ; } if ( $ item == '*' ) { $ j = array_pop ( $ stack ) ; $ i = array_pop ( $ stack ) ; array_push ( $ stack , $ i * $ j ) ; } if ( $ item == '/' ) { $ j = array_pop ( $ stack ) ; $ i = array_pop ( $ stack ) ; array_push ( $ stack , $ i / $ j ) ; } if ( $ item == '%' ) { $ j = array_pop ( $ stack ) ; $ i = array_pop ( $ stack ) ; array_push ( $ stack , $ i % $ j ) ; } } else { array_push ( $ stack , $ item ) ; } } return $ stack [ 0 ] ; }
calculates the result of an expression in reverse polish notation
calculates the result of an expression in reverse polish notation