Пример #1
0
        private void ButtonEquals_Click(object sender, RoutedEventArgs e)
        {
            Utils.log("ButtonEquals_Click", "Clicked Equals");
            resetFlashTimer = true;

            // if just hit equals, re-evaluate expression;
            if (JUST_HIT_EQUALS)
            {
                Utils.log("ButtonEquals_Click", "Hit equals twice in a row");
                ClearAllTokens();
                this.TextBoxInputTokens.InsertRange(0, MEM_PREV_TOKENS);
                currentToken = MEM_PREV_TOKENS.Count;
                Utils.log("ButtonEquals_Click", "cToken = {0}", currentToken);
            }

            // Uncomment to view list of tokens.
            /*string str = "";
            foreach (Token t in TextBoxInputTokens)
            {
                Debugger.Log(0, "[ButtonEquals]", t.ToString());
                str += t.getRepresentation();
            }

            Debugger.Log(0, "[ButtonEquals]", str);*/

            this.TextBoxInputTokens.Add(new Token(TokenType.EOF, "Null"));

            currentToken += 1;
            Parser parser = new MathParser(this.TextBoxInputTokens);
            try
            {
                parser.term();
                // remove top element.
                this.TextBoxInputTokens.RemoveAt(this.TextBoxInputTokens.Count - 1);
                currentToken -= 1;

                MEM_PREV_TOKENS = new List<Token>(new List<Token>(this.TextBoxInputTokens));
            }
            catch (SyntaxException excp)
            {
                // remove EOF token and error
                this.TextBoxInputTokens.RemoveAt(this.TextBoxInputTokens.Count - 1);
                currentToken -= 1;

                MEM_ANS = 0;
                this.answerTokens.Add(new IdentToken("MATH ERROR"));

                DrawEquals();
                this.currentToken = excp.getPosition();
                UpdateTextBox();

                return;
            }
            catch (Exception)
            {
                // remove EOF token and error
                this.TextBoxInputTokens.RemoveAt(this.TextBoxInputTokens.Count - 1);
                currentToken -= 1;

                MEM_ANS = 0;
                this.answerTokens.Add(new IdentToken("MATH ERROR"));

                DrawEquals();
                return;
            }

            AST output;

            try
            {
                output = parser.Pop();
                double answer = output.evaluate();
                Utils.log("ButtonEquals_Click", "Answer = {0}", answer);

                // Handle infinite answers.
                if (Double.IsInfinity(answer))
                {
                    MEM_ANS = 0;
                    this.answerTokens.Add(new IdentToken("MATH ERR (ANSWER TOO LARGE)"));
                }
                else
                {
                    MEM_ANS = answer;

                    foreach (char c in Convert.ToString(answer))
                    {
                        if (c == '.')
                        {
                            this.answerTokens.Add(new DotToken());
                        }
                        else if (c == 'E')
                        {
                            this.answerTokens.Add(new OperationToken("x10", TokenType.OP_POW10));
                        }
                        else if (c == '-')
                        {
                            this.answerTokens.Add(new OperationToken("-", TokenType.OP_SUB));
                        }
                        else
                        {
                            this.answerTokens.Add(new DigitToken(Convert.ToString(c)));
                        }
                    }
                }

                Utils.log("ButtonEquals_Click", "Drawing AnswerBox");
                DrawEquals();
            }
            catch (MathException)
            {
                // remove EOF token and error
                this.TextBoxInputTokens.RemoveAt(this.TextBoxInputTokens.Count - 1);
                currentToken -= 1;

                MEM_ANS = 0;
                this.answerTokens.Add(new IdentToken("MATH ERROR"));

                DrawEquals();
                return;
            }
            catch (Exception)
            {
                // remove EOF token and error
                this.TextBoxInputTokens.RemoveAt(this.TextBoxInputTokens.Count - 1);
                currentToken -= 1;

                MEM_ANS = 0;
                this.answerTokens.Add(new IdentToken("MATH ERROR"));

                DrawEquals();
                return;
            }
        }