Exemplo n.º 1
0
 protected int Precedence(Symbol sym)
 {
     switch (sym.m_type)
     {
         case Type.Bracket:
             return 5;
         case Type.Function:
             return 4;
         case Type.Comma:
             return 0;
     }
     switch (sym.m_name)
     {
         case "^":
             return 3;
         case "/":
         case "*":
         case "%":
             return 2;
         case "+":
         case "-":
             return 1;
     }
     return -1;
 }
Exemplo n.º 2
0
        protected Symbol Evaluate(Symbol sym1, Symbol opr, Symbol sym2)
        {
            Symbol result = new Symbol();
            result.m_name = sym1.m_name + opr.m_name + sym2.m_name;
            result.m_type = Type.Result;
            result.m_value = 0;
            switch (opr.m_name)
            {
                case "^":
                    result.m_value = System.Math.Pow(sym1.m_value, sym2.m_value);
                    break;
                case "/":
                    {
                        if (sym2.m_value > 0)
                            result.m_value = sym1.m_value / sym2.m_value;
                        else
                        {
                            result.m_name = "Divide by Zero.";
                            result.m_type = Type.Error;
                        }
                        break;
                    }
                case "*":
                    result.m_value = sym1.m_value * sym2.m_value;
                    break;
                case "%":
                    result.m_value = sym1.m_value % sym2.m_value;
                    break;
                case "+":
                    result.m_value = sym1.m_value + sym2.m_value;
                    break;
                case "-":
                    result.m_value = sym1.m_value - sym2.m_value;
                    break;
                case "&":
                    result.m_value = (double) ((int)sym1.m_value & (int)sym2.m_value);
                    break;
                case "|":
                    result.m_value = (double) ((int)sym1.m_value | (int)sym2.m_value);
                    break;

                default:
                    result.m_type = Type.Error;
                    result.m_name = "Undefine operator: " + opr.m_name + ".";
                    break;
            }
            return result;
        }
Exemplo n.º 3
0
 protected Symbol EvaluateFunction(string name, params Object[] args)
 {
     Symbol result = new Symbol();
     result.m_name = "";
     result.m_type = Type.Result;
     result.m_value = 0;
     switch (name)
     {
         case "cos":
             if (args.Length == 1)
             {
                 result.m_name = name + "(" + ((Symbol)args[0]).m_value.ToString() + ")";
                 result.m_value = System.Math.Cos(((Symbol)args[0]).m_value);
             }
             else
             {
                 result.m_name = "Invalid number of parameters in: " + name + ".";
                 result.m_type = Type.Error;
             }
             break;
         case "sin":
             if (args.Length == 1)
             {
                 result.m_name = name + "(" + ((Symbol)args[0]).m_value.ToString() + ")";
                 result.m_value = System.Math.Sin(((Symbol)args[0]).m_value);
             }
             else
             {
                 result.m_name = "Invalid number of parameters in: " + name + ".";
                 result.m_type = Type.Error;
             }
             break;
         case "tan":
             if (args.Length == 1)
             {
                 result.m_name = name + "(" + ((Symbol)args[0]).m_value.ToString() + ")";
                 result.m_value = System.Math.Tan(((Symbol)args[0]).m_value);
             }
             else
             {
                 result.m_name = "Invalid number of parameters in: " + name + ".";
                 result.m_type = Type.Error;
             }
             break;
         case "cosh":
             if (args.Length == 1)
             {
                 result.m_name = name + "(" + ((Symbol)args[0]).m_value.ToString() + ")";
                 result.m_value = System.Math.Cosh(((Symbol)args[0]).m_value);
             }
             else
             {
                 result.m_name = "Invalid number of parameters in: " + name + ".";
                 result.m_type = Type.Error;
             }
             break;
         case "sinh":
             if (args.Length == 1)
             {
                 result.m_name = name + "(" + ((Symbol)args[0]).m_value.ToString() + ")";
                 result.m_value = System.Math.Sinh(((Symbol)args[0]).m_value);
             }
             else
             {
                 result.m_name = "Invalid number of parameters in: " + name + ".";
                 result.m_type = Type.Error;
             }
             break;
         case "tanh":
             if (args.Length == 1)
             {
                 result.m_name = name + "(" + ((Symbol)args[0]).m_value.ToString() + ")";
                 result.m_value = System.Math.Tanh(((Symbol)args[0]).m_value);
             }
             else
             {
                 result.m_name = "Invalid number of parameters in: " + name + ".";
                 result.m_type = Type.Error;
             }
             break;
         case "log":
             if (args.Length == 1)
             {
                 result.m_name = name + "(" + ((Symbol)args[0]).m_value.ToString() + ")";
                 result.m_value = System.Math.Log10(((Symbol)args[0]).m_value);
             }
             else
             {
                 result.m_name = "Invalid number of parameters in: " + name + ".";
                 result.m_type = Type.Error;
             }
             break;
         case "ln":
             if (args.Length == 1)
             {
                 result.m_name = name + "(" + ((Symbol)args[0]).m_value.ToString() + ")";
                 result.m_value = System.Math.Log(((Symbol)args[0]).m_value);//, 2);
             }
             else
             {
                 result.m_name = "Invalid number of parameters in: " + name + ".";
                 result.m_type = Type.Error;
             }
             break;
         case "logn":
             if (args.Length == 2)
             {
                 result.m_name = name + "(" + ((Symbol)args[0]).m_value.ToString() + "'" + ((Symbol)args[1]).m_value.ToString() + ")";
                 result.m_value = System.Math.Log(((Symbol)args[0]).m_value); // , ((Symbol)args[1]).m_value);
             }
             else
             {
                 result.m_name = "Invalid number of parameters in: " + name + ".";
                 result.m_type = Type.Error;
             }
             break;
         case "sqrt":
             if (args.Length == 1)
             {
                 result.m_name = name + "(" + ((Symbol)args[0]).m_value.ToString() + ")";
                 result.m_value = System.Math.Sqrt(((Symbol)args[0]).m_value);
             }
             else
             {
                 result.m_name = "Invalid number of parameters in: " + name + ".";
                 result.m_type = Type.Error;
             }
             break;
         case "abs":
             if (args.Length == 1)
             {
                 result.m_name = name + "(" + ((Symbol)args[0]).m_value.ToString() + ")";
                 result.m_value = System.Math.Abs(((Symbol)args[0]).m_value);
             }
             else
             {
                 result.m_name = "Invalid number of parameters in: " + name + ".";
                 result.m_type = Type.Error;
             }
             break;
         case "acos":
             if (args.Length == 1)
             {
                 result.m_name = name + "(" + ((Symbol)args[0]).m_value.ToString() + ")";
                 result.m_value = System.Math.Acos(((Symbol)args[0]).m_value);
             }
             else
             {
                 result.m_name = "Invalid number of parameters in: " + name + ".";
                 result.m_type = Type.Error;
             }
             break;
         case "asin":
             if (args.Length == 1)
             {
                 result.m_name = name + "(" + ((Symbol)args[0]).m_value.ToString() + ")";
                 result.m_value = System.Math.Asin(((Symbol)args[0]).m_value);
             }
             else
             {
                 result.m_name = "Invalid number of parameters in: " + name + ".";
                 result.m_type = Type.Error;
             }
             break;
         case "atan":
             if (args.Length == 1)
             {
                 result.m_name = name + "(" + ((Symbol)args[0]).m_value.ToString() + ")";
                 result.m_value = System.Math.Atan(((Symbol)args[0]).m_value);
             }
             else
             {
                 result.m_name = "Invalid number of parameters in: " + name + ".";
                 result.m_type = Type.Error;
             }
             break;
         case "exp":
             if (args.Length == 1)
             {
                 result.m_name = name + "(" + ((Symbol)args[0]).m_value.ToString() + ")";
                 result.m_value = System.Math.Exp(((Symbol)args[0]).m_value);
             }
             else
             {
                 result.m_name = "Invalid number of parameters in: " + name + ".";
                 result.m_type = Type.Error;
             }
             break;
         default:
             if (m_defaultFunctionEvaluation != null)
                 result = m_defaultFunctionEvaluation(name, args);
             else
             {
                 result.m_name = "Function: " + name + ", not found.";
                 result.m_type = Type.Error;
             }
             break;
     }
     return result;
 }
Exemplo n.º 4
0
        public void Parse(string equation)
        {
            int state = 1;
            string temp = "";
            Symbol ctSymbol;

            m_bError = false;
            m_sErrorDescription = "None";

            m_equation.Clear();
            m_postfix.Clear();

            int nPos = 0;
            //-- Remove all white spaces from the equation string --
            equation = equation.Trim();
            while ((nPos = equation.IndexOf(' ')) != -1)
                equation = equation.Remove(nPos, 1);

            for (int i = 0; i < equation.Length; i++)
            {
                ctSymbol = new Symbol();
                switch (state)
                {
                    case 1:
                        if (Char.IsNumber(equation[i]) || equation[i] == '.')
                        {
                            state = 2;
                            temp += equation[i];
                        }
                        else if (Char.IsLetter(equation[i]))
                        {
                            state = 3;
                            temp += equation[i];
                        }
                        else
                        {
                            ctSymbol.m_name = equation[i].ToString();
                            ctSymbol.m_value = 0;
                            switch (ctSymbol.m_name)
                            {
                                case ",":
                                    ctSymbol.m_type = Type.Comma;
                                    break;
                                case "(":
                                case ")":
                                case "[":
                                case "]":
                                case "{":
                                case "}":
                                    ctSymbol.m_type = Type.Bracket;
                                    break;
                                default:
                                    ctSymbol.m_type = Type.Operator;
                                    break;
                            }
                            m_equation.Add(ctSymbol);
                        }
                        break;
                    case 2:
                        if ((Char.IsNumber(equation[i])) || (equation[i] == '.'))
                            temp += equation[i];
                        else if (!Char.IsLetter(equation[i]))
                        {
                            state = 1;
                            ctSymbol.m_name = temp;
                            ctSymbol.m_value = Double.Parse(temp);
                            ctSymbol.m_type = Type.Value;
                            m_equation.Add(ctSymbol);
                            ctSymbol = new Symbol();
                            ctSymbol.m_name = equation[i].ToString();
                            ctSymbol.m_value = 0;
                            switch (ctSymbol.m_name)
                            {
                                case ",":
                                    ctSymbol.m_type = Type.Comma;
                                    break;
                                case "(":
                                case ")":
                                case "[":
                                case "]":
                                case "{":
                                case "}":
                                    ctSymbol.m_type = Type.Bracket;
                                    break;
                                default:
                                    ctSymbol.m_type = Type.Operator;
                                    break;
                            }
                            m_equation.Add(ctSymbol);
                            temp = "";
                        }
                        break;
                    case 3:
                        if (Char.IsLetterOrDigit(equation[i]) || equation[i] == '_' || equation[i] == ':')
                            temp += equation[i];
                        else
                        {
                            state = 1;
                            ctSymbol.m_name = temp;
                            ctSymbol.m_value = 0;
                            if (equation[i] == '(')
                                ctSymbol.m_type = Type.Function;
                            else
                            {
                                if (ctSymbol.m_name == "pi")
                                    ctSymbol.m_value = System.Math.PI;
                                else if (ctSymbol.m_name == "e")
                                    ctSymbol.m_value = System.Math.E;
                                ctSymbol.m_type = Type.Variable;
                            }
                            m_equation.Add(ctSymbol);
                            ctSymbol = new Symbol();
                            ctSymbol.m_name = equation[i].ToString();
                            ctSymbol.m_value = 0;
                            switch (ctSymbol.m_name)
                            {
                                case ",":
                                    ctSymbol.m_type = Type.Comma;
                                    break;
                                case "(":
                                case ")":
                                case "[":
                                case "]":
                                case "{":
                                case "}":
                                    ctSymbol.m_type = Type.Bracket;
                                    break;
                                default:
                                    ctSymbol.m_type = Type.Operator;
                                    break;
                            }
                            m_equation.Add(ctSymbol);
                            temp = "";
                        }
                        break;
                }
            }
            if (temp != "")
            {
                ctSymbol = new Symbol();
                ctSymbol.m_name = temp;
                if (state == 2)
                {
                    ctSymbol.m_value = Double.Parse(temp);
                    ctSymbol.m_type = Type.Value;
                }
                else
                {
                    if (ctSymbol.m_name == "pi")
                        ctSymbol.m_value = System.Math.PI;
                    else if (ctSymbol.m_name == "e")
                        ctSymbol.m_value = System.Math.E;
                    else
                        ctSymbol.m_value = 0;
                    ctSymbol.m_type = Type.Variable;
                }
                m_equation.Add(ctSymbol);
            }
        }