Implement reverse-Polish Stack

Source: Internet
Author: User

Implement reverse-Polish Stack

The stack method is used for non-recursive frontend and backend traversal of Binary Trees. Xpp said that we should simply implement the four arithmetic operations against the Polish stack.

This is written by reference to other programs, and the comments are messy. In addition, this is a four-digit computation that directly implements Computer Computation and does not print out the inverse polish expression. Today, my waist is too sour. I will make a change tomorrow and print out the reverse Polish style in the form of thio164. is there a maze algorithm in the stack ??? I cannot remember any non-recursive traversal in my mind.

# Include "stdafx. h "# include" stdio. h "# include" stdlib. h "# include" string. h "# define MAX_OPE_LEN 10 # define MAX_STRING_LEN 100 # define MAX_STACK_LEN 100 // typedef struct operater {char name; int pior; int opernum;} Operater; Operater opStack [MAX_OPE_LEN]; int opStacktop =-1; // Add a number indicating the top of the stack. This is also a wonderful double numStack [MAX_STACK_LEN]; int numStacktop =-1; int getPior (char name) {if (name = '(' | name = ') // note the brackets priority {return 0;} I F (name = '+' | name = '-') {return 1;} if (name = '*' | name = '/') {return 2;} if (name = '! ') {Return 3;} exit (1); // exit in other cases.} int getopernum (char name) {if (name = '+' | name = '-' | name = '*' | name = '/') {return 2 ;} if (name = '! ') {Return 1;} if (name =' ('| name =') // note that the operands of parentheses are 0; {return 0 ;} exit (1);} void pushoperater (Operater op) // The inbound operation is an operator {if (opStacktop
 
  
= '0' & * s <= '9') {string [j ++] = * s; s ++;} if (* s = '. ') {string [j ++] = * s; s ++; while (* s> = '0' & * s <= '9 ') {string [j ++] = * s; s ++ ;}} (* I) = (* I) + j; string [j] = '\ 0 '; // return atof (string) when the character ends; // convert the character to num} double operater2num (Operater op) {double num2 = popnum (); double num1 = popnum (); if (op. name = '+') {return num1 + num2;} if (op. name = '-') {return num1-num2;} if (op. name = '*') {return num1 * num2;} if (op. name = '/') {return num1/num2;} {e Xit (1) ;}///================== this part is also relatively difficult, for the time being, you can skip this step =========== double operater1num (Operater op) // non-operation? Why ?? {Double num = popnum (); if (op. name = '! ') {Double result = 1; while (num> 1) {result * = num; num --;} return result;} exit (1);} double operater (Operater op) {if (op. opernum = 2) {return operater2num (op);} if (op. opernum = 1) {return operater1num (op) ;}exit (1) ;}// enter the complex main function operation int main () {char string [MAX_STRING_LEN]; printf ("input infix expression: \ n"); scanf ("% s", string); // pay attention to scanf usage and enter it in string. % S indicates that the entered string is ah, % f, the input is float type ah, and no damn \ nOperater op, opTop; // The latter is the stack top operator, which indicates that the operator has reached the bottom opTop. name = '#'; opTop. opernum = 0; opTop. pior = 0; pushoperater (opTop); int I = 0; for (I; string [I]! = '\ 0' & string [I]! = ';) {If (string [I]> = '0' & string [I] <= '9') pushnum (getNumfromString (& string [I], & I) // obtain all the numbers starting from I and press them into num into the stack else {op. name = string [I]; op. opernum = getopernum (op. name); op. pior = getPior (op. name); opTop = popoperater (); // compare the top-stack operator of the operator with the newly discovered operator if (op. name = '(') {pushoperater (opTop); pushoperater (op);} else if (op. name = ') {while (opTop. name! = '(') // As long as it does not reach the left bracket, obtain the operand and Num in numStack for calculation, and press the calculation result into numStack {pushnum (operater (opTop )); opTop = popoperater () ;}} else {if (opTop. name! = '#' & Op. pior <= opTop. pior) {pushnum (operater (opTop) ;}else {pushoperater (opTop) ;}pushoperater (op) ;} I ++; // since the operator is a character, when the above characters and numbers are converted, they may not be added one by one} // The string has been converted, and the operator stack is left at this time, then all retrieved operations} while (opTop = popoperater ()). name! = '#') {Pushnum (operater (opTop);} printf ("% f \ n", popnum (); system ("pause"); return 0 ;}
 

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.