1, binary expression evaluation
Based on expression evaluation Binary Tree
description
Enter an expression (expression are less than the number of positive integers. 10) , using the binary tree to represent this expression , creating an expression tree, and then using the binary tree traversal operations find the value of the expression .
Entry
Multiple sets of data. Each data line, as an expression, expression to '=' end. When you enter only a "=", the input end.
Export
Each output data line, the value of the expression.
Sample input 1
2*(2+5)= 1+2= =
Sample Output 1
14 3
#include<iostream> #include<stack> #include<string> #include<cstring> #include<queue> using namespace std; typedef struct Node* BinTree; typedef BinTree BT; string s; queue<char> num; queue<char> op; struct Node { char Data; BT Left; BT Right; int ans; }; int fact(char c) { if (c >= '0' && c <= '9') return 1; else return 2; } BT createNode(char c) { BT p = new Node; p->Data = c; p->Left = p->Right = NULL; if (fact(c) == 1) p->ans = c - '0'; else p->ans = 0; Return P; } the BT createTree () { for ( int I = 0 ; I <s.size () - . 1 ; I ++ ) { IF (FACT (S [I]) == . 1 ) num.push (S [I ]); the else op.push (S [I]); } the BT Head = NULL; int in flag = 0 ; // case when labeled with parentheses int the sflag = 0 ; // process starts in parentheses case where IF (S [ 0 ] == ' (' ) = The sflag . 1 ; the while (! Op.empty ()) // not empty { char Option; Option = op.front (); // get a first operator op.pop (); IF (Option =! ' ( ' && = Option! ' ) ' ) { the BT T = the createNode (Option); IF (Option == ' + ' || Option == ' - ' ) { IF (In Flag ==0 ) { IF (Head == NULL) { T -> Left = the createNode (num.front ()); num.pop (); T -> = Right the createNode (num.front ()); num.pop () ; // take two numbers, as two children } the else { T -> = Left Head; T -> = Right the createNode (num.front ()); num.pop (); } Head= T; } else { if (Head == NULL) { T->Left = createNode(num.front()); num.pop(); T->Right = createNode(num.front()); num.pop(); Head = T; } else { T->Left = Head->Right; Head->Right = T; T->Right = createNode(num.front()); num.pop(); } } } else if (option == '*' || option == '/') { if (flag == 0) { if (Head == NULL) { T->Left = createNode(num.front()); num.pop(); T->Right = createNode(num.front()); num.pop(); Head = T; } else { if (sflag == 1 || Head->Data == '*' || Head->Data == '/') { T->Left = Head; Head = T; T->Right = createNode(num.front()); num.pop(); sflag = 0; } else { T->Left = Head->Right; Head->Right = T; T->Right = createNode(num.front()); num.pop(); } } } if (flag == 1) { if (Head == NULL) { T->Left = createNode(num.front()); num.pop(); T->Right = createNode(num.front()); num.pop(); Head = T; } else { T->Left = Head->Right; Head->Right = T; T->Right = createNode(num.front()); num.pop(); } } } } else if (option == '(') { flag = 1; } else if (option == ')') { flag = 0; } } return Head; } void solve(BT L) { if (L) { solve(L->Left); solve(L->Right); char option = L->Data; if (option == '+') L->ans = L->Left->ans + L->Right->ans; if (option == '-') L->ans = L->Left->ans - L->Right->ans; if (option == '*') L->ans = L->Left->ans * L->Right->ans; if (option == '/') L->ans = L->Left->ans / L->Right->ans; } } int main() { while (cin >> s && s[0] != '=') { BT H = createTree(); solve(H); cout << H->ans << endl; } return 0; }