Exemplo n.º 1
0
 /// <summary>
 /// Main part of a programm
 /// </summary>
 /// <param name="args"></param>
 static void Main(string[] args)
 {
     CountTree tree = new CountTree();
     StreamReader streamReader = new StreamReader("input.txt");
     string str = "";
     while (!streamReader.EndOfStream)
     {
         str += streamReader.ReadLine();
     }
     int tmp = 0;
     tree.Build(ref tmp, str);
     Console.WriteLine(tree.CountThisTree());
     tree.PrintThisTree();
     Console.ReadKey();
 }
Exemplo n.º 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;
        }