コード例 #1
0
ファイル: Program.cs プロジェクト: ASzot/MathologicaDemo
        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
ファイル: Program.cs プロジェクト: ASzot/MathologicaDemo
        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);
        }
コード例 #3
0
ファイル: MathTest.cs プロジェクト: ASzot/MathologicaDemo
        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);
        }
コード例 #4
0
        public async Task <ActionResult <MathHistory> > Create(MathHistory mathHistory)
        {
            // Runs equation through SolveMath to return an answer
            mathHistory.Answer = MathSolver.SolveMath(mathHistory.Equation);
            _context.MathHistories.Add(mathHistory);
            await _context.SaveChangesAsync();

            return(CreatedAtAction(nameof(GetById), new { id = mathHistory.Id }, mathHistory));
        }
コード例 #5
0
        public void Construct_MathSolver_ShouldNotBeNull()
        {
            //---------------Set up test pack-------------------

            //---------------Assert Precondition----------------

            //---------------Execute Test ----------------------
            var mathSolver = new MathSolver();

            //---------------Test Result -----------------------
            Assert.IsNotNull(mathSolver);
        }
コード例 #6
0
        static void Main(string[] args)
        {
            var mathSolver = new MathSolver();
            var lines      = Convert.ToInt32(System.Console.ReadLine());

            for (int i = 0; i < lines; i++)
            {
                var numberLine = System.Console.ReadLine();

                var result = mathSolver.Solve(new FindThePointProblem(numberLine));
                System.Console.WriteLine(result);
            }
        }
コード例 #7
0
        public void FindThePointProblem_Given1122_ShouldReturn33()
        {
            //---------------Set up test pack-------------------
            var mathSolver          = new MathSolver();
            var findThePointProblem = new FindThePointProblem("1 1 2 2");
            //---------------Assert Precondition----------------

            //---------------Execute Test ----------------------
            var result = mathSolver.Solve(findThePointProblem);

            //---------------Test Result -----------------------
            Assert.AreEqual("3 3", result);
        }
コード例 #8
0
        public string Compute()
        {
            MathSolver _solverSvc = new MathSolver();
            var        result     = _solverSvc.Solve(Request.Query["expr"]);

            if (String.IsNullOrEmpty(result.Error))
            {
                return(result.Value.ToString());
            }
            else
            {
                return(result.Error);
            }
        }
コード例 #9
0
        public static void DisplayUserFriendlySols(List <MathSolverLibrary.Equation.Solution> solutions)
        {
            if (solutions.Count > 0)
            {
                Console.WriteLine();
                WriteLineColor(ConsoleColor.DarkYellow, "Solutions:");
            }

            foreach (var sol in solutions)
            {
                Console.WriteLine();
                string starterStr = "";
                if (sol.SolveFor != null)
                {
                    starterStr += sol.SolveForToTexStr() + " " + sol.ComparisonOpToTexStr() + " ";
                }
                string outputStr;
                if (sol.Result != null)
                {
                    Console.WriteLine("Result:");
                    outputStr = starterStr + sol.ResultToTexStr();
                    outputStr = MathSolver.FinalizeOutput(outputStr);
                    Console.WriteLine(" " + outputStr);
                    if (sol.Multiplicity != 1)
                    {
                        Console.WriteLine(" " + "Multiplicity of " + sol.Multiplicity.ToString() + ".");
                    }
                }
                if (sol.GeneralResult != null)
                {
                    Console.WriteLine("General Result:");
                    outputStr = starterStr + sol.GeneralToTexStr();
                    outputStr = MathSolver.FinalizeOutput(outputStr);
                    Console.WriteLine(" " + outputStr);
                    Console.WriteLine("     Where " + sol.GeneralResult.IterVarToTexString() + " is a real integer.");
                }
                if (sol.ApproximateResult != null)
                {
                    Console.WriteLine("Approximate Result:");
                    outputStr = starterStr + sol.ApproximateToTexStr();
                    outputStr = MathSolver.FinalizeOutput(outputStr);
                    Console.WriteLine(" " + outputStr);
                }
            }

            Console.WriteLine();
        }
コード例 #10
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);
                    }
                }
            }
        }
コード例 #11
0
ファイル: TestMathSolver.cs プロジェクト: anderscui/cslib
        public void TestQ1()
        {
            var res = MathSolver.Q1();

            Console.WriteLine("{0}, {1}", res.Item1, res.Item2);
        }