Advertisement
Guest User

Untitled

a guest
Nov 24th, 2017
70
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 7.80 KB | None | 0 0
  1.  class Tree
  2.     {
  3.         private TreeNode Root;
  4.        
  5.         public void Destroy()
  6.         {
  7.             Root = new TreeNode();
  8.         }
  9.  
  10.         public Tree()
  11.         {
  12.             Root = new TreeNode();
  13.         }
  14.  
  15.         #region CreateTree(int[],int)
  16.  
  17.         private bool FindNotZero(int[] Mas)
  18.         {
  19.             bool result= false;
  20.             int summa=0;
  21.             for (var i = 0; i < Mas.Length; i++) summa += Mas[i];
  22.             if (summa > 0) result = true;
  23.             return result;
  24.         }
  25.  
  26.  
  27.         public void CreateTree(int[] Mas,int result)
  28.         {
  29.             TreeNode root=Root;
  30.             Random rnd = new Random();
  31.             bool res = false;
  32.             int i = 0;
  33.             int kof = 1;
  34.             int element;
  35.             int Result=result/2;
  36.             if (result % 2 != 0) Result++;
  37.             if ((Mas[2] == 0) && (Mas[3] == 0))
  38.             {
  39.                 Result = result;
  40.             }
  41.             if ((Mas[0] == 0) && (Mas[1] == 0))
  42.             {
  43.                 Result = result;
  44.             }
  45.             while (FindNotZero(Mas))
  46.             {
  47.                 res = false;
  48.  
  49.                 while (res!=true)
  50.                 {
  51.                     if ((Mas[2] != 0) || (Mas[3] != 0)) i = rnd.Next(2, 4);
  52.                     else i = rnd.Next(0, 2);
  53.                     if (Mas[i] != 0) res = true;
  54.                 }
  55.  
  56.                 switch (i)
  57.                 {
  58.                     case 0:
  59.                         Mas[i]--;
  60.  
  61.                         if (Root.RightNode == null)
  62.                         {
  63.                             if(Root.LeftNode!=null)Result = result / 2;
  64.                             Root.RightNode = new TreeNode(Result);
  65.                         }
  66.  
  67.                         root = Root.RightNode;
  68.                         while (root.LeftNode != null) root = root.LeftNode;
  69.                         root.LeftNode = new TreeNode('+');
  70.                         root = root.LeftNode;
  71.                         if(Result+1>0)element = rnd.Next(0, Result+1);
  72.                         else element = rnd.Next( Result + 1,0);
  73.                         root.LeftNode = new TreeNode(element);
  74.                         root.RightNode = new TreeNode(Result - element);
  75.                         //if ((Mas[i] == 0)&&(root.LeftNode.Figure>=0)) root.LeftNode.Sign = 'm';
  76.                         Result = element;
  77.                         break;
  78.  
  79.                     case 1:
  80.                         Mas[i]--;
  81.  
  82.                         if (Root.RightNode == null)
  83.                         {
  84.                             if (Root.LeftNode!= null) Result = result / 2;
  85.                             Root.RightNode = new TreeNode(Result);
  86.                         }
  87.  
  88.                         root = Root.RightNode;
  89.                         while (root.LeftNode != null) root = root.LeftNode;
  90.  
  91.                         root.LeftNode = new TreeNode('-');
  92.                         root = root.LeftNode;
  93.                        
  94.                         element = rnd.Next(Result - 1, Result + Result / 100 * 20);
  95.                         root.LeftNode = new TreeNode(element);
  96.                         root.RightNode = new TreeNode(element - Result);
  97.                         //if ((Mas[i] == 0) && (root.LeftNode.Figure >= 0)) root.LeftNode.Sign = 'm';
  98.                         Result = element;
  99.                         break;
  100.  
  101.                     case 2:
  102.                         Mas[i]--;
  103.  
  104.                         root = Root;
  105.                         while (root.LeftNode != null) root=root.LeftNode;
  106.                         root.LeftNode = new TreeNode('/');
  107.                         root = root.LeftNode;
  108.  
  109.                         kof = 1;
  110.                         element = rnd.Next(Result, Result +Result+200);
  111.  
  112.                         while ((element % kof != 0) || (element / kof != Result))
  113.                         {
  114.                             if ((kof > element)||(element/kof<Result))
  115.                             {
  116.                                 element = rnd.Next(Result, Result + Result+200);
  117.                                 kof = 1;
  118.                             }
  119.                             kof++;
  120.                         }
  121.  
  122.                        
  123.                          root.LeftNode = new TreeNode(element);
  124.                          root.RightNode = new TreeNode(kof);
  125.                        
  126.                          Result = element;
  127.  
  128.  
  129.                         break;
  130.  
  131.                     case 3:
  132.                         Mas[i]--;
  133.  
  134.                         root = Root;
  135.                         while (root.LeftNode != null) root = root.LeftNode;
  136.                         root.LeftNode = new TreeNode('*');
  137.                         root = root.LeftNode;
  138.  
  139.                        
  140.                         kof = 1;
  141.                         element = rnd.Next(1, Result+1);
  142.  
  143.                         while((element*kof!=Result))
  144.                         {
  145.                             kof++;
  146.                             if (kof * element > Result)
  147.                             {
  148.                                 element = rnd.Next(1, Result+1);
  149.                                 kof = 1;
  150.                             }
  151.                         }
  152.  
  153.                         if (kof>element)
  154.                         {
  155.                             root.LeftNode = new TreeNode(kof);
  156.                             root.RightNode = new TreeNode(element);
  157.                             Result = kof;
  158.                         }
  159.                         else
  160.                         {
  161.                             root.LeftNode = new TreeNode(element);
  162.                             root.RightNode = new TreeNode(kof);
  163.                             Result = element;
  164.                         }
  165.  
  166.                         break;
  167.                 }
  168.  
  169.             }
  170.         }
  171.  
  172.         #endregion
  173.  
  174.         #region Строка
  175.  
  176.  
  177.         private string RowProcess(StringBuilder str)
  178.         {
  179.             var i=0;
  180.             while (i<str.Length)
  181.             {
  182.                 if (((int)str[i] == 0) && (i + 1 < str.Length) && (i != 0))
  183.                 {
  184.                     if (str[i + 1] != '-') str[i] = '+';
  185.                     else str.Remove(i, 1);
  186.                 }
  187.                 else
  188.                 if ((str[i] == '-') && (str[i + 1] == '-'))
  189.                 {
  190.                     str[i] = '+';
  191.                     str.Remove(i + 1, 1);
  192.                 }
  193.                 else if (str[i] == '+' && str[i + 1] == '-') str.Remove(i, 1);
  194.                 else if (str[i] == '-' && str[i + 1] == '+') str.Remove(i+1, 1);
  195.  
  196.  
  197.                 i++;
  198.             }
  199.             return Convert.ToString(str);
  200.         }
  201.  
  202.         private StringBuilder ToString(TreeNode root)
  203.         {
  204.             StringBuilder str = new StringBuilder("");
  205.             if (root != null)
  206.             {
  207.                 if (root.LeftNode != null) str.Insert(str.Length, ToString(root.LeftNode));
  208.  
  209.                 if (root.Sign != 'e') str.Insert(str.Length, root.Sign);
  210.                 else
  211.                 {
  212.                     if (((root.LeftNode != null) && (root.RightNode == null)) || ((root.LeftNode == null) && (root.RightNode != null))) ;
  213.                     else str.Insert(str.Length, root.Figure);
  214.                 }
  215.  
  216.                 if (root.RightNode != null) str.Insert(str.Length, ToString(root.RightNode));
  217.             }
  218.             return str;
  219.         }
  220.  
  221.         #endregion
  222.  
  223.         public string Generate(int pCount, int sCount, int dCount, int mCount, int result)
  224.         {
  225.             Destroy();
  226.             Root.Figure = result;
  227.             int[] Mas = new int[4] { pCount, sCount, dCount, mCount };
  228.             CreateTree(Mas,result);
  229.             string str = RowProcess(ToString(Root));
  230.            // Console.WriteLine((int)str[4]);
  231.             return str;
  232.         }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement