Advertisement
Guest User

eTreeFile

a guest
Nov 11th, 2019
133
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 3.25 KB | None | 0 0
  1.  
  2. #include<iostream>
  3. #include<string>
  4. #include <stack>
  5. using namespace std;
  6.  
  7. struct node{
  8.     char info;
  9.     node* left;
  10.     node* right;
  11. };
  12.  
  13.  
  14.  
  15. //Determines the priority of an operator
  16. int priority(char op){
  17. //  if ((op =='('))
  18.     //  return 0;
  19.     if ((op =='+') || (op =='-'))
  20.         return 1;
  21.     else if ((op =='/') || (op =='*'))
  22.             return 2;
  23.     else return -1;
  24. }
  25.  
  26. //Places a char from the input stack into a new treenode
  27. node* makeNode(char info){
  28.     node* childnode;
  29.     childnode = new node;
  30.     childnode->info = info;
  31.     childnode->left = NULL;
  32.     childnode->right = NULL;
  33.     return childnode;
  34. }
  35.  
  36.  
  37. //Using a recursive function, the value of the expression is Calculated
  38. int evaluateTree(node* treenode){
  39.     int x,y,z;
  40.     if ((treenode->info) == '+'||(treenode->info) == '-'||(treenode->info) == '*'||(treenode->info) == '/') {
  41.         x = evaluateTree(treenode->left);
  42.         y = evaluateTree(treenode->right);
  43.         if (treenode->info=='+')
  44.             z=x+y;
  45.         else if (treenode->info=='-')
  46.             z=x-y;
  47.         else if (treenode->info=='*')
  48.             z=x*y;
  49.         else if (treenode->info=='/')
  50.             z=x/y;
  51.         return z;
  52.     }
  53.     else return treenode->info - '0';  
  54. }
  55. bool isdigit(char temp)
  56. {
  57.     return temp <'9' && temp >'0';
  58. }
  59.  
  60.  
  61. void infixTopostfix(string infix, string &postfix)
  62. { int j = 0;
  63.   stack <char> charStack;
  64.   char temp;
  65.     for(int i = 0; i < infix.length(); i++)
  66.     {   temp = infix[i];
  67.         if (isdigit(temp))
  68.                 postfix += temp;
  69.         else if (temp == '(')
  70.                charStack.push(temp);
  71.         else if (temp == '+'||temp == '-'||temp == '*'||temp == '/')
  72.                     { while( !charStack.empty() && (priority(temp) <= priority(charStack.top())))
  73.                                 { char c = charStack.top();
  74.                                     charStack.pop();
  75.                                     postfix += c;
  76.                                  }  
  77.                                 charStack.push(temp);
  78.                     }
  79.                
  80.             else if (temp == ')')
  81.                     { while(! charStack.empty() && charStack.top() != '(')
  82.                                 { char c = charStack.top();
  83.                                     charStack.pop();
  84.                                     postfix += c;
  85.                                  }
  86.                         charStack.pop();
  87.                     }
  88.     }
  89.     while( ! charStack.empty() )
  90.         { char c = charStack.top();
  91.             charStack.pop();
  92.             postfix += c;
  93.          }
  94. }
  95.  
  96. int main(){
  97.     stack<node*> treenodes;  // stack for output node pointer addresses
  98.     char temp, ch, again=' ';
  99.     string infix, postfix; // the infix expression read from the line
  100.     while (again != 'n')
  101.     {   postfix=""; // the infix expression read from the line
  102.         cout << "Please enter an Infix Expression" << endl;
  103.         cin >> infix;
  104.         infixTopostfix(infix, postfix);
  105.         for(int i =0; i<postfix.length();i++)
  106.         {   temp=postfix[i];
  107.             //If it is operand, make it into a node, add push it
  108.             if (isdigit(temp))  
  109.                 treenodes.push(makeNode(temp));
  110.             if ((temp=='+') || (temp=='/') || (temp =='-') || (temp=='*'))
  111.                { node *t,*t1, *t2;
  112.                  t2 = treenodes.top(); treenodes.pop();
  113.                  t1 = treenodes.top(); treenodes.pop();
  114.                  t = makeNode(temp);
  115.                  t->right = t2;
  116.                  t->left = t1;
  117.                  treenodes.push(t);
  118.                 }  
  119.         }
  120.    
  121.         node *Tree  = treenodes.top(); treenodes.pop();
  122.  
  123.         cout << "Would  you like to evaluate the expression? (y/n)";
  124.         cin >> ch;
  125.         if (ch == 'y' || ch == 'Y')
  126.         {
  127.             int answer = evaluateTree(Tree);
  128.             cout << endl << "Evaluation: " << answer << endl;
  129.         }
  130.        
  131.         cout << endl;
  132.         cout << "Would  you like to convert another expression? (y/n)";
  133.         cin >> again;
  134.  
  135.     }
  136. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement