Advertisement
Guest User

Untitled

a guest
Jul 28th, 2017
57
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 5.33 KB | None | 0 0
  1. #include <stdio.h>
  2. #include <stdlib.h>
  3. #include <string.h>
  4.  
  5. typedef struct s_TNode {
  6.     char Info;
  7.     struct s_TNode *Left;
  8.     struct s_TNode *Right;
  9. } t_TNode;
  10.  
  11. typedef struct s_Tree {
  12.     t_TNode *Root;
  13. } t_Tree;
  14.  
  15. typedef struct s_LNode {
  16.     char Info;
  17.     struct s_LNode *Next;
  18. } t_LNode;
  19.  
  20. typedef struct s_Stack {
  21.     t_LNode *Top;
  22. } t_Stack;
  23.  
  24. int Pop (t_Stack *Stack, char *Res)
  25. {
  26.     t_LNode *tmp;
  27.  
  28.     if (!Stack)
  29.     {
  30.         printf("\nNULL pointer to stack.");
  31.         return 0;
  32.     }
  33.     if (!Stack->Top)
  34.     {
  35.         printf("\nStack is empty.");
  36.         return 0;
  37.     }
  38.     *Res = (Stack->Top)->Info;
  39.     tmp = Stack->Top;
  40.     Stack->Top = tmp->Next;
  41.     free(tmp);
  42.     tmp = NULL;
  43.     return 1;
  44. }
  45.  
  46. int Push (t_Stack *Stack, char Arg)
  47. {
  48.     t_LNode *tmp;
  49.     t_LNode *New;
  50.    
  51.     if (!Stack)
  52.     {
  53.         printf("\nNULL pointer to stack.");
  54.         return 0;
  55.     }
  56.     tmp = Stack->Top;
  57.     if (!(New = (t_LNode *)malloc(sizeof(t_LNode))))
  58.     {
  59.         printf("\nCouldn't allocate memory for new stack element.");
  60.         return 0;
  61.     }
  62.     Stack->Top = New;
  63.     (Stack->Top)->Next = tmp;
  64.     (Stack->Top)->Info = Arg;
  65.     return 1;
  66. }
  67.  
  68. int Prior(char Top, char Arg)
  69. {
  70.     if (Top == '+' || Top == '-')
  71.     {
  72.         if (Arg == '+' || Arg == '-' || Arg == ')')
  73.         {
  74.             return 1;
  75.         }
  76.         else
  77.         {
  78.             return 0;
  79.         }
  80.     }
  81.     if (Top == '*' || Top == '/' || Top == '^')
  82.     {
  83.         if (Arg == '+' || Arg == '-' || Arg == '*' || Arg == '/' || Arg == '^' || Arg == ')')
  84.         {
  85.             return 1;
  86.         }
  87.         else
  88.         {
  89.             return 0;
  90.         }
  91.     }
  92.     if (Top == '(')
  93.     {
  94.         if (Arg == ')')
  95.         {
  96.             return 1;
  97.         }
  98.         else
  99.         {
  100.             return 0;
  101.         }
  102.     }
  103.     return 0;
  104. }
  105.  
  106. void ConvertToRPN(char Arg[], char Res[])
  107. {
  108.     int ArgL = strlen(Arg);
  109.     int j = 0;
  110.     int i = 0;
  111.     int PriorV = 0;
  112.     char Smb = '\0';
  113.     t_Stack Stack; Stack.Top = NULL;
  114.    
  115.     // пока во входной строке что-то есть
  116.     for (i = 0 ; i < ArgL ; ++i)
  117.     {
  118.         if (Stack.Top)
  119.         {
  120.             PriorV = Prior((Stack.Top)->Info, Arg[i]);
  121.         }
  122.         // если Arg[i] -- операнд, то выводим его в выходную строку
  123.         if (!(Arg[i] == '+' || Arg[i] == '-' || Arg[i] == '*' || Arg[i] == '/' || Arg[i] == '(' || Arg[i] == ')' || Arg[i] == '^'))
  124.         {
  125.             Res[j++] = Arg[i];
  126.         }
  127.         // иначе
  128.         else
  129.         //  пока (на стеке более приоритетная операция)
  130.         {
  131.             while (PriorV)
  132.             //  вытаскиваем smb со стека, smb != "(" и отправляем в вых строку
  133.             {
  134.                 Pop(&Stack, &Smb);
  135.                 if (Smb != '(')
  136.                 {
  137.                     Res[j++] = Smb;
  138.                 }
  139.                 if (Stack.Top)
  140.                 {
  141.                     PriorV = Prior((Stack.Top)->Info, Arg[i]);
  142.                 }
  143.                 else
  144.                 {
  145.                     PriorV = 0;
  146.                 }
  147.                
  148.             }
  149.             //  push(Arg[i])
  150.             if (Arg[i] != ')')
  151.             {
  152.                 Push(&Stack, Arg[i]);
  153.             }
  154.         }
  155.     }  
  156.     //а теперь выталкиваем все, что осталось в стеке
  157.     while (Stack.Top)
  158.     {
  159.         Pop(&Stack, &Smb);
  160.         if (Smb != '(')
  161.         {
  162.             Res[j++] = Smb;
  163.         }
  164.     }
  165.     return;
  166. }
  167.  
  168. void BuildTreeNode (t_TNode *Node, char RPN[])
  169. {
  170.     int Len = strlen(RPN) - 1;
  171.    
  172.     if (!Node)
  173.     {
  174.         printf("\nNULL pointer to tree node.)");
  175.         return;
  176.     }
  177.     //если передали пустую вершину, то засунем в нее последний символ строки
  178.     if (Node->Info == '\0')
  179.     {
  180.         Node->Info = RPN[Len];
  181.         RPN[Len] = '\0';
  182.         Len = strlen(RPN) - 1;
  183.     }
  184.     //начинаем с правого поддерева
  185.     if (!(Len + 1))
  186.     {
  187.         return;
  188.     }
  189.  
  190.     if (!(Node->Right = (t_TNode *) malloc(sizeof(t_TNode))))
  191.     {
  192.         printf("\nCouldn't allocate memory for right tree node.");
  193.         return;
  194.     }
  195.  
  196.     Node->Right->Info = RPN[Len];
  197.     Node->Right->Left = NULL;
  198.     Node->Right->Right = NULL;
  199.  
  200.     if (!(RPN[Len] == '+' || RPN[Len] == '-' || RPN[Len] == '*' || RPN[Len] == '/' || RPN[Len] == '(' || RPN[Len] == ')' || RPN[Len] == '^'))
  201.     {
  202.         RPN[Len] = '\0';
  203.     }
  204.     else
  205.     {
  206.         RPN[Len] = '\0';
  207.         BuildTreeNode(Node->Right, RPN);
  208.     }
  209.    
  210.     //продолжаем левым поддеревом
  211.     Len = strlen(RPN) - 1;
  212.    
  213.     if (!(Len + 1))
  214.     {
  215.         return;
  216.     }
  217.  
  218.     if (!(Node->Left = (t_TNode *) malloc(sizeof(t_TNode))))
  219.     {
  220.         printf("\nCouldn't allocate memory for left tree node.");
  221.         return;
  222.     }
  223.  
  224.     Node->Left->Info = RPN[Len];
  225.     Node->Left->Left = NULL;
  226.     Node->Left->Right = NULL;
  227.  
  228.     if (!(RPN[Len] == '+' || RPN[Len] == '-' || RPN[Len] == '*' || RPN[Len] == '/' || RPN[Len] == '(' || RPN[Len] == ')' || RPN[Len] == '^'))
  229.     {
  230.         RPN[Len] = '\0';
  231.     }
  232.     else
  233.     {
  234.         RPN[Len] = '\0';
  235.         BuildTreeNode(Node->Left, RPN);
  236.     }
  237.  
  238. }
  239.  
  240. void BuildTreeFromRPN (t_Tree *Tree, char RPN[])
  241. {
  242.     BuildTreeNode(Tree->Root, RPN);
  243. }
  244.  
  245. void DirectTreeTraversal(t_TNode *Root)
  246. {
  247.     printf("%c ",Root->Info);
  248.     if (Root->Left)
  249.     {
  250.         DirectTreeTraversal(Root->Left);
  251.     }
  252.     if (Root->Left)
  253.     {
  254.         DirectTreeTraversal(Root->Right);
  255.     }
  256. }
  257.  
  258. void InorderTreeTraversal (t_TNode *Root)
  259. {
  260.     if (Root->Left)
  261.     {
  262.         InorderTreeTraversal(Root->Left);
  263.     }
  264.     printf("%c ",Root->Info);
  265.     if (Root->Right)
  266.     {
  267.         InorderTreeTraversal(Root->Right);
  268.     }
  269. }
  270. void main ()
  271. {
  272.     char Exp[30] = {'\0'};
  273.     char Res[30] = {'\0'};
  274.     int i = 0;
  275.     int LRes;
  276.     char ch;
  277.     t_Tree Tree;
  278.  
  279.     printf("Enter Infix expression\n");
  280.     while (Exp[i++] = getchar())
  281.     {
  282.         if (Exp[i-1] == '\n')
  283.         {
  284.             Exp[i-1] = '\0';
  285.             break;
  286.         }
  287.     }
  288.     ConvertToRPN(Exp, Res);
  289.     LRes = strlen(Res);
  290.     for (i = 0 ; i < LRes ; ++i)
  291.     {
  292.         printf("%c", Res[i]);
  293.     }
  294.     printf("\n");
  295.     if (!((Tree.Root) = (t_TNode *) malloc(sizeof(t_TNode))))
  296.     {
  297.         printf("Couldn't allocate memory for tree.");
  298.         return;
  299.     }
  300.  
  301.     Tree.Root->Info = '\0';
  302.     BuildTreeFromRPN(&Tree, Res);
  303.     printf("\nDirect tree traversal: ");
  304.     DirectTreeTraversal(Tree.Root);
  305.     printf("\nInorder tree traversal: ");
  306.     InorderTreeTraversal(Tree.Root);
  307.     printf("\n");
  308. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement