Data structure--stack--infix expression and suffix expression

Source: Internet
Author: User

What is infix expression and what is a suffix expression

The polynomial calculations we generally see are composed of infix expressions: 1+2*3+4/3

Like this, why is it that infix? Because its calculated symbols are in the middle of two numbers.

Then it's natural to understand. The suffix expression is a calculated symbol that follows two numbers.

such as 123*+43/+

What is the relationship between infix expression and suffix expression?

In fact, you will find that the formula given above is actually the same, but the order of calculation in the suffix expression you can not understand it.

Because we are used to fancy the calculation of the prefix expression.

In fact, they can be converted between each other. They can also express the same meaning, the same computational formula.

Why do you propose a suffix expression

There will be a problem right here, why not use infix expression, and put forward an ugly suffix expression.

First of all, for us, when we calculate, we see multiplication and division as a natural way to calculate, and for the computer so stupid things, it does not know.

For infix expressions, the computer can not anticipate whether there will be higher-order calculation symbols, whether or not to calculate first, so in the order of calculation is likely to error.

So there is the suffix expression, and the computer does not have to worry about the order of calculation when computing the suffix expression. So the question is, how do you calculate the value of a suffix expression?

Don't worry, let's start by looking at how to convert an infix expression to a suffix expression.

infix expression converted to suffix expression

Here we have agreed in advance, our formula contains subtraction and parentheses, all the data we temporarily in single-digit, does not include the second square and brackets. Of course it is not possible, we will try to simplify the problem here.

1. Swipe from left to right to iterate through an expression:
2. If the current traversal of the character ch is an operand, the print
3. Else If The current traversal of CH is ' (', into the stack
4. Else If The current traversal of CH is ') ', the top element of the stack pops up until the stack is empty or popped ' ('
5. Else,
.... 5.1 If the precedence of the previous operator is smaller than the priority of the current operator CH, or the stack is empty on the stack.
....... 5.2 Else, the operator that pops up the top of the stack and prints until the stack is empty or the current operator CH has precedence over the top of the stack. Put the current operator into the stack.
6. Repeat 2-6 steps until the traversal is complete
7. Eject and print the remaining operators in the stack

Code description
#include <cstdio>#include<iostream>#include<cstdlib>#include<string>#include<string.h>#include<stack>using namespacestd;intGetRank (CharSign ) {    Switch(sign) { Case '+':         Case '-':            return 1;  Case '*':         Case '/':            return 2; }    return-1;}intMain () {intI=0; Stack<Char>Expstack; stringexpression; CIN>>expression;  for(i=0; I<expression.length (); i++)    {        if(expression[i]>='0'&& expression[i]<='9') {cout<<Expression[i]; }        Else if(Expression[i] = ='(') {Expstack.push (expression[i]); }        Else if(Expression[i] = =')')        {            Chartop =Expstack.top ();            Expstack.pop ();  while(!expstack.empty () && Top! ='(') {cout<<top; Top=Expstack.top ();            Expstack.pop (); }        }        Else        {            intnow =GetRank (Expression[i]); if(Expstack.empty () | | now >GetRank (Expstack.top ()))            {Expstack.push (expression[i]); }            Else            {                 while(!expstack.empty () && now <=GetRank (Expstack.top ())) {cout<<Expstack.top ();                Expstack.pop ();            } expstack.push (Expression[i]); }        }    }         while(!Expstack.empty ()) {cout<<Expstack.top ();    Expstack.pop (); }    return 0;}

Calculation of postfix expressions

We can now get the suffix expression, so how to use it to calculate.

In fact, it is very simple, first we observe that there are no parentheses in the suffix expression, so it is easy for us to calculate the order.

Read from left to right, digital into the stack, if you encounter a calculation symbol (such as encountering a plus sign), stack two numbers and then calculate (such as two numbers added), and then the results into the stack.

The last data in the stack is the result of the calculation.

Thinking

Stack for the calculation with parentheses, arithmetic and so more useful, the use of the characteristics of the stack can save some of the low level of calculation, let the first calculation of high level.

After encountering such a problem, you can consider using stacks first.

Data structure--stack--infix expression and suffix expression

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.