Calculators | Inverse Polish expression

Source: Internet
Author: User
Tags php code polish notation calculator reverse polish notation calculator

1. Principle of implementation:

Description: In my original inverse Polish calculator, postfix was used to denote inverse polish expressions. But after discussion with Buptpatriot, want to implement directly, the following is the method of direct implementation:

First, define the priority level:

function level ($OP) {
	switch ($op) {case
		' + ': Case
		'-':
			return 1;
		Case ' * ': Case
		'/':
			return 2;
		Case ' ^ ':  
			return 3;
	}
}


After that, the input sequence is scanned, the input string is read, and the numbers (possibly floating-point, negative numbers) and operators are extracted.

Collect floating-point numbers:

function Readin ($expr) {
	$expr = Str_split ($expr);
	$len  = count ($expr);
	$infix = Array ();
	$opts  = Array (' + ', '-', ' * ', '/', ' (', ') ');
	$num = "";
	for ($i = 0; $i < $len; $i + +) {
		if (In_array ($expr [$i], $opts) | | ($i = = $len-1)) {
			if ($num = Floatval ($num)) {
				Array_push ($infix, $num);
				$num = "";
			}
			if ($expr [$i] = = '-' && ($expr [$i-1] = = NULL | | $expr [$i-1] = = ' (') 
				&& (Is_numeric ($expr [$i +1]) | | $ex pr[$i +1] = = ' (')} {
				$expr [$i] = ' ^ ';//unary negotion
			}
			Array_push ($infix, $expr [$i]);
		}
		else {
			$num. = $expr [$i];
		}
	}
	Var_dump ($infix);
	return $infix;
}


For the negative sign (-) do the processing:

if ($expr [$i] = = '-' && ($expr [$i-1] = = NULL | | $expr [$i-1] = = ' (') 
				&& (Is_numeric ($expr [$i +1]) | | $ex pr[$i +1] = = ' (')
				} {$expr [$i] = ' ^ '; 
			}

At this point, the negative sign '-' turns into ' ^ '

1) If it is a number, it is deposited in the stack postfix;

2) If it is an operator:

2.1) is ' (', then deposited in stack stack;

2.2) is ' + ', '-', ' * ', '/', ', then check if stack stack is empty:

2.2.1) stack is empty: it is stored in the stack

2.2.2) stack is not empty, perform the following actions:

POPs an element op from a stack

If the element is not ' (' and the priority of the popup element is greater than the priority of the current scan element, the pop-up element is deposited into the postfix stack, and a 2 element, a, B, is popped from the postfix, and the result of a-op is computed and deposited into the postfix;

Otherwise, the popup element op is re-pressed into the stack stack

Finally, the currently scanned operator is in the stack stack

2.3) is ' ^ ': Perform the following actions:

1 elements A is popped from Postfix, and (0-a) is deposited in postfix;

2.4) is ') ': Perform the following actions:

The pop-up element is stored in the postfix stack, and a 2 element, a, B, is popped from the postfix, and the result of a Op b. is calculated and deposited into the postfix;

Until the popup element is ' (';

Draw a table to see: 4/((3-1) * * = 1 of the calculation process

Current Stack Postfix
4 Empty 4
/ / 4
( /( 4
( /(( 4
3 /(( 43
- /((- 43
1 /((- 431
) /( 42
* /(* 42
2 /(* 422
/ 44
Empty 1 (Result)


2.php Code:

 


3. Operation Result:

Input: 4/((3-1) * *)

Output:




Contains the result of the negative symbol:

Input:-( -1.2+1.8)/(1/3.0)

Output:


Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.