7-1 Expression conversions (25 points)

Source: Internet
Author: User

Topic:

The arithmetic expression has the form of prefix notation, infix notation and suffix notation. The arithmetic expression used in daily use is infix notation, that is, the two-dollar operator is in the middle of two operands. Please design your program to convert infix expressions to suffix expressions.

Input format:

The input gives an infix expression with no spaces in one line, which can contain,, + - * , \ and left and right brackets, with an () expression of no more than 20 characters.

Output format:

outputting the converted suffix expression on a single line requires that different objects (operands, operation symbols) be separated by a space, but with no extra space at the end.

Ideas:

Thanks to the big guy blog inspired over the card for one weeks title: https://www.cnblogs.com/8023spz/p/7635353.html

To summarize:

1. Build an empty stack storage operator

2, when the digital direct output, the value of the attention is that the number may be a decimal, negative (minus and number is output together) or with a positive number;

3, when encountered operator, the first comparison of the current symbol A and the top of the stack symbol B operation priority size, if a > b directly into the stack, otherwise the output stack operator until empty or stack top element as ' (';

4, when encountering ') ' operator, directly output the operator in the stack until the stack is empty or encounters the operator ' (';

5. The last operator in the output stack until the stack is empty.

6, the most card format is a case of the operator and the right parenthesis consecutive occurrences such as a (+5)-1, to carefully consider the processing of the output format;

The first is the code of the +5-1 card to vomiting blood:

#include <bits/stdc++.h>using namespacestd;Const intMAXN = 1e4 +Ten; typedefLong LongLl;stack<Char>STA;intMain () {map<Char,int>MP; stringstr; CIN>>str; BOOLIsfirst =true; mp['-'] =1, mp['+'] =1; mp['*'] =2, mp['/'] =2; mp['('] =3, mp[')'] =3;  for(inti =0; I<str.size (); i++) {        if(((i==0|| str[i-1]=='(') && (str[i]=='+'|| Str[i] = ='-')) || (str[i]>='0'&&str[i]<='9') || (str[i]=='.')) {            if(str[i]!='+') {                if(!isfirst) {////Carefully consider the position processing of the first output format, enter the (+5)-1cout<<" ";////Output 25 + 1-, malformed, in order to find this error, give an example to want to die}////cout<<Str[i]; }             while(str[i+1]=='.'|| (str[i+1]>='0'&& str[i+1]<='9') ) {i++; cout<<Str[i]; } Isfirst=false; } Else {            if(str[i]==')') {                 while(!sta.empty () && sta.top ()! ='(') {cout<<' '<<Sta.top ();                Sta.pop ();            } sta.pop (); } Else if(Sta.empty () | | Mp[str[i]] >mp[sta.top ()])            {Sta.push (str[i]); } Else {                 while(!sta.empty () && sta.top ()! ='(') {cout<<' '<<Sta.top ();                Sta.pop ();            } sta.push (Str[i]); }        }    }     while(!Sta.empty ()) {cout<<' '<<Sta.top ();    Sta.pop (); }    return 0;}/*Input Sample: 2+3* (7-4) +8/4 sample output: 2 3 7 4-* + 8 4/+*/
View Code

Correct the full-pair code:

#include <bits/stdc++.h>using namespacestd;Const intMAXN = 1e4 +Ten; typedefLong LongLl;stack<Char>STA;intMain () {map<Char,int>MP; stringstr; CIN>>str; BOOLIsfirst =true; mp['-'] =1, mp['+'] =1; mp['*'] =2, mp['/'] =2; mp['('] =3, mp[')'] =3;  for(inti =0; I<str.size (); i++) {        if(((i==0|| str[i-1]=='(') && (str[i]=='+'|| Str[i] = ='-')) || (str[i]>='0'&&str[i]<='9') || (str[i]=='.')) {            if(!Isfirst) {cout<<" "; }            if(str[i]!='+') {cout<<Str[i]; }             while(str[i+1]=='.'|| (str[i+1]>='0'&& str[i+1]<='9') ) {i++; cout<<Str[i]; } Isfirst=false; } Else {            if(str[i]==')') {                 while(!sta.empty () && sta.top ()! ='(') {cout<<' '<<Sta.top ();                Sta.pop ();            } sta.pop (); } Else if(Sta.empty () | | Mp[str[i]] >mp[sta.top ()])            {Sta.push (str[i]); } Else {                 while(!sta.empty () && sta.top ()! ='(') {cout<<' '<<Sta.top ();                Sta.pop ();            } sta.push (Str[i]); }        }    }     while(!Sta.empty ()) {cout<<' '<<Sta.top ();    Sta.pop (); }    return 0;}
View Code

7-1 Expression conversions (25 points)

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.