The implementation of the inverse Polish expression

Source: Internet
Author: User


In general, expressions are composed of operands and operators, such as arithmetic expressions usually put operators in the middle of two operands, such as the form of a+b, this form is called infix expression, then the question, whether there is a suffix expression, prefix expression???

Yes, that's right, these suffixes are expressed, and the prefix expressions are presented by the Polish mathematician, Jan Lukasiewicz.

Writes an operator before the operand, known as a Polish expression (Polish expression) or a prefix expression (Prefix expression), such as +ab;

Writes the operator after the operand, called the inverse Polish expression (Reverse Polish expression) or suffix expression (Suffix expressions), such as ab+;

650) this.width=650; "src=" Http://s1.51cto.com/wyfs02/M02/7E/D5/wKioL1cKHfyB_t3QAAAhA_9JeX8352.png "title=" capture. PNG "alt=" Wkiol1ckhfyb_t3qaaaha_9jex8352.png "/>

If we have an expression, how should we ask for its value? Here the stack will come in handy, because the operand in front of the operator, so in order to iterate through the expression, encountered when the operand to the stack, when the operator is encountered, let the nearest operator two operand out of the stack, and participate in the operation, and then press the result of the operation into the stack. The process is as follows:

650) this.width=650; "src=" Http://s1.51cto.com/wyfs02/M01/7E/D8/wKiom1cKHxHjyGqKAABA-RXiv1A220.png "title=" capture. PNG "alt=" Wkiom1ckhxhjygqkaaba-rxiv1a220.png "/>

To write the inverse Polish expression solver, you would treat an inverse polish as an array, and the array should be an array of structs, each of which contains two elements, one for the type of the array element (operand type or operator type) and one for the value of the type.

Just implement the suffix expression today, and use enumerations here to make it clearer.

#define _CRT_SECURE_NO_WARNINGS 1


#include <iostream>

#include <stack>

#include <assert.h>

using namespace Std;


Enum Type

{

Op_num,

Op_symbol,

};


Enum SYMBOL

{

ADD,

SUB,

MUL,

Div

};

Defines a struct array that includes the type of the array and the values of the arrays

struct Cell

{

Type _type;

int _value;

};

Inverse Polish expression calculation function

int COUNTRNP (Cell a[],size_t size)

{

Stack <int> s;

If the function parameter must not be empty, the assertion should be used

ASSERT (a);

for (size_t i=0;i<size;i++)

The element inside the {//array is pressed directly into the stack if it is a numeric value

if (a[i]._type==op_num)

{

S.push (A[i]._value);

}

If the element inside the array is not a value, but an operator, the two elements closest to the operator are stacked, and the operation

Else

{

int Right=s.top ();

S.pop ();

int Left=s.top ();

S.pop ();

Switch (a[i]._value)

{

Case ADD:

S.push (Left+right);

Break

Case SUB:

S.push (Left-right);

Break

Case MUL:

S.push (Left*right);

Break

Case DIV:

S.push (Left/right);

Break

Default


Break

}

}

}

return S.top ();

}


int main ()

{

Cell A[]={{op_num, 12},{op_num, 3},{op_num, 4},

{Op_symbol,add},{op_symbol,mul},

{op_num, 6},{op_symbol,sub},{op_num, 8},{op_num, 2},

{Op_symbol,div},{op_symbol,add}};

size_t size =sizeof (a)/sizeof (Cell);

COUT<<COUNTRNP (A, size) <<endl;

System ("pause");

return 0;

}


Operation Result:

650) this.width=650; "src=" Http://s2.51cto.com/wyfs02/M01/7E/D8/wKiom1cKGlbxHEBHAAAmPDN6qJ4302.png "title=" capture. PNG "alt=" Wkiom1ckglbxhebhaaampdn6qj4302.png "/>


The implementation of the inverse Polish 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.