infix Input Inverse Polish calculator program part1

Source: Internet
Author: User

When looking at the K&r, it mentions the inverse Polish notation, honestly see me vaguely, mainly this anti-human suffix notation made by the calculator, the General people simply do not know how to input well. Today, when reading, saw the infix expression to the suffix expression method only to realize that the original is less this step. Now I know how to do a usable inverse Polish calculator.

Let's start with a brief introduction to how to complete this step conversion. infix expressions are the expressions that we are accustomed to, such as: 1+2*3 the suffix expression should be written 123*+ if the operator precedence is considered.

Written in the form of a suffix expression, we can easily solve the computational problem by using the stack, the idea is to push it into the stack when we see a number, to pop the corresponding operand from the stack when encountering an operator, and to push the result of the operation into the stack, and the value of the last stack is the value of the expression.

Although the suffix expression is convenient to calculate, it is cumbersome to enter. To write a user-friendly calculator program, we first need to complete the infix expression to the suffix expression conversion. We use two arrays to hold infix and suffix expression characters. Gets the input of the infix expression from the user into the corresponding array. The infix expression is then traversed. When we read an operand we immediately write it to the array of the suffix expression, and when we read the operator or the opening parenthesis we push it into the stack (starting with an empty stack). If we read the closing parenthesis, we pop the stack element and write it to the array until we encounter the opening parenthesis, notice that the left parenthesis pops up without writing to the array, and if we read the operator again (+-*/), we eject the operator from the stack until we encounter the lower priority element or the opening parenthesis (note that it must be lower, And then push the operator into the stack, and if we're done reading, we'll pop up the stack element until the stack becomes an empty stack.

With the algorithm, it is relatively simple to turn it into code, and we write this conversion as a function, Tran ().

#include <stdio.h>#include<ctype.h>#defineMAXSIZE 100//maximum number of characters that can be entered and the capacity of the stackCharInfix[maxsize]; Infix expression ArrayCharPostfix[maxsize]; Array of suffix ExpressionsCharStack[maxsize]; Creation Stackinttop =-1;
intSize =0; Array sizevoidPushCharc)//Because the elements of this program into the stack can not overflow, here do not do error detection {stack[++top] =C;}CharPopvoid){ if(Top >=0) returnstack[top--]; Elseprintf ("Stack empty!");}voidTranCharA[],Charb[]) { inti,j,tmp; for(i =0, j =0; i < size; i++) { if(IsDigit (a[i]) | | a[i] = ='.') B[j++] =A[i]; Else if(A[i] = ='('{//Consider a simple parenthesis, or a negative (-X) TMP= i+1; if(A[tmp] = ='-'{//If it is negative-X, copy (-X) to the suffix array while(A[i]! =')') B[j+ +] = a[i++]; B[j++] =')'; } Elsepush (A[i]); } Else if(A[i] = ='+'|| A[i] = ='-') {//if it is + or-- while(Top >=0&& Stack[top]! ='(')//pop up until the parentheses or the empty stack b[j++] =pop (); Push (A[i]); } Else if(A[i] = ='*'|| A[i] = ='/') {//if it is * or/, consider whether to eject while(Stack[top] = ='*'|| Stack[top] = ='/') B[j++] =pop (); Push (A[i]); } Else if(A[i] = =')') { while(Stack[top]! ='(') B[j++] =pop (); Pop (); } } while(Top >=0)//To eject all elements of the stack b[j++] =pop ();}

So we're done. Turn the infix expression of the user input into a computer-friendly suffix expression, followed by the processing of the suffix expression. In this regard k&r on the detailed explanation. If the reader has doubts, it can be used as reference. Of course, if you don't have k&r on hand, you can read my next blog post.

infix Input Inverse Polish calculator program part1

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.