Ejemplo n.º 1
0
		public CalcResult Solve (string input) {
			ParseResult parseResult = this.ParseInput (input);

			if (parseResult.Error != CalcError.None) {
				return new CalcResult (parseResult.Error, parseResult.ErrorObject);
			}

			//Debug.WriteLine (parseResult.Output);
			//return new CalcResult (1);

			CalcResult result = this.ActualSolve (parseResult.Output);
			
			if (result.Error == CalcError.None) {
				result = new CalcResult (this.Round (result.Result));
				this.constant.PreviousAnswer = result.Result;
			}

			return result;
		}
Ejemplo n.º 2
0
        public CalcResult Solve(string input)
        {
            ParseResult parseResult = this.ParseInput(input);

            if (parseResult.Error != CalcError.None)
            {
                return(new CalcResult(parseResult.Error, parseResult.ErrorObject));
            }

            //Debug.WriteLine (parseResult.Output);
            //return new CalcResult (1);

            CalcResult result = this.ActualSolve(parseResult.Output);

            if (result.Error == CalcError.None)
            {
                result = new CalcResult(this.Round(result.Result));
                this.constant.PreviousAnswer = result.Result;
            }

            return(result);
        }
Ejemplo n.º 3
0
        private ParseResult ParseInput(string input)
        {
            //remove all whitespace and convert to lower
            input = input.Replace(" ", "").ToLower();

            CalcResult result = this.core.VerifyBrackets(input);

            if (result.Error != CalcError.None)
            {
                return(new ParseResult(result.Error, result.ErrorObject));
            }

            result = this.function.VerifyBrackets(input);

            if (result.Error != CalcError.None)
            {
                return(new ParseResult(result.Error, result.ErrorObject));
            }

            //convert negation sign to a specific negation function
            input = this.ConvertNegationSign(input);

            //output string list
            List <string> output = new List <string> ();

            //operator stack
            Stack <string> stack = new Stack <string> ();

            //parse token until input is empty
            //shunting yard starts now!
            while (input.Length > 0)
            {
                string token = "";

                //check each module to see if they can find a token
                foreach (IModule module in this.modules)
                {
                    token = module.GetNextToken(input);

                    if (token != "")
                    {
                        break;
                    }
                }

                //no token means unrecognized input, bail
                if (token == "")
                {
                    return(new ParseResult(CalcError.UnknownToken, input));
                }

                //remove the found token from the input
                input = this.RemoveTokenFromInput(input, token);

                //if number or constant, push to output
                if (this.core.IsNumber(token) || this.constant.IsToken(token))
                {
                    output.Add(token);
                }
                else if (this.core.IsLeftBracket(token))                       //if left bracket, push to stack
                {
                    stack.Push(token);
                }
                else if (this.core.IsRightBracket(token))                       //if right bracket, push all the things
                //push stack onto output until left bracket is found
                {
                    while (this.core.IsLeftBracket(stack.Peek()) == false)
                    {
                        output.Add(stack.Pop());
                    }

                    //throw away left bracket
                    stack.Pop();
                }
                else if (this.function.IsToken(token))
                {
                    stack.Push(token);
                }
                else
                {
                    //if operatorr token, apply precedence and associativity rules
                    if (this.operatorr.IsToken(token))
                    {
                        //if exponent, push to stack
                        if (this.operatorr.IsExponent(token) || this.operatorr.IsNegation(token))
                        {
                            stack.Push(token);
                        }
                        else                              //if other operatorr operators
                                                          //pop stack onto output as long as top of stack is more important than current operator
                        {
                            while (stack.Count > 0 && this.operatorr.Op1PrecedenceLessOrEqualOp2(token, stack.Peek()))
                            {
                                output.Add(stack.Pop());
                            }

                            stack.Push(token);
                        }
                    }
                }
            }

            //join the output and the stack and return the string
            return(new ParseResult((string.Join(" ", output) + " " + string.Join(" ", stack.ToArray())).Trim()));
        }