Suffix expression (inverse Polish expression) and evaluated (floating-point, negative, and greater than 10), C + + implementation __c++

Source: Internet
Author: User
suffix expression (inverse Polish expression) and evaluated

This is a simple application of a stack.
Rules: From left to right to traverse infix expression of each number and symbol, if the number on the output, that is, as part of the suffix expression;
If the symbol, then the top of the stack to determine the priority of the symbol, is the right bracket or priority lower than the top sign (multiplication and division priority plus minus) then the top of the stack elements out to find and output, and the current symbol into the stack, until the final output suffix expression.

Support (), + 、-、 *,/and%

#include <stdexcept> #include <iostream> #include <vector> #include <string> #include <stack

> #include <regex> #include <cmath> using namespace std;
    Split character Vector<string> split () {vector<string> temp_vecstr;
    Regex Reg_dig ("[[:d Igit:].]");

    String Temp_str, Temp_str1, TEMP_STR2, TEMP_STR3;
    cout << "Please enter an expression:" << Endl;

    Cin >> Temp_str;

        for (Auto i = Temp_str.begin (); I!= temp_str.end (); ++i) {temp_str2 = *i;

            if (Regex_search (TEMP_STR2, Reg_dig)) {temp_str1 = = TEMP_STR2;

            Auto P = i;
                if (++p = = Temp_str.end ()) {temp_vecstr.push_back (TEMP_STR1);
            Temp_str1.clear ();

            }--p;
                if (++p!= temp_str.end ()) {--p;

                TEMP_STR2 = * (++p);
   if (!regex_search (TEMP_STR2, Reg_dig)) {temp_vecstr.push_back (TEMP_STR1);                 Temp_str1.clear (); }} else {if (temp_str2 = "+" | | temp_str2 = = "| | temp_str2 = =" * "| | temp_ STR2 = = "/" | | TEMP_STR2 = "(" | |

                TEMP_STR2 = = "%") {temp_vecstr.push_back (TEMP_STR2);
                Auto P = i;

                    if (++p!= temp_str.end ()) {--p;
                    TEMP_STR3 = * (++p);

                        if (TEMP_STR3 = = "-") {temp_str1 + = TEMP_STR3;
                            if (++p!= temp_str.end ()) {auto P1 = p;
                            TEMP_STR3 = *P1; for (; Regex_search (TEMP_STR3, reg_dig) && p1!= temp_str.end (); ++p1, ++i) {Temp
                                _STR3 = *P1;
                                if (Regex_search (TEMP_STR3, Reg_dig)) {temp_str1 = = TEMP_STR3;
                    }        } temp_vecstr.push_back (TEMP_STR1);
                            Temp_str1.clear ();
                            if (Regex_search (TEMP_STR3, Reg_dig)) {++i; else {Temp_}}}}
            Vecstr.push_back (TEMP_STR2);
}} return temp_vecstr; }//Convert to suffix expression stack<string> postfix_expression (vector<string> temp_vecstr) {stack<string> Temp_Stac
    Ksymbol, Temp_stackexp, Temp_stack1, Temp_stack2; Regex Reg_dig ("[[:d Igit:].]
    +");

    String Temp_str;

        for (auto &i:temp_vecstr) {temp_str = i;
        if (Regex_search (Temp_str, Reg_dig)) {Temp_stackexp.push (TEMP_STR);
        } if (Temp_str = = "(") {Temp_stacksymbol.push (TEMP_STR); } if (temp_str = ")") {while TEMP_STACKSYMBOl.top ()!= "(") {Temp_stackexp.push (Temp_stacksymbol.top ());
            Temp_stacksymbol.pop ();
        } temp_stacksymbol.pop (); } if (Temp_str = = "+" | | | temp_str = = "-") {if (!temp_stacksymbol.empty ()) {if (temp _stacksymbol.top () = = "+" | | Temp_stacksymbol.top () = = "-" | | Temp_stacksymbol.top () = = "*" | | Temp_stacksymbol.top () = = "/" | |  Temp_stacksymbol.top () = = "%") {while (!temp_stacksymbol.empty () && temp_stacksymbol.top ()!=
                        "(") {Temp_stackexp.push (Temp_stacksymbol.top ());
                    Temp_stacksymbol.pop ();
                } temp_stacksymbol.push (TEMP_STR);
                else {Temp_stacksymbol.push (TEMP_STR);
            } else {Temp_stacksymbol.push (TEMP_STR);
  } if (temp_str = "%") {          if (!temp_stacksymbol.empty ()) {if (temp_stacksymbol.top () = "*" | | temp_stacksymbol.top () = = "/" ||  Temp_stacksymbol.top () = = "%") {while (!temp_stacksymbol.empty () && temp_stacksymbol.top ()!= "(" && temp_stacksymbol.top ()!= "+" && temp_stacksymbol.top ()!= "-") {Temp_sta
                        Ckexp.push (Temp_stacksymbol.top ());
                    Temp_stacksymbol.pop ();
                } temp_stacksymbol.push (TEMP_STR);
                else {Temp_stacksymbol.push (TEMP_STR);
            } else {Temp_stacksymbol.push (TEMP_STR);
                } if (temp_str = "*" | | temp_str = = "/") {if (!temp_stacksymbol.empty ()) { if (temp_stacksymbol.top () = "*" | | temp_stacksymbol.top () = = "/") {while!temp_stacksymbol.empty () && Temp_stacksymbol.top ()!= "(" && temp_stacksymbol.top ()!= "+" && temp_stacksymbol.top ()!= "-") {
                        Temp_stackexp.push (Temp_stacksymbol.top ());
                    Temp_stacksymbol.pop ();
                } temp_stacksymbol.push (TEMP_STR);
                else {Temp_stacksymbol.push (TEMP_STR);
            } else {Temp_stacksymbol.push (TEMP_STR);
        }} while (!temp_stacksymbol.empty ()) {Temp_stackexp.push (Temp_stacksymbol.top ());
    Temp_stacksymbol.pop ();
        while (!temp_stackexp.empty ()) {Temp_stack1.push (Temp_stackexp.top ());
        Temp_stack2.push (Temp_stackexp.top ());
    Temp_stackexp.pop ();
    cout << endl << "The suffix expression is:" << Endl << Endl;
        while (!temp_stack2.empty ()) {cout << temp_stack2.top () << ""; temp_sTack2.pop ();

    } cout << Endl << Endl;
return temp_stack1;
    }//Evaluation void Evaluation (Stack<string> temp_stackexp) {Auto Temp_num1 = 0.0, temp_num2 = 0.0; Regex Reg_dig ("[[:d Igit:].]
    +");
    Stack<double> Temp_stacknum;

    String Temp_str;

            try {while (!temp_stackexp.empty ()) {temp_str = Temp_stackexp.top ();
                if (Regex_search (Temp_str, Reg_dig)) {Temp_stacknum.push (Stod (TEMP_STR));
            Temp_stackexp.pop (); } if (Temp_str = = "+") {if (!temp_stacknum.empty ()) {temp_num1 = temp_s
                    Tacknum.top ();
                    Temp_stacknum.pop ();
                    temp_num2 = Temp_stacknum.top ();
                    Temp_stacknum.pop ();
                    Temp_stacknum.push (temp_num1 + temp_num2);
                Temp_stackexp.pop (); } else {throw runtime_error (expression input error, please reInput! ");}}
                    if (Temp_str = = "-") {if (!temp_stacknum.empty ()) {temp_num1 = Temp_stacknum.top ();
                    Temp_stacknum.pop ();
                    temp_num2 = Temp_stacknum.top ();
                    Temp_stacknum.pop ();
                    Temp_stacknum.push (TEMP_NUM2-TEMP_NUM1);
                Temp_stackexp.pop ();
                else {throw runtime_error ("Input error, please re-enter!"); } if (temp_str = "%") {if (!temp_stacknum.empty ()) {Temp
                    _NUM1 = Temp_stacknum.top ();
                    Temp_stacknum.pop ();
                    temp_num2 = Temp_stacknum.top ();
                    Temp_stacknum.pop ();
                    Temp_stacknum.push (Fmod (temp_num2, temp_num1));
                Temp_stackexp.pop (); else {throw runtime_eRror ("Input error, please re-enter!"); } if (temp_str = = "*") {if (!temp_stacknum.empty ()) {Temp
                    _NUM1 = Temp_stacknum.top ();
                    Temp_stacknum.pop ();
                    temp_num2 = Temp_stacknum.top ();
                    Temp_stacknum.pop ();
                    Temp_stacknum.push (TEMP_NUM1 * temp_num2);
                Temp_stackexp.pop ();
                else {throw runtime_error ("Input error, please re-enter!"); } if (Temp_str = = "/") {if (!temp_stacknum.empty ()) {Temp
                    _NUM1 = Temp_stacknum.top ();
                    Temp_stacknum.pop ();
                    temp_num2 = Temp_stacknum.top ();
                    Temp_stacknum.pop ();
                    Temp_stacknum.push (TEMP_NUM2/TEMP_NUM1);
                Temp_stackexp.pop (); else {THRow runtime_error ("Input error, please re-enter!");
        catch (Runtime_error &err) {cout << err.what () << Endl;
    Return Evaluation (Postfix_expression (Split ()));

The value of the cout << expression is: "<< temp_stacknum.top () << Endl << Endl;}
    int main () {while (1) {Evaluation postfix_expression (Split ()));
return 0; }
Related Article

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.