예제 #1
0
        public void TestMethod1()
        {
            ExprSolver inst = new ExprSolver();
            //(3+5.3) * 2.7 - ln(22) / pow(2.2, -1.7)  ~ 10.6
            string expresion = "(3+5.3) * (2.7 + sqrt(4)) / pow(2,2)";
            expresion =inst.ReplaceFunctionsWithEvaluation(expresion);
            expresion =inst.ClearSpaces(expresion);
                            //(3+5.3)*(2.7+2)/4
            Stack<object> splitedExpresion =inst.SplitExpresion(expresion);
            Stack<object> splited = new Stack<object>();
            splited.Push("(");
            splited.Push(3.0);
            splited.Push("+");
            splited.Push(5.3);
            splited.Push(")");
            splited.Push("*");
            splited.Push("(");
            splited.Push(2.7);
            splited.Push("+");
            splited.Push(2.0);
            splited.Push(")");
            splited.Push("/");
            splited.Push(4.0);

            object[] x = splited.ToArray();
            string xx = string.Join(" ", x);
            object[] y = splitedExpresion.ToArray();
            string yy = string.Join(" ", y);

            splitedExpresion = inst.ReverseStack(splitedExpresion);
            Stack<object> reversedPolishNotation = inst.ShuntingYard(splitedExpresion);
            //3	5.3	+ 2.7 2	+ 4 / *
            Stack<object> shunt = new Stack<object>();

            shunt.Push("*");
            shunt.Push("/");
            shunt.Push(4.0);
            shunt.Push("+");
            shunt.Push(2.0);
            shunt.Push(2.7);
            shunt.Push("+");
            shunt.Push(5.3);
            shunt.Push(3.0);
            object[] a = shunt.ToArray();
            string aa = string.Join(" ", a);
            object[] b = splitedExpresion.ToArray();
            string bb = string.Join(" ", b);
            double polish = inst.ReversedPolishNotation(reversedPolishNotation);

            //ReplaceFunctionsWithEvaluation and ClearSpaces Test
            Assert.AreEqual("(3+5.3)*(2.7+2)/4", expresion);
            //SplitExpresion Test
            Assert.AreEqual(xx, yy);
            //ReverseStack and ShuntingYard Test
            Assert.AreEqual(aa, bb);
            Assert.AreEqual(9.753, polish);
        }
예제 #2
0
        static void Main(string[] args)
        {
            /* Write a program that calculates the value of given arithmetical expression. The expression can contain the following elements only:
            Real numbers, e.g. 5, 18.33, 3.14159, 12.6
            Arithmetic operators: +, -, *, / (standard priorities)
            Mathematical functions: ln(x), sqrt(x), pow(x,y)
            Brackets (for changing the default priorities)
                Examples:
                (3+5.3) * 2.7 - ln(22) / pow(2.2, -1.7)  ~ 10.6
                pow(2, 3.14) * (3 - (3 * sqrt(2) - 3.2) + 1.5*0.3)  ~ 21.22
                Hint: Use the classical "shunting yard" algorithm and "reverse Polish notation".*/
            //string expresion = "  pow( 2 , 3.14) * (3 - (3 * sqrt(2) - 3.2) + 1.5*0.3)";
            string expresion = "  (3+5) * 2 - 4 / pow(2,2)";
            ExprSolver inst = new ExprSolver();
            //string expresion = "(3+5.3) * 2.7 + sqrt(4) / pow(2,8)";

            Console.WriteLine(inst.ExpresionSolever(expresion));
        }