Advertisement
Proff_Ust

tree_formula_simplification

Dec 8th, 2019
149
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 7.19 KB | None | 0 0
  1. #include <iostream>
  2. using namespace std;
  3.  
  4. struct TTree
  5. {
  6.     char inf;
  7.     TTree* left;
  8.     TTree* right;
  9. };
  10.  
  11. struct TStack
  12. {
  13.     TTree* inf;
  14.     TStack* next;
  15. };
  16.  
  17. bool isEmpty(TStack* s)
  18. {
  19.     return s==NULL;
  20. }
  21.  
  22. void push(TStack* &s, TTree* newValue)
  23. {
  24.     if(isEmpty(s))
  25.     {
  26.         TStack* newElem = new TStack;
  27.         newElem->inf = newValue;
  28.         newElem->next = s;
  29.         s = newElem;
  30.     }
  31.     else
  32.         if(s->inf!=newValue)
  33.         {
  34.             TStack* newElem = new TStack;
  35.             newElem->inf = newValue;
  36.             newElem->next = s;
  37.             s = newElem;
  38.         }
  39. }
  40.  
  41. TTree* pop(TStack* &s)
  42. {
  43.     TTree* retValue;
  44.     TStack* prev;
  45.     if(!isEmpty(s))
  46.     {
  47.         retValue = s->inf;
  48.         prev = s;
  49.         s = s->next;
  50.         delete prev;
  51.         return retValue;
  52.     }
  53.     else
  54.         return NULL;
  55. }
  56.  
  57. bool znak(char c)
  58. {
  59.     return ((c=='-')||(c=='+')||(c=='*')||(c=='/'));
  60. }
  61.  
  62. int priority(char c)
  63. {
  64.     switch(c)
  65.     {
  66.         case '!':
  67.             return 4;
  68.         case '*':
  69.         case '/':
  70.         case '%':
  71.             return 3;
  72.         case '+':
  73.         case '-':
  74.             return 2;
  75.         case '=':
  76.             return 1;
  77.     }
  78.     return 0;
  79. }
  80.  
  81. bool opLeftAssoc(char c)
  82. {
  83.     return c!='!';
  84. }
  85.  
  86. void createTreeFromInfix(TTree* &root, string str)
  87. {
  88.     TStack* stack1 = NULL;
  89.     TStack* stack2 = NULL;
  90.     for(unsigned int i=0;i<str.size();i++)
  91.     {
  92.         TTree* PRoot  = new TTree;
  93.         TTree* PRoot1 = new TTree;
  94.         if(isalnum(str[i]))
  95.         {
  96.             PRoot->inf = str[i];
  97.             PRoot->left = NULL;
  98.             PRoot->right = NULL;
  99.             push(stack1, PRoot);
  100.         }
  101.         if(str[i]=='(')
  102.         {
  103.             PRoot->inf = str[i];
  104.             PRoot->left = NULL;
  105.             PRoot->right = NULL;
  106.             push(stack2, PRoot);
  107.         }
  108.         if(znak(str[i]))
  109.         {
  110.             while(!isEmpty(stack2))
  111.             {
  112.                 TTree* PRoot8 = new TTree;
  113.                 if(((opLeftAssoc(str[i]))&&(priority(str[i]))<=priority(stack2->inf->inf)))
  114.                 {
  115.                     PRoot8->inf   = pop(stack2)->inf;
  116.                     PRoot8->right = pop(stack1);
  117.                     PRoot8->left  = pop(stack1);
  118.                     push(stack1, PRoot8);
  119.                 }else
  120.                     break;
  121.             }
  122.             PRoot1->inf = str[i];
  123.             PRoot1->left = NULL;
  124.             PRoot1->right = NULL;
  125.             push(stack2, PRoot1);
  126.         }
  127.         if(str[i]==')')
  128.         {
  129.             while(stack2->inf->inf!='(')
  130.             {
  131.                 TTree* PRoot5 = new TTree;
  132.                 PRoot5->inf   = pop(stack2)->inf;
  133.                 PRoot5->right = pop(stack1);
  134.                 PRoot5->left  = pop(stack1);
  135.                 push(stack1, PRoot5);
  136.             }
  137.             pop(stack2);
  138.         }
  139.     }
  140.     while(!isEmpty(stack2))
  141.     {
  142.         TTree* PRoot3 = new TTree;
  143.         PRoot3->inf   = pop(stack2)->inf;
  144.         PRoot3->right = pop(stack1);
  145.         PRoot3->left  = pop(stack1);
  146.         push(stack1, PRoot3);
  147.     }
  148.     root = pop(stack1);
  149. }
  150.  
  151. void printTreeGraph(TTree* root, int level)
  152. {
  153.     if(root)
  154.     {
  155.         printTreeGraph(root->right, level+2);
  156.         for(int i=0;i<level;i++)
  157.             cout<<" ";
  158.         cout<<root->inf<<endl;
  159.         printTreeGraph(root->left, level+2);
  160.     }
  161. }
  162.  
  163. void printTree(TTree* root)
  164. {
  165.     if(root)
  166.     {
  167.         printTree(root->right);
  168.         printTree(root->left);
  169.         cout<<root->inf<<" ";
  170.     }
  171. }
  172.  
  173.  
  174. void Task(TTree* &root)
  175. {
  176.     if(root)//если жерево непусто
  177.         if((root->left)&&(root->right)) //и у него есть потомки
  178.         {
  179.             if(isalnum(root->left->inf)&&(isalnum(root->right->inf)))//проверяем что оба потомка операнды
  180.             {
  181.                 if((root->left->inf=='0')||(root->right->inf=='0'))//если один из потомков 0
  182.                 {
  183.                     if((root->inf=='-')||(root->inf=='+'))//и оператор + или -
  184.                     {
  185.                         if(root->left->inf=='0')//а зависимости от того с какой стороны 0
  186.                         {
  187.                             root->inf = root->right->inf;//берем ненулевое значение и кладем его вместо оператора
  188.                             delete root->right;//чистим за собой память
  189.                             delete root->left;
  190.                             root->right = NULL;//присваиваем потомка NULL
  191.                             root->left = NULL;
  192.                         }else
  193.                         {
  194.                             root->inf = root->left->inf;
  195.                             delete root->right;
  196.                             delete root->left;
  197.                             root->right = NULL;
  198.                             root->left = NULL;
  199.                         }
  200.  
  201.                     }else
  202.                         if(root->inf=='*')
  203.                         {
  204.                             root->inf = '0';
  205.                             delete root->left;
  206.                             delete root->right;
  207.                             root->right = NULL;
  208.                             root->left = NULL;
  209.                         }
  210.                 }else
  211.                     if((root->left->inf=='1')||(root->right->inf=='1'))
  212.                         if(root->inf=='*')
  213.                         {
  214.                             if(root->left->inf=='1')
  215.                             {
  216.                                 root->inf = root->right->inf;
  217.                                 delete root->left;
  218.                                 delete root->right;
  219.                                 root->left = NULL;
  220.                                 root->right = NULL;
  221.                             }else
  222.                             {
  223.                                 root->inf = root->left->inf;
  224.                                 delete root->left;
  225.                                 delete root->right;
  226.                                 root->left = NULL;
  227.                                 root->right = NULL;
  228.                             }
  229.                         }
  230.             }
  231.             Task(root->left);
  232.             Task(root->right);
  233.         }
  234. }
  235.  
  236. int calculate(TTree* root)
  237. {
  238.     if((root->left)&&(root->right))
  239.     {
  240.         if(root->inf=='+')
  241.             return calculate(root->left)+calculate(root->right);
  242.         if(root->inf=='-')
  243.             return calculate(root->left)-calculate(root->right);
  244.         if(root->inf=='*')
  245.             return calculate(root->left)*calculate(root->right);
  246.         if(root->inf=='/')
  247.             return calculate(root->left)/calculate(root->right);
  248.     }
  249.     return root->inf - '0';
  250. }
  251.  
  252. int main()
  253. {
  254.     setlocale(0,"Russian");
  255.     string s;
  256.     cin>>s;
  257.     TTree* tree = NULL;
  258.     createTreeFromInfix(tree, s);
  259.     printTreeGraph(tree,1);
  260.     cout<<endl<<endl;
  261.     printTree(tree);
  262.     cout<<endl<<calculate(tree)<<endl;
  263.     Task(tree);
  264.     printTreeGraph(tree,1);
  265.     printTree(tree);
  266.     return 0;
  267. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement