Inverse Polish method for solving mathematical expressions (c + +)

Source: Internet
Author: User

The main application of the stack, there are two functions deletespace (), stringtodouble () in my other blog: Some extension functions on string.

This procedure is only the basic function realization, does not have the error control.


#include <iostream> #include <stack> #include <string> #include <map> #include "fstring.h"/* * The inverse Polish representation is used to solve the mathematical expression * 1, the input infix representation is converted to a suffix expression * 2, the suffix expression is calculated to produce the result */using namespace std;stack<string> postorderstr;map <string, int> symbolpriority;void initializationsymbol (map<string, int>& symbol) {//define priority pair< String, int> element;element = Make_pair ("+", 0), Symbol.insert (element), element = Make_pair ("-", 0); Symbol.insert ( element = Make_pair ("*", 1), Symbol.insert (element), element = Make_pair ("/", 1); Symbol.insert (element);} void Strtopoststack (string& str, stack<string>& postorderstr) {//The input mathematical expression is first represented as a suffix expression stack<string > auxilary;//Auxiliary array for temporary storage of symbols string::iterator iter = Str.begin (); String::iterator iter1 = Iter;bool flag = false;// Whether to encounter parentheses for (; ITER! = Str.end (); ++iter) {if (*iter = = ' + ' | | *iter = = '-' | | *iter = ' * ' | | *iter = '/' | | *iter = '/' | | | *iter = = ') ' {if (iter1! = iter) {//may appear two consecutive symbols string subStr (Iter1, ITER);//Deposit Digital PostorDerstr.push (SUBSTR);} String symbol (iter,iter+1);//symbol is stored by priority, if (*iter = = ' (') {flag = True;auxilary.push (symbol);} else if (flag && *iter! = ') ') {Auxilary.push (symbol);} else if (!flag) {if (Auxilary.empty ()) {Auxilary.push (symbol);} Else{string TMP2 = Auxilary.top (); while (SYMBOLPRIORITY[TMP2] >= Symbolpriority[symbol]) {Postorderstr.push (TMP2); Auxilary.pop (); if (Auxilary.empty ()) BREAK;TMP2 = Auxilary.top ();} Auxilary.push (symbol);}} else if (flag && *iter = = ') ') {string tmp1 = Auxilary.top ();d O{postorderstr.push (TMP1); Auxilary.pop (); tmp1 = Auxi Lary.top ();} while (tmp1!= "("); Auxilary.pop (); flag = false;} Holds the next digit starting position iter1 = iter+1;}} String Tmp5 (Iter1, ITER);p Ostorderstr.push (TMP5), while (!auxilary.empty ()) {string tmp6 = Auxilary.top (); Postorderstr.push (TMP6); Auxilary.pop ();}} Double Computefun (stack<string> postorderstrnew) {//overflow control stack<double> computestack;//COMPUTE auxiliary stack while (! Postorderstrnew.empty ()) {string tmp7 = Postorderstrnew.top (); if (TMP7 = = "+" | | tmp7 = "-" || TMP7 = = "*" | | TMP7 = = "/") {//] The sign is taken out of the calculation auxiliary stack of two elements to start the calculation, and the results are pressed into the stack double value1, value2, value3;value1 = Computestack.top (); Computestack.pop (); value2 = Computestack.top (); Computestack.pop (); if (TMP7 = = "+") {Value3 = value2 + value1;} else if (TMP7 = = "-") {value3 = value2-value1;} else if (TMP7 = = "*") {value3 = value2 * value1;} Else{value3 = value2/value1;} Computestack.push (VALUE3);} Else{double value;//Converts the character to a value and presses it into the stack value = stringtodouble (TMP7); Computestack.push (value);} Postorderstrnew.pop ();} return Computestack.top ();} int main () {Initializationsymbol (symbolpriority); string mathstr;cout << "Please input a math string:" <<endl ; Getline (CIN,MATHSTR); cout << "The string you are input is:" << endl;deletespace (MATHSTR); cout << mathstr& Lt;<endl;strtopoststack (Mathstr, Postorderstr);stack<string> Postorderstrnew;while (!postOrderStr.empty ( ) {String tmp4 = Postorderstr.top ();//cout << tmp4 << "";//rollover to another stack is the suffix expression postorderstrnew.push (tmp4); PostorDerstr.pop ();} cout << endl;cout << "The compute result is:" <<computefun (postorderstrnew) <<endl;return 0;}


Copyright NOTICE: This article for Bo Master original article, without Bo Master permission not reproduced.

Inverse Polish method for solving mathematical expressions (c + +)

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.