Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- class Tree
- {
- private TreeNode Root;
- public void Destroy()
- {
- Root = new TreeNode();
- }
- public Tree()
- {
- Root = new TreeNode();
- }
- #region CreateTree(int[],int)
- private bool FindNotZero(int[] Mas)
- {
- bool result= false;
- int summa=0;
- for (var i = 0; i < Mas.Length; i++) summa += Mas[i];
- if (summa > 0) result = true;
- return result;
- }
- public void CreateTree(int[] Mas,int result)
- {
- TreeNode root=Root;
- Random rnd = new Random();
- bool res = false;
- int i = 0;
- int kof = 1;
- int element;
- int Result=result/2;
- if (result % 2 != 0) Result++;
- if ((Mas[2] == 0) && (Mas[3] == 0))
- {
- Result = result;
- }
- if ((Mas[0] == 0) && (Mas[1] == 0))
- {
- Result = result;
- }
- while (FindNotZero(Mas))
- {
- res = false;
- while (res!=true)
- {
- if ((Mas[2] != 0) || (Mas[3] != 0)) i = rnd.Next(2, 4);
- else i = rnd.Next(0, 2);
- if (Mas[i] != 0) res = true;
- }
- switch (i)
- {
- case 0:
- Mas[i]--;
- if (Root.RightNode == null)
- {
- if(Root.LeftNode!=null)Result = result / 2;
- Root.RightNode = new TreeNode(Result);
- }
- root = Root.RightNode;
- while (root.LeftNode != null) root = root.LeftNode;
- root.LeftNode = new TreeNode('+');
- root = root.LeftNode;
- if(Result+1>0)element = rnd.Next(0, Result+1);
- else element = rnd.Next( Result + 1,0);
- root.LeftNode = new TreeNode(element);
- root.RightNode = new TreeNode(Result - element);
- //if ((Mas[i] == 0)&&(root.LeftNode.Figure>=0)) root.LeftNode.Sign = 'm';
- Result = element;
- break;
- case 1:
- Mas[i]--;
- if (Root.RightNode == null)
- {
- if (Root.LeftNode!= null) Result = result / 2;
- Root.RightNode = new TreeNode(Result);
- }
- root = Root.RightNode;
- while (root.LeftNode != null) root = root.LeftNode;
- root.LeftNode = new TreeNode('-');
- root = root.LeftNode;
- element = rnd.Next(Result - 1, Result + Result / 100 * 20);
- root.LeftNode = new TreeNode(element);
- root.RightNode = new TreeNode(element - Result);
- //if ((Mas[i] == 0) && (root.LeftNode.Figure >= 0)) root.LeftNode.Sign = 'm';
- Result = element;
- break;
- case 2:
- Mas[i]--;
- root = Root;
- while (root.LeftNode != null) root=root.LeftNode;
- root.LeftNode = new TreeNode('/');
- root = root.LeftNode;
- kof = 1;
- element = rnd.Next(Result, Result +Result+200);
- while ((element % kof != 0) || (element / kof != Result))
- {
- if ((kof > element)||(element/kof<Result))
- {
- element = rnd.Next(Result, Result + Result+200);
- kof = 1;
- }
- kof++;
- }
- root.LeftNode = new TreeNode(element);
- root.RightNode = new TreeNode(kof);
- Result = element;
- break;
- case 3:
- Mas[i]--;
- root = Root;
- while (root.LeftNode != null) root = root.LeftNode;
- root.LeftNode = new TreeNode('*');
- root = root.LeftNode;
- kof = 1;
- element = rnd.Next(1, Result+1);
- while((element*kof!=Result))
- {
- kof++;
- if (kof * element > Result)
- {
- element = rnd.Next(1, Result+1);
- kof = 1;
- }
- }
- if (kof>element)
- {
- root.LeftNode = new TreeNode(kof);
- root.RightNode = new TreeNode(element);
- Result = kof;
- }
- else
- {
- root.LeftNode = new TreeNode(element);
- root.RightNode = new TreeNode(kof);
- Result = element;
- }
- break;
- }
- }
- }
- #endregion
- #region Строка
- private string RowProcess(StringBuilder str)
- {
- var i=0;
- while (i<str.Length)
- {
- if (((int)str[i] == 0) && (i + 1 < str.Length) && (i != 0))
- {
- if (str[i + 1] != '-') str[i] = '+';
- else str.Remove(i, 1);
- }
- else
- if ((str[i] == '-') && (str[i + 1] == '-'))
- {
- str[i] = '+';
- str.Remove(i + 1, 1);
- }
- else if (str[i] == '+' && str[i + 1] == '-') str.Remove(i, 1);
- else if (str[i] == '-' && str[i + 1] == '+') str.Remove(i+1, 1);
- i++;
- }
- return Convert.ToString(str);
- }
- private StringBuilder ToString(TreeNode root)
- {
- StringBuilder str = new StringBuilder("");
- if (root != null)
- {
- if (root.LeftNode != null) str.Insert(str.Length, ToString(root.LeftNode));
- if (root.Sign != 'e') str.Insert(str.Length, root.Sign);
- else
- {
- if (((root.LeftNode != null) && (root.RightNode == null)) || ((root.LeftNode == null) && (root.RightNode != null))) ;
- else str.Insert(str.Length, root.Figure);
- }
- if (root.RightNode != null) str.Insert(str.Length, ToString(root.RightNode));
- }
- return str;
- }
- #endregion
- public string Generate(int pCount, int sCount, int dCount, int mCount, int result)
- {
- Destroy();
- Root.Figure = result;
- int[] Mas = new int[4] { pCount, sCount, dCount, mCount };
- CreateTree(Mas,result);
- string str = RowProcess(ToString(Root));
- // Console.WriteLine((int)str[4]);
- return str;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement