Exemple #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);
            }
        }
Exemple #2
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);
            }
        }
Exemple #3
0
        public static void UserFriendlyDisplay(MathSolverLibrary.Equation.SolveResult result, MathSolverLibrary.TermType.EvalData evalData)
        {
            if (evalData.GetMsgs() != null)
            {
                foreach (string msg in evalData.GetMsgs())
                {
                    WriteLineColor(ConsoleColor.DarkYellow, " " + msg);
                }
            }

            if (evalData.GetInputTypeStr() != null)
            {
                WriteLineColor(ConsoleColor.DarkGreen, "Topic is " + evalData.GetInputTypeStr());
            }

            if (evalData.GetGraphEqStrs() != null)
            {
                string finalGraphStr = "";
                for (int i = 0; i < evalData.GetGraphEqStrs().Length; ++i)
                {
                    finalGraphStr += evalData.GetGraphEqStrs()[i];
                    if (i != evalData.GetGraphEqStrs().Length - 1)
                    {
                        finalGraphStr += "; ";
                    }
                }

                WriteLineColor(ConsoleColor.White, "Graph " + finalGraphStr);
            }

            if (!result.Success)
            {
                WriteLineColor(ConsoleColor.DarkRed, "Failure");
                foreach (string msg in evalData.GetFailureMsgs())
                {
                    WriteLineColor(ConsoleColor.Red, "  " + msg);
                }
            }
            else
            {
                if (result.Solutions == null)
                {
                    return;
                }

                int solCount = result.Solutions.Count;
                if (evalData.GetHasPartialSolutions())
                {
                    Console.WriteLine("The input was partially evaluated to...");
                    for (int i = 0; i < evalData.GetPartialSolutions().Count; ++i)
                    {
                        string partialSolStr = evalData.PartialSolToTexStr(i);
                        partialSolStr = MathSolver.FinalizeOutput(partialSolStr);
                        Console.WriteLine(" " + partialSolStr);
                    }
                    Console.WriteLine();
                    if (solCount > 0)
                    {
                        string pluralStr = solCount > 1 ? "s were" : " was";
                        Console.WriteLine("The following " + solCount.ToString() + " solution" + pluralStr +
                                          " also obtained...");
                        DisplayUserFriendlySols(result.Solutions);
                    }
                }
                else
                {
                    Console.WriteLine("The input was successfully evaluated.");
                    if (solCount > 0)
                    {
                        DisplayUserFriendlySols(result.Solutions);
                    }
                    if (result.GetHasRestrictions())
                    {
                        DisplayUserFreindlyRests(result.Restrictions);
                    }
                }
            }
        }
Exemple #4
0
        private static MathSolverLibrary.Equation.SolveResult EvaluateTerm(MathSolverLibrary.TermType.GenTermType termEval, int optionIndex, ref MathSolverLibrary.TermType.EvalData evalData)
        {
            Stopwatch stopwatch = new Stopwatch();

            stopwatch.Start();
            MathSolverLibrary.Equation.SolveResult result = termEval.ExecuteCommandIndex(optionIndex - 1, ref evalData);
            stopwatch.Stop();

            ConsoleHelper.WriteLineColor(ConsoleColor.DarkCyan, "Evaluating took " + stopwatch.ElapsedMilliseconds.ToString() + "ms");

            return(result);
        }
Exemple #5
0
        private static MathSolverLibrary.TermType.GenTermType ParseInput(string input, ref MathSolverLibrary.Information_Helpers.FuncDefHelper funcDefHelper, ref MathSolverLibrary.TermType.EvalData evalData)
        {
            Stopwatch stopwatch = new Stopwatch();

            // Start timing how long the parsing process takes.
            stopwatch.Start();

            // Will contain the list of parsing errors if any.
            List <string> parseErrors = new List <string>();

            // Parse the input using the math parsing engine.
            var termEval = MathSolver.ParseInput(input, ref evalData, ref parseErrors);

            // Stop the timing.
            stopwatch.Stop();

            if (termEval == null)
            {
                // The user's input was invalid.
                ConsoleHelper.WriteLineColor(ConsoleColor.Red, "Cannot interpret.");
                return(null);
            }

            ConsoleHelper.WriteLineColor(ConsoleColor.DarkCyan, "Parsing took " + stopwatch.ElapsedMilliseconds.ToString() + "ms");

            return(termEval);
        }