Пример #1
0
        private static void Main(string[] args)
        {
            // The caching mechanism for saving any user defined constants or functions.
            MathSolverLibrary.Information_Helpers.FuncDefHelper funcDefHelper = new MathSolverLibrary.Information_Helpers.FuncDefHelper();

            // Display some messages to the user.
            ConsoleHelper.SetConsoleWindow();
            ConsoleHelper.DisplayHelpScreen();

            // Initialize the math solving engine.
            MathSolver.Init();

            for (; ;)
            {
                // Poll user input.
                Console.ForegroundColor = ConsoleColor.Magenta;
                Console.Write(">");
                Console.ResetColor();
                string inputStr = Console.ReadLine();

                // Check if the user wants to quit the program.
                if (inputStr == "quit")
                {
                    break;
                }

                if (ProcessSpecialCommand(inputStr))
                {
                    continue;
                }

                // The temporary data necessary for the math evaluation engine.
                // Necessary in the parsing stage to determine the context and meaning of the expression.
                MathSolverLibrary.TermType.EvalData evalData = new MathSolverLibrary.TermType.EvalData(_useRad, new WorkMgr(), funcDefHelper);


                var termEval = ParseInput(inputStr, ref funcDefHelper, ref evalData);

                // Display the possible methods of evaluation to the user.
                Console.WriteLine("Input desired evaluation option:");
                for (int i = 0; i < termEval.GetCmdCount(); ++i)
                {
                    Console.WriteLine(" " + (i + 1).ToString() + ")" + termEval.GetCommands()[i]);
                }

                // Get the command the user wants to evaluate.
                Console.ForegroundColor = ConsoleColor.Magenta;
                Console.Write(">");
                Console.ResetColor();
                string optionStr = Console.ReadLine();
                int    optionIndex;
                if (!int.TryParse(optionStr, out optionIndex))
                {
                    return;
                }

                MathSolverLibrary.Equation.SolveResult solveResult = EvaluateTerm(termEval, optionIndex, ref evalData);
                ConsoleHelper.UserFriendlyDisplay(solveResult, evalData);
            }
        }
Пример #2
0
        public static void Test()
        {
            // Initialize the math solving engine.
            MathSolver.Init();

            EvalData evalData = ConstructEvalData();

            // Object deriving from ExComp representing a algebraic variable.
            AlgebraComp x = new AlgebraComp("x");

            ExComp complexExpression = ConstructEx("3x^2 - 3", evalData);

            ExComp combined = AddOp.StaticCombine(x, complexExpression);

            // Square the expression.
            ExComp squared = PowOp.RaiseToPower(complexExpression, new ExNumber(2.0), ref evalData, false);

            // String containing the evaluated result.
            string result = squared.ToAlgTerm().FinalToDispStr();

            ExComp left  = ConstructEx(¨ln(x - 2) ¨, evalData);
            ExComp right = ConstructEx(¨3¨, evalData);

            // The object containing the functionality used to solve algebraic equations.
            AlgebraSolver agSolver = new AlgebraSolver();

            // Any additional information regarding the result will be stored in the evalData object.
            ExComp solveResult = agSolver.SolveEq(left, right, new AlgebraVar(¨x¨), evalData);
        }