C + + implements Infix expression pre-and suffix

Source: Internet
Author: User

#include <iostream> #include <string> #include <stack>using namespace std;bool isint (char ch) {if (ch    >= ' 0 ' &&ch<= ' 9 ') return true; return false;} BOOL Isoperator (char ch) {if (ch== ' + ' | | ch== '-' | | ch== ' * ' | |    ch== '/') return true; return false;}    int oplevel (char ch) {int level;        Switch (CH) {case ' + ': Case '-': level=1;    Break    Case ' * ': level=2;    Case '/': break;        default:level=0;    Break } return level; /* infix-to-prefix algorithm 1) to reverse the input string. 2) Check the next element of the input. 3) If it is an operand, add it to the output string. 4) If it is closed parenthesis, press it on the stack.    5) If the operator, then I) if the stack is empty, this operator into the stack.    II) If the top of the stack is a closing parenthesis, this operator is in the stack.    III) if its precedence is greater than or equal to the top of the stack operator, this operator is placed on the stack. IV) Otherwise, the stack top operator is out of the stack and added to the output string, repeating step 5. 6) In the case of open brackets, the operators in the stack stack and output until the closing brackets are encountered. Close the parentheses out of the stack and discard. 7) If the input is not complete, skip to step 2. 8) If the input is complete, all remaining operators in the stack are stacked and added to the output string. 9) The reverse of the output string.    */string Priorder (String mystr) {stack<char> opstack;    string result;        for (int i=mystr.length ()-1; i>=0; i--) {char ch=mystr[i]; if (Isint (ch)) {ResulT.push_back (CH);        } else if (') ' ==ch) {opstack.push (CH); } else if (Isoperator (CH))//operator {while (true) {if (Opstack.empty () | | | Opstack.top () = = ') ' | |                (Oplevel (CH) >=oplevel (Opstack.top ())))                    {Opstack.push (CH);                Break                    } else {Result.push_back (opstack.top ());                Opstack.pop ();                }}} and else if (' (' ==ch) {while (Opstack.top ()! = ') ') {                Result.push_back (Opstack.top ());            Opstack.pop ();        } opstack.pop ();        }} while (!opstack.empty ()) {Result.push_back (Opstack.top ());    Opstack.pop (); } return result;    /* infix-to-suffix algorithm */string postorder (String mystr) {string result;    Stack<char> Opstack; for (int i=0; i<mystr.length (); I+ +) {char ch=mystr[i];        if (Isint (ch)) {result.push_back (CH);        } else if (' (' ==ch) {opstack.push (CH); } else if (Isoperator (CH)) {while (true) {if (Opstack.empty () | | | Opstack.top () = = ' (' | |                    Oplevel (CH) >=oplevel (Opstack.top ())) {Opstack.push (CH);                Break                    } else {Result.push_back (opstack.top ());                Opstack.pop ();                }}}} else if (') ' ==ch) {while (Opstack.top ()! = ' (') {                Result.push_back (Opstack.top ());            Opstack.pop ();        } opstack.pop ();        }} while (!opstack.empty ()) {Result.push_back (Opstack.top ());    Opstack.pop (); } return result;    int main () {string mystr;    cin>>mystr; string RESult;    Result=priorder (MYSTR);    for (int i=result.length ()-1; i>=0; i--) {cout<<result[i];    } cout<<endl;    Result=postorder (MYSTR);    for (int i=0; i<=result.length (); i++) {cout<<result[i]; } return 0;}

  

C + + implements Infix expression pre-and suffix

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.