Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- using System;
- using System.Collections.Generic;
- using System.Linq;
- using System.Text;
- using System.Threading.Tasks;
- namespace GeneratForUnity
- {
- class Tree
- {
- private TreeNode Root;
- 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 TreeNode CreateTree(int[] Mas,int result)
- {
- TreeNode root=new TreeNode();
- Random rnd = new Random();
- if (FindNotZero(Mas))
- {
- bool res = false;
- int i = 0;
- int kof = 1;
- int element;
- while (res!=true)
- {
- i = rnd.Next(0, 4);
- if (Mas[i] != 0) res = true;
- }
- switch (i)
- {
- case 0:
- Mas[i]--;
- root.Sign = '+';
- element = rnd.Next(0, result+1);
- root.LeftNode = new TreeNode(element);
- root.RightNode = new TreeNode(result - element);
- if (FindNotZero(Mas)) root.LeftNode.LeftNode = CreateTree(Mas, root.LeftNode.Figure);
- break;
- case 1:
- Mas[i]--;
- root.Sign = '-';
- element = rnd.Next(result-1,result + result/100*20);
- root.LeftNode = new TreeNode(element);
- root.RightNode = new TreeNode(element - result);
- if (FindNotZero(Mas)) root.LeftNode.LeftNode = CreateTree(Mas, root.LeftNode.Figure);
- break;
- case 2:
- Mas[i]--;
- root.Sign = '/';
- kof = 1;
- element = rnd.Next(result, result + 200);
- while ((element % kof != 0) || (element / kof != result))
- {
- if (kof > element)
- {
- element = rnd.Next(result + 10, result + 200);
- kof = 1;
- }
- kof++;
- }
- root.LeftNode = new TreeNode(element);
- root.RightNode = new TreeNode(kof);
- if (FindNotZero(Mas)) root.LeftNode.LeftNode = CreateTree(Mas, root.LeftNode.Figure);
- break;
- case 3:
- Mas[i]--;
- root.Sign = '*';
- 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);
- }
- else
- {
- root.LeftNode = new TreeNode(element);
- root.RightNode = new TreeNode(kof);
- }
- if (FindNotZero(Mas)) root.LeftNode.LeftNode = CreateTree(Mas, root.LeftNode.Figure);
- break;
- }
- }
- else root = null;
- return root;
- }
- #endregion
- public int Calculation(string str)
- {
- int result = 0;
- StringBuilder strInt = new StringBuilder("ds");
- for (var i=0;i<str.Length;i++)
- {
- }
- return result;
- }
- private string ToString(TreeNode root)
- {
- StringBuilder str = new StringBuilder("");
- 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)) ;
- else str.Insert(str.Length, root.Figure);
- }
- if (root.RightNode!=null) str.Insert(str.Length, ToString(root.RightNode)+")");
- return Convert.ToString(str);
- }
- public string Generate(int pCount, int sCount, int dCount, int mCount, int result)
- {
- Root.Figure = result;
- int[] Mas = new int[4] { pCount, sCount, dCount, mCount };
- Root.LeftNode=CreateTree(Mas,result);
- string str = ToString(Root.LeftNode);
- return str;
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement