Detailed conversion and expression evaluation of reverse Polish notation

For a calculation formula such as: 3 * (5 + 6) -2

This equation is called infix expression, people watching would be more convenient if direct calculation using a computer will be difficult, so it is a postfix infix expression becomes easy to understand the computer to calculate.

Postfix expression called Reverse Polish Notation, the amount of calculation EDITORIAL, the operator is written on the back, and the brackets can be removed

As becomes ab + a + b

a * (b + c) may be changed to abc + *

 

The general formula into reverse Polish notation, follow these steps:

1. From left to right scan formula, if digital, the calculated value of the digital output directly

2. If the left parenthesis, directly into the stack

3. If it is a right parenthesis, check whether the top of the stack is a left parenthesis, if it is, the left parenthesis from the stack,

If not, the top of the stack of output symbols, pop, continue to the third step, until you find an opening parenthesis

4. If +, -, *, /, priority operators, comparison operators, and the stack current operators,

If the operator priority higher than the priority of the operator stack or the stack or the stack is empty symbols in parentheses, the operator of the stack,

Otherwise, the situation is less, the output of the symbol stack, the stack.

 

All the elements in the equation are dealt with can be obtained by the equation reverse Polish notation.

C ++ code as follows:

#include <bits/stdc++.h>
using namespace std;
char str[100][20];
int len=0;   //记录输入的算式长度 
/*
*用于计算运算符的优先级 
*/
int Grade(char ch) 
{
	if(ch=='*' || ch=='/')
		return 2;
	else if(ch=='+' || ch=='-')
		return 1;
	else return 0;
}
void Against()
{
	stack<char>s;
	//输出原算式 
	printf("原算式:\n"); 
	for(int i=0 ;i<len; i++) 
		printf("%s ", str[i]);
	printf("\n");
	printf("转化成逆波兰表达式:\n"); 
	
	//转化成逆波兰表达式 
	for(int i=0; i<len; i++) {
		//如果是数直接输出这个数 
		if(str[i][0]>='0' && str[i][0]<='9') 
			printf("%s ", str[i]);
		//不是数,就为运算符,这是在输入的中缀式合法的情况下计算的 
		else if(s.empty()) {
		//当栈为空时,直接把当前运算符入栈 
			s.push(str[i][0]);
		}
		
		//当运算符为左括号时,无条件入栈 
		else if(str[i][0]=='(')  
			s.push('(');
		else if(str[i][0]==')') {
			/*
			*当运算符为有括号时, 查看栈顶元素,
			*当栈顶元素不为左括号,出栈并输出
			*当栈顶元素为左括号时,出栈 
			*/
			while(1) {
				char ch = s.top();
				if(ch!='(') {
					s.pop();
					printf("%c ", ch);
				}
				else {
					s.pop();
					break;
				}
			}
		}
		else {
			/*判断 +, -, *, /, 四种符号*/
			while(1) {
				//如果栈为空, 入栈 
				if(s.empty()) {
					s.push(str[i][0]);
					break;
				}	
				/*
				*比较当前运算符和栈顶运算符的优先级
				*如果当前的大于栈顶的,入栈
				*否则出栈输出并继续比较 
				*/ 
				char ch = s.top();
				char operater = str[i][0];
				if(Grade(operater) > Grade(ch)){
				//((operater == '*' || operater=='/') && (ch=='+' || ch=='-') || ch=='(')
				//两种if条件都可以判断运算符的优先级 
					s.push(operater);
					break;
				}	
				else {
					s.pop();
					printf("%c ", ch);
				}
			}	
		}
	}
	
	//把剩下栈中的运算符输出 
	while(!s.empty())
	{
		printf("%c ", s.top());
		s.pop();
	}
}
int main()
{
	/*
	*输入时注意每个数和运算符要分开,为了以后比较好判断 
	*/
	while(scanf("%s", str[len++])!=EOF);
	len--;
	Against();
} 

 

To 3 * (5 + 6) -2 as an example, is converted to the Reverse Polish Notation * 356 + 2 -

So how reverse Polish notation this calculation?

Computer reverse Polish notation is relatively simple.

Given a reverse Polish notation, calculated as follows:

1. Analyzing digital or operator, if it is digital, the digital value of the stack is obtained.

2. If the operator, two numbers popped from the stack, the stack with the number before the stack with the number of operation according to the operator, then the value obtained by the stack.                                         

May not be well understood that this sentence, such as over-first-out stack num1, after the stack is num2, minus the time for the operator, the number of new num3 = num2 - num1.

 

C ++ code as follows

#include <bits/stdc++.h>
using namespace std;
char str[100][20];
int len=0;   //记录输入的算式长度 
int Value_Against()
{
	stack<double>s;
	
	printf("逆波兰表达式: ");
	for(int i=0; i<len; i++)
		printf("%s ", str[i]);
	printf("\n");
	
	//遍历每个字符串 
	for(int i=0; i<len; i++) {
		//如果为数字,计算出数字,入栈 
		if(str[i][0] >= '0' && str[i][0] <= '9') {
			int num=0;
			for(int j=0; j<strlen(str[i]); j++)
				num = num*10 + (str[i][j] - '0');
			s.push(num);
		}
		else {
			/*
			*如果是运算符,从栈中弹出两个数,
			*根据运算符计算后把新的数入栈 
			*/
			double num1 = s.top();
			s.pop();
			double num2 = s.top();
			s.pop();
			double num3;
			
			if(str[i][0] == '+')
				num3 = num2 + num1;
			else if(str[i][0] == '-')
				num3 = num2 - num1;
			else if(str[i][0] == '*')
				num3 = num2 * num1;
			else if(str[i][0] == '/')
				num3 = num2 / num1;
			
			s.push(num3);
		}
	}
	//最后栈顶元素即为答案 
	printf("求值为%.2lf\n", s.top());
}
int main() 
{
	while(scanf("%s", str[len++])!=EOF);
	
	len--;
	
	Value_Against();
} 

Take the above has been transformed good reverse Polish notation, for example

Value can be determined 3 * (5 + 6) 31 -2

发布了463 篇原创文章 · 获赞 122 · 访问量 5万+

Guess you like

Origin blog.csdn.net/qq_41505957/article/details/103026407