Sicily infix expression to suffix expression

Source: Internet
Author: User
Description

Convert infix expression to postfix expression ). Assume that the operands in the infix expression are represented by a single English letter and contain only the left brace '(', right brances') 'and Binary Arithmetic Operators + ,-,*,/.
 

Input Format

The first line is the number of test samples n.
The following n rows, each of which is a string that represents an infix expression (contains only the operands and operators, left and right brackets, and does not contain any other characters). The length cannot exceed 100 characters.
 

Output Format

Output the corresponding suffix expression string for each test sample line (which only contains the operands and operators and does not contain any other characters)
 

Sample input: copy the sample input to the clipboard.
2 A+B*C-D-E/Fa+(b-c)*d+e/f
Sample output
ABC * + D-EF/-ABC-D * + EF/+

Solution 1: (AC)
#include <iostream>#include <string>#include <stack> using namespace std;bool isoper(char a){    if(a==‘+‘||a==‘-‘||a==‘*‘||a==‘/‘||a==‘%‘)        return true;    else return false;}int rank(char a){    if(a==‘*‘||a==‘/‘||a==‘%‘)        return 3;    else return 1;}int main(){    stack<char> op;    string str;    cin>>str;    int i,len= str.length();    for(i=0;i<len;i++)    {        if(!isoper(str[i]))            cout<<str[i];        else        {            if(op.empty()) op.push(str[i]);            else            {                while(!op.empty()&&rank(op.top())>=rank(str[i]))                {                    cout<<op.top();                    op.pop();                }                op.push(str[i]);            }        }    }    while(!op.empty())    {          cout<<op.top();          op.pop();    }    return 0;}

Solution 2: (currently, an incorrect answer is prompted. Here, we will put aside a paragraph and look back)

# Include <iostream> # include <stack> # include <queue> # include <vector> # include <list> using namespace STD; int main () {string data; cin> data; vector <char> temp; string out = ""; // etc. List <char> fuhao; int COUNT = 0; // The operands are processed, for (INT I = 0; I <data. length (); ++ I) {If (data [I] = '+' | data [I] = '-' | data [I] = '*' | data [I] = '/' | data [I] = '% ') {fuhao. push_back (data [I]);} else if (count <2) {temp. push_back (data [I]); + + Count;} else if (COUNT = 2) {If (fuhao. back () = '*' | fuhao. back () = '/' | fuhao. back () = '%') & (fuhao. front () = '+' | fuhao. front () = '-') {temp. push_back (data [I]); ++ count; temp. push_back (fuhao. back (); -- count; fuhao. pop_back ();} else if (fuhao. front () = '*' | fuhao. front () = '/' | fuhao. front () = '%') {temp. push_back (fuhao. front (); -- count; fuhao. pop_front (); ++ count; temp. push_back (data [I]);} else if (Fuhao. back () = '+' | fuhao. back () = '-') & (fuhao. front () = '+' | fuhao. front () = '-') {temp. push_back (fuhao. front (); -- count; fuhao. pop_front (); ++ count; temp. push_back (data [I]) ;}} while (! Fuhao. empty () {temp. push_back (fuhao. back (); fuhao. pop_back () ;}for (INT I = 0; I <temp. size (); ++ I) {cout <temp [I] ;}}

This question is simple, but % is ignored during the first time. If you do more questions on Sicily, it seems that you can exercise your thinking and increase rigor.


This question is also enclosed in parentheses, which are put on hold temporarily. There are two other methods.

Sicily infix expression to 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.