Reverse polish expression

Source: Internet
Author: User

The intermediate expression is converted to an inverse polish expression, which implements the basic addition, subtraction, multiplication, division, and parentheses.


The general algorithm for converting a common medium-order expression to a reverse polish expression is: first, two stacks need to be allocated. One stack S1, which serves as a temporary storage operator, contains an ending symbol ), an input stack S2 is an empty stack that is opposite to the Polish stack. The S1 stack can be first placed with the operator # With the lowest priority. Note that the infix should end with the operator with the lowest priority. Other characters can be specified, not necessarily. Take characters from the left end of the infix and perform the following steps in sequence: 1) if the extracted characters are operands, analyze the complete number of operations, this operand is directly sent to the S2 stack. 2) if the extracted character is an operator, compare the operator with the element at the top of the S1 stack. If the operator has a higher priority than the operator at the top of the S1 stack, the operator is added to the S1 stack. Otherwise, the top operator of the S1 stack is popped up and sent to the S2 stack until the top operator of the S1 stack is lower than or equal to) the operator priority, this operator is sent to the S1 stack. 3) if the extracted character is "", it is directly sent to the top of the S1 stack. 4) if the extracted character is ")", the operators between the "" closest to the top of the S1 Stack are pushed to the S2 stack one by one, and "" is discarded at this time. 5) Repeat the above 1 ~ Step 4 until all input characters are processed. 6) if the extracted character is "#", all operators in the S1 Stack are not included, it is sent to the S2 stack in sequence. After completing the preceding steps, the S2 stack outputs the result in a reverse Polish format. However, S2 should perform reverse processing. You can use the inverse Polish calculation method!


# Include <iostream> using namespace std; # define MaxSize 100 # define MaxOp 7 struct {char ch; int pri;} lpri [] ={{ '=', 0 }, {'(', 1}, {'*', 5}, {'/', 5}, {'+', 3}, {'-', 3 }, {')', 6 }}, rpri [] = {'=', 0}, {'(', 6}, {'*', 4 }, {'/', 4}, {'+', 2}, {'-', 2}, {')', 1}; int leftpri (char op) {int I; for (I = 0; I <MaxOp; I ++) if (lpri [I]. ch = op) return lpri [I]. pri;} int rightpri (char op) {int I; for (I = 0; I <MaxOp; I ++) if (rpri [I]. ch = op) return Rpri [I]. pri;} int InOp (char ch) {if (ch = '(' | ch = ') '| ch =' + '| ch ='-'| ch =' * '| ch ='/') return 1; else return 0;} int Precede (char op1, char op2) {if (leftpri (op1) = rightpri (op2) return 0; else if (leftpri (op1) <rightpri (op2) return-1; else return 1;} void trans (char * exp, char postexp []) {struct {char data [MaxSize]; int top ;} op; int I = 0; op. top =-1; op. top ++; op. data [op. to P] = '; while (* exp! = '\ 0') {if (! InOp (* exp) {while (* exp> = '0' & * exp <= '9') {postexp [I ++] = * exp; exp ++; if (* exp = '. ') {postexp [I ++] = * exp; exp ++;} postexp [I ++] =' # ';} else {switch (Precede (op. data [op. top], * exp) {case-1: op. top ++; op. data [op. top] = * exp; exp ++; break; case 0: op. top --; exp ++; break; case 1: postexp [I ++] = op. data [op. top]; op. top --; break ;}}while (op. data [op. top]! = ') {Postexp [I ++] = op. data [op. top]; op. top --;} postexp [I] = '\ 0';} float calculated (char * postexp) {struct {float data [MaxSize]; int top;} st; float d, a, B, c, e, f; st. top =-1; while (* postexp! = '\ 0') {switch (* postexp) {case' + ': a = st. data [st. top]; st. top --; B = st. data [st. top]; st. top --; c = a + B; st. top ++; st. data [st. top] = c; break; case '-': a = st. data [st. top]; st. top --; B = st. data [st. top]; st. top --; c = B-a; st. top ++; st. data [st. top] = c; break; case '*': a = st. data [st. top]; st. top --; B = st. data [st. top]; st. top --; c = a * B; st. top ++; st. data [st. top] = c; break; case '/': a = st. data [st. top]; st. top --; B = St. data [st. top]; st. top --; if (! = 0) {c = B/a; st. top ++; st. data [st. top] = c;} else {cout <"Division by zero error! "<Endl; exit (0) ;}break; default: d = 0; e = 0; f = 0; while (* postexp> = '0' & * postexp <= '9') {d = 10 * d + * postexp-'0'; postexp ++; if (* postexp = '. ') {postexp ++; while (* postexp> = '0' & * postexp <= '9') {f ++; e = e * 10 + * postexp-'0'; postexp ++;} for (int I = 0; I <f; I ++) e = e/10;} d = d + e; st. top ++; st. data [st. top] = d; break;} postexp ++;} return st. data [st. top];} int main () {char exp [MaxSize]; char postexp [MaxSize]; float value = 0; cout <"Enter the formula you want to calculate :"; cin> exp; trans (exp, postexp); cout <"converted to a suffix expression:" <postexp <endl; value = calculated (postexp ); cout <"Calculation Result:" <value <endl; return 0 ;}


This article is from the "Calm dreamer" blog, please be sure to keep this source http://idiotxl1020.blog.51cto.com/6419277/1288623

Related Article

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.