C + + expression evaluation (using data structure stack)

Source: Internet
Author: User

Alas, just once again in C + + wrote a more intact expression evaluation program, and finally streamlined program less than 100 lines. It doesn't make me

Think of a freshman semester just learn C language, I took a great effort, wrote hundreds of lines and the function is not very complete (at that time can not calculate the table with parentheses

Dashi) is a simple calculator program. Just got a comparison of the two programs. Feeling is still very deep, at the same time once again embodies the data structure in the program design

The importance of the.

Once that program has loopholes and complex logic, so do not mention, just talk about today's improved procedures, the idea is mainly used

Stack advanced after the data structure. There are two stacks built into the program: one for storing operators, and one for storing operands or results of operations. Basic

The process is:

(1): First set the operand stack as an empty stack, set the operator stack with ' # ' as the stack bottom element (its lowest priority).

(2): Priority is given by setting a value for the out-of-stack operator in the stack

(3): Find all the operators and operands in the expression, and go directly into the stack for the operands. operator and the operator stack.

The stack top operation is prioritized, if the stack has a large priority, then the corresponding operation and the operands and the stack operators are out of the stack, if the priority is only required

Stack operator out of the stack continue to find the next operator, if the stack has a low priority, the stack operator into the stack. Loop until you finish parsing the expression

All the operators and operands are available.

(4): Finally, only one element will be left in the operand stack, and the element will be the value of the expression being evaluated.

#include <iostream> #include <stack> #include <string>using namespace std;/* infer the precedence relation function between symbols; 0 indicates that the =,-1 represents an operator within the &LT;*C1 stack. Operator outside C2 stack */int Judge (char C1,char c2) {int a1,a2;if (' + ' ==c1| | ') -' ==c1 ' a1 = 3;if (' * ' ==c1| | ') /' ==c1) a1 = 5;if (' (' ==c1) a1 = 1;if (') ' ==c1) a1 = 7;if (' # ' ==c1) a1 = 0;if (' + ' ==c2| | ') -' ==c2 ' a2 = 2;if (' * ' ==c2| | ') /' ==c2) a2 = 4;if (' (' ==c2) a2 = 6;if (') ' ==c2) a2 = 1;if (' # ' ==c2) a2 = 0;if (A1&GT;A2) return 1;if (A1==A2) return 0;if (a1< A2) return-1;} Symbolic arithmetic function double run (char C, double d1,double D2) {switch (c) {case ' + ': return d1+d2;break;case '-': return d1-d2;break;case ') * ': Return d1*d2;break;case '/': Return D1/d2;break;default:return 0.0;break;}} int main () {char * op = ' +-*/() # '; string str; cin>>str;//to expression string str add ' # ' to end identifier Str.append (1, ' # ');stack<char> optr;//operator Stack stack<double> opnd;//operand stack int a = -1;//first the # symbol into the stack optr.push (' # '); while (true) {int b = a+1; a = Str.find_first _of (op,a+1); if (a==string::npos) break; if (a!=b) {string SS (Str,b,a-b); double D=atof (Ss.c_str ()); Data first into the stack opnd.push (d); }//Operator precedence is greater than int ju = Judge (Optr.top (), Str[a]);  if ( -1==ju)//The stack priority is large directly into the stack {Optr.push (Str[a]),} if (0==ju)//stack and inside and outside the priority is equal to the stack {optr.pop ();} if (1==ju)//stack with a large priority, out of the stack operation {double D1 = Opnd.top (); Opnd.pop (); Double D2 = Opnd.top (); Opnd.pop (); D1 = Run (Optr.top (), D2,D1); The result of operation is in stack Opnd.push (D1); Optr.pop (); a--; }}//Delete expression last ' # ' end identifier Str.erase (Str.length () -1,1);cout<<str<< "=" <<opnd.top () <<endl;}


C + + expression evaluation (using data structure stack)

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.