Esempio n. 1
0
 public static void NodesToPolishNotation(AbstractSyntaxTree tree)
 {
     if (tree != null)
     {
         if (!(tree is AstOperator newTree))
         {
             var unOpTree = tree as AstOperand;
             if (unOpTree.Status == "arg")
             {
                 Postfix.Add(InfixToAstParser.ExpressionArgs[int.Parse(unOpTree.Value)]);
                 PolishNotation += InfixToAstParser.ExpressionArgs[int.Parse(unOpTree.Value)];
             }
             else
             {
                 Postfix.Add(unOpTree.Value);
                 PolishNotation += unOpTree.Value;
             }
         }
         else if (newTree.LeftChild != null && newTree.RightChild != null)
         {
             NodesToPolishNotation(newTree.LeftChild);
             NodesToPolishNotation(newTree.RightChild);
             Postfix.Add(newTree.Value);
             PolishNotation += newTree.Value;
         }
         else if (newTree.LeftChild != null)
         {
             NodesToPolishNotation(newTree.LeftChild);
             Postfix.Add(newTree.Value);
             PolishNotation += newTree.Value;
         }
     }
 public AstOperator(string value, AbstractSyntaxTree left, AbstractSyntaxTree right)
 {
     Value      = value;
     LeftChild  = left;
     RightChild = right;
     Status     = "Op";
 }
Esempio n. 3
0
        public AbstractSyntaxTree SecondPass(AbstractSyntaxTree tree)
        {
            Simulator.Postfix.Clear();
            Simulator.NodesToPolishNotation(tree);
            var           tokens = InfixToAstParser.ShuntingYardAlgorithm(InfixToAstParser.PostfixToInfix(Simulator.Postfix));
            List <string> result = new List <string>();

            foreach (var token in tokens)
            {
                if (Regex.IsMatch(token, @"cos|sin|tan|ctn|log|ex"))
                {
                    string operand = result[result.Count - 1];
                    if (!Regex.IsMatch(operand.ToString(), @"[0-9]+$") || operand == "cos" || operand == "sin" || operand == "tan" || operand == "ctn" || operand == "log" || operand == "ex")
                    {
                        result.Add(token);
                        continue;
                    }
                    result.RemoveAt(result.Count - 1);
                    switch (token)
                    {
                    case "sin":
                        result.Add(Convert.ToString(Math.Sin(Convert.ToDouble(operand))));
                        break;

                    case "cos":
                        result.Add(Convert.ToString(Math.Cos(Convert.ToDouble(operand))));
                        break;

                    case "ctn":
                        result.Add(Convert.ToString(1 / Math.Tan(Convert.ToDouble(operand))));
                        break;

                    case "tan":
                        result.Add(Convert.ToString(Math.Tan(Convert.ToDouble(operand))));
                        break;

                    case "log":
                        result.Add(Convert.ToString(Math.Log(Convert.ToDouble(operand))));
                        break;

                    case "ex":
                        result.Add(Convert.ToString(Math.Exp(Convert.ToDouble(operand))));
                        break;
                    }
                }
                const string pattern = @"[a-z]+|[0-9]+$";
                if (Regex.IsMatch(token.ToString(), pattern))
                {
                    result.Add(token.ToString());
                    continue;
                }
                string rightOperand = result[result.Count - 1];
                string leftOperand  = result[result.Count - 2];
                if (!Regex.IsMatch(rightOperand.ToString(), @"[0-9]+$") || !Regex.IsMatch(leftOperand.ToString(), @"[0-9]+$"))
                {
                    result.Add(token);
                    continue;
                }
                result.RemoveAt(result.Count - 1);
                result.RemoveAt(result.Count - 1);
                switch (token)
                {
                case "+":
                    result.Add(Convert.ToString(Convert.ToDouble(leftOperand) + Convert.ToDouble(rightOperand)));
                    break;

                case "-":
                    result.Add(Convert.ToString(Convert.ToDouble(leftOperand) - Convert.ToDouble(rightOperand)));
                    break;

                case "*":
                    result.Add(Convert.ToString(Convert.ToDouble(leftOperand) * Convert.ToDouble(rightOperand)));
                    break;

                case "/":
                    result.Add(Convert.ToString(Convert.ToDouble(leftOperand) / Convert.ToDouble(rightOperand)));
                    break;

                case "^":
                    result.Add(Convert.ToString(Math.Pow(Convert.ToDouble(leftOperand), Convert.ToDouble(rightOperand))));
                    break;
                }
            }
            return(InfixToAstParser.Parse(InfixToAstParser.PostfixToInfix(result)));
        }
 public AstOperator(string value, AbstractSyntaxTree operand)
 {
     Value     = value;
     LeftChild = operand;
     Status    = "Op";
 }
Esempio n. 5
0
 public List <string> ThirdPass(AbstractSyntaxTree tree)
 {
     Simulator.Postfix.Clear();
     Simulator.NodesToPolishNotation(tree);
     return(InfixToAstParser.PostfixToInfix(Simulator.Postfix).Select(x => Convert.ToString(x)).ToList());
 }