Infix expression detects and converts a suffix expression, computes the result of a suffix expression

Source: Internet
Author: User

The implementation code is as follows

#include <iostream> #include <stack> #include <ctype.h>using namespace std;//The number here is only one//scan expression is legal, Legal return 0, otherwise return non 0int Scanner (const char *str) {stack<char*> s;if (str==null) {return-1;} Char *p= (char*) str;while (*p!= ') {if (*p== ' (')//If the left parenthesis is in the stack {s.push (p);} else if (*p== ') ')//right parenthesis to get the stack top symbol and match {if (!s.empty ())//Stack not empty {char *t=s.top ();//Get stack top element to match if (*t== ' (')//Match {s.pop ();}}} p++;} if (!s.empty ())//Last stack is empty match failed {return-1;} return 0;} Returns the priority of an operation symbol int getpriority (char ch) {if (ch== ' + ' | | ch== '-') {return 1;} else if (ch== ' * ' | | ch== '/') {return 2;} Else{return 0;}} Infix expression converted to postfix expression int Transform (/*in*/char *src,/*out*/char *dest) {stack<char*> s;int i=0;if (src==null| | Dest==null) {return-1;} Char *psrc=src;while (*psrc!= ') {//if (*psrc>= ' 0 ' && *psrc<= ' 9 ')//If it is a number if (IsDigit (*PSRC))//If the character is a number {dest[i]=*psrc;i++;} else if (*psrc== ')//If the left parenthesis is in the stack {s.push (PSRC);} else if (*psrc== ') ')//If it is a closing parenthesis, pop up the top element of the stack {while (!s.empty () && (*s.top ())! = ' (') {dest[i]=*s.top (); I++;s.pop ();} S.pop ();//pop-up opening parenthesis}else if (*psrc==' + ') | | (*psrc== '-') | | (*psrc== ' * ') | | (*psrc== '/')) If it is operator {while (!s.empty () &&getpriority (*s.top ()) >=getpriority (*PSRC))//comparison operator precedence {//if stack top symbol priority is not low dest[i] =*s.top (); I++;s.pop ();} S.push (PSRC);//stack top symbol priority low}else{cout<< "there are illegal characters in the expression" <<endl;return-1;} psrc++;} while (!s.empty ()) {dest[i]=*s.top (); I++;s.pop ();} dest[i]= ' + ';//The last character ends with a. return 0;} algorithm int arithmetic (int left,int right,char c) {switch (c) {case ' + ': return left+right;case '-': return left-right;case ' * ': Return left*right;case '/': if (right==0) {return 0;} Else{return Left/right;} Default:return 0;}} Computes the result of the operation of the suffix expression//Description This interface returns the result, not very good, the result can be outgoing, using a first-level pointer//int calculate (char *str,int* out) int calculate (char *src) {if (src== NULL) {return-1;} stack<int> s;//stack into intchar* psrc=src; The result of an int result=0;//expression evaluates while (*psrc!= ' s) {if (IsDigit (*PSRC))//If the character is a number {S.push (*psrc-' 0 ');//Converts the number symbol to int into the stack}else if ((*psrc== ' + ') | | (*psrc== '-') | | (*psrc== ' * ') | | (*psrc== '/')) If it is the symbol {int right=s.top ();//Get left operand and eject s.pop (); int left=s.top ();//Get right operand and pop s.pop (); int Re=ariThmetic (LEFT,RIGHT,*PSRC); S.push (re);//Press the result of the operation into the stack}else{cout<< "there are illegal characters in the expression" <<endl;return-1;} psrc++;} if (S.size () ==1)//The stack has only one result, which is the result of the final operation {result=s.top (); return Result;//s.pop ();} else{cout<< "Operation result is incorrect" <<endl;return-1;} return result;} int main () {char *exp= "(2+4) *3-9";cout<< "infix expression:" <<exp<<endl;if (Scanner (exp)!=0) {printf ("Expression match failed \ n "); return-1;} Char newexp[128]; Transform (exp,newexp);cout<< "suffix expression:" <<newexp<<endl;int re=calculate (newexp);cout<< " The result of the operation is: "<<re<<endl;return 0;}

  

Infix expression detects and converts a suffix expression, computes the result of a 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.