infix expression for suffix, then evaluated by suffix expression

Source: Internet
Author: User

Person: infix expression

Computer: suffix expression

The infix suffix is required and then calculated. Either step requires a stack:

For (3 + 4) x5-6

One infix turn suffix

The stack involved is used to store symbols such as "+-x/()"

Thought:

Meet the digital output; Meet the symbol into the stack

When the empty stack or symbol is the left parenthesis, it is directly into the stack;

When the operator is the symbol to be put into the stack is greater than the symbol in the stack is now in the stack than the operator is larger or the output of the stack, and then into the stack;

When the operator is a closing parenthesis, the symbol in the stack until the first opening parenthesis is stacked, and the unsigned symbol is output

Algorithm:

Infix expression is changed to postfix expression void Mid2after (char *a, char *&b) {int L=strlen (a); int i=0,j=0;sqstack S;initstack (s); while (i<l &&s.top>=-1) {if (a[i]>= ' 0 ' &&a[i]<= ' 9 ') {b[j++]=a[i++];if (a[i]< ' 0 ' | | A[i]> ' 9 ') b[j++]= ';} Else{if (s.top==-1| | a[i]== ' (') s.data[++s.top]=a[i++];else if (a[i]== ' * ' | | a[i]== '/') {while (s.data[s.top]== ' * ' | | s.data[s.top]== '/') {b[j++]=s.data[s.top--];} s.data[++s.top]=a[i++];} else if (a[i]== ' + ' | | a[i]== '-') {while (s.data[s.top]== ' + ' | | s.data[s.top]== '-' | | s.data[s.top]== ' * ' | | s.data[s.top]== '/') {b[j++]=s.data[s.top--];}      s.data[++s.top]=a[i++];} else if (a[i]== ') ') {I++;while (s.data[s.top]!= ' (') {b[j++]=s.data[s.top--];} s.top--;} else if (a[i]== ') i++;else {cout<< "Inlegal input!"; return;}}} while (s.top>-1) {b[j++]=s.data[s.top--];}}

Two-suffix evaluation

The suffix expression evaluation stack is primarily used to store numerical values

Thought:

Each meeting number is into the stack;

Meeting operator is the top two-bit operation of the stack, and the result is re-entered.

Note the suffix expression of the wood is enclosed in parentheses.

Algorithm:

suffix Evaluation int Get_after (char *b) {int L=strlen (b); Sqstack s;initstack (s); int I=0,ss;while (i<l&&s.top>=-1) { if (b[i]>= ' 0 ' &&b[i]<= ' 9 ') {     ss=0;while (b[i]>= ' 0 ' &&b[i]<= ' 9 ') {     int j=1;    j= (b[i]-' 0 ');    Ss=ss*10+j;     i++;    cout<<ss<< "";}   S.data[++s.top]=ss;} else {int t=s.data[s.top];    Switch (B[i])    {case ' + ': s.data[--s.top]=s.data[s.top]+t;break;     Case '-': s.data[--s.top]=s.data[s.top]-t;break;     Case ' * ': s.data[--s.top]=s.data[s.top]*t;break;     Case '/': {if (t==0) return-100;s.data[--s.top]=s.data[s.top]/t;break;              }     Case ": Break;    default:{cout<< "error! "<<B[I];RETURN-100;}    }    i++;}} if (s.top>-1) return s.data[s.top];}



infix expression for suffix, then evaluated by 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.