예제 #1
0
파일: TreeTest.cs 프로젝트: Gosed/HomeWorks
 public void SumTest()
 {
     Summ sum = new Summ();
     sum.Number = -1;
     sum.Operation = '+';
     NumberClass num1 = new NumberClass();
     num1.Number = 8;
     num1.Operation = 'x';
     num1.LeftBranch = null;
     num1.RightBranch = null;
     NumberClass num2 = new NumberClass();
     num2.Number = 7;
     num2.Operation = 'x';
     num2.LeftBranch = null;
     num2.RightBranch = null;
     sum.LeftBranch = num1;
     sum.RightBranch = num2;
     Assert.AreEqual(15, sum.CountBranches());
 }
예제 #2
0
        /// <summary>
        /// Building of a tree
        /// </summary>
        /// <param name="i">Index to build(0 - default)</param>
        /// <param name="str">String to read</param>
        public void Build(ref int i, string str)
        {
            TreeClass tmp = new Summ();
            while ((i < str.Length) && ((str[i] == ' ') || (str[i] == ')')))
                i++;
            if ((str[i] == '+') || (str[i] == '-') || (str[i] == '/') || (str[i] == '*'))
            {
                tmp = OperationType(str[i]);
                tmp.Operation = str[i];
                tmp.Number = -1;
                i++;
            }
            if ((i < str.Length) && (str[i] == '('))
            {
                CountTree LTree = new CountTree();
                i++;
                LTree.Build(ref i, str);
                CountTree RTree = new CountTree();
                i++;
                RTree.Build(ref i, str);
                tmp.LeftBranch = LTree.head;
                tmp.RightBranch = RTree.head;
            }
            int j = i;
            int number = 0;
            while ((j < str.Length) && ((int)str[j] - (int)'0') < 10 && ((int)str[j] - (int)'0') >= 0)
            {

                number = (number * 10) + (int)str[j] - (int)'0';
                j++;
            }
            if (number != 0)
            {
                tmp = OperationType(str[i]);
                i = j;
                tmp.Number = number;
                tmp.Operation = 'x';
            }
            this.head = tmp;
        }