예제 #1
0
        public void Recyrse(string input, out Node D)
        {
            D = null;
            if (input.Length == 2)
            {
                if (TestForSymbol(input.Substring(0, 1)) == 0)
                {
                    D = new NodeConst(Int32.Parse(input.Substring(1, 1)));
                }
                else
                {
                    D = new NodeConst(Int32.Parse(input.Substring(0, 1)));
                }
                //Console.Write(N + " ");
                //Console.WriteLine(D.Value);
            }
            else if (TestForSymbol(input) == 3)
            {
                D = new NodeConst(Int32.Parse(input));
                //Console.Write(N + " ");
                //Console.WriteLine(input);
            }
            else if ((TestForSymbol(input.Substring(0, 1)) == 0) && (input.Length >= 5))
            {
                string s1 = "";
                string s2 = input;
                s2 = s2.Substring(1);
                s2 = s2.Substring(0, s2.Length - 1);
                string ch = "";
                Node   D1, D2;
                s1 = StringSplit(ref s2, out ch);
                Recyrse(s1, out D1);
                Recyrse(s2, out D2);
                Operation t = Operation.NONE;;
                if (ch == "+")
                {
                    t = Operation.PLUS;
                }
                if (ch == "-")
                {
                    t = Operation.MINUS;
                }
                if (ch == "*")
                {
                    t = Operation.MULTIPLY;
                }
                //ch to operation

                D = new NodeOperation(t, D1, D2);
                //Console.Write(N + " ");
                //Console.WriteLine(ch);
                //Console.WriteLine(ch);
            }
        }
예제 #2
0
 public void Expression(ref string s, out Node D)  // выражение
 {
     D = null;
     s = s.Trim();
     if (s.Length != 0)
     {
         if (Test(s[0], '0', '1', '2', '3', '4', '5', '6', '7', '8', '9', ',', '+', '-'))
         {
             D = new NodeConst(Int32.Parse(Pop(ref s, 1)));
         }
         else if (Peek(ref s) == '(')
         {
             s = s.Substring(1);
             s = s.Substring(0, s.Length - 1);
             string s1 = SignSplit(ref s);
             string sign = Pop(ref s, 1);
             Node   DL, DR = null;
             Expression(ref s1, out DL);
             Expression(ref s, out DR);
             Operation op = Operation.NONE;
             if (sign == "+")
             {
                 op = Operation.PLUS;
             }
             else if (sign == "-")
             {
                 op = Operation.MINUS;
             }
             else if (sign == "/")
             {
                 op = Operation.DIVIDE;
             }
             else if (sign == "*")
             {
                 op = Operation.MULTIPLY;
             }
             D = new NodeOperation(op, DL, DR);
         }
     }
     else
     {
         D = null; error = 4; // ожидается выражение
     }
 }  // Expression