Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include<iostream>
- #include<string>
- #include <stack>
- using namespace std;
- struct node{
- char info;
- node* left;
- node* right;
- };
- //Determines the priority of an operator
- int priority(char op){
- // if ((op =='('))
- // return 0;
- if ((op =='+') || (op =='-'))
- return 1;
- else if ((op =='/') || (op =='*'))
- return 2;
- else return -1;
- }
- //Places a char from the input stack into a new treenode
- node* makeNode(char info){
- node* childnode;
- childnode = new node;
- childnode->info = info;
- childnode->left = NULL;
- childnode->right = NULL;
- return childnode;
- }
- //Using a recursive function, the value of the expression is Calculated
- int evaluateTree(node* treenode){
- int x,y,z;
- if ((treenode->info) == '+'||(treenode->info) == '-'||(treenode->info) == '*'||(treenode->info) == '/') {
- x = evaluateTree(treenode->left);
- y = evaluateTree(treenode->right);
- if (treenode->info=='+')
- z=x+y;
- else if (treenode->info=='-')
- z=x-y;
- else if (treenode->info=='*')
- z=x*y;
- else if (treenode->info=='/')
- z=x/y;
- return z;
- }
- else return treenode->info - '0';
- }
- bool isdigit(char temp)
- {
- return temp <'9' && temp >'0';
- }
- void infixTopostfix(string infix, string &postfix)
- { int j = 0;
- stack <char> charStack;
- char temp;
- for(int i = 0; i < infix.length(); i++)
- { temp = infix[i];
- if (isdigit(temp))
- postfix += temp;
- else if (temp == '(')
- charStack.push(temp);
- else if (temp == '+'||temp == '-'||temp == '*'||temp == '/')
- { while( !charStack.empty() && (priority(temp) <= priority(charStack.top())))
- { char c = charStack.top();
- charStack.pop();
- postfix += c;
- }
- charStack.push(temp);
- }
- else if (temp == ')')
- { while(! charStack.empty() && charStack.top() != '(')
- { char c = charStack.top();
- charStack.pop();
- postfix += c;
- }
- charStack.pop();
- }
- }
- while( ! charStack.empty() )
- { char c = charStack.top();
- charStack.pop();
- postfix += c;
- }
- }
- int main(){
- stack<node*> treenodes; // stack for output node pointer addresses
- char temp, ch, again=' ';
- string infix, postfix; // the infix expression read from the line
- while (again != 'n')
- { postfix=""; // the infix expression read from the line
- cout << "Please enter an Infix Expression" << endl;
- cin >> infix;
- infixTopostfix(infix, postfix);
- for(int i =0; i<postfix.length();i++)
- { temp=postfix[i];
- //If it is operand, make it into a node, add push it
- if (isdigit(temp))
- treenodes.push(makeNode(temp));
- if ((temp=='+') || (temp=='/') || (temp =='-') || (temp=='*'))
- { node *t,*t1, *t2;
- t2 = treenodes.top(); treenodes.pop();
- t1 = treenodes.top(); treenodes.pop();
- t = makeNode(temp);
- t->right = t2;
- t->left = t1;
- treenodes.push(t);
- }
- }
- node *Tree = treenodes.top(); treenodes.pop();
- cout << "Would you like to evaluate the expression? (y/n)";
- cin >> ch;
- if (ch == 'y' || ch == 'Y')
- {
- int answer = evaluateTree(Tree);
- cout << endl << "Evaluation: " << answer << endl;
- }
- cout << endl;
- cout << "Would you like to convert another expression? (y/n)";
- cin >> again;
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement