Exemplo n.º 1
0
        /**
         * Calculates the numeric value of an expression.
         *
         * @param expression     the expression to calculate
         *
         * @return the numeric value of the expression
         *
         * @throws Exception if the expression contained an error
         */
        public int Calculate(string expression)
        {
            ArithmeticParser parser;
            Node             node;

            parser = new ArithmeticParser(new StringReader(expression), this);
            parser.Prepare();
            node = parser.Parse();
            return((int)node.Values[0]);
        }
        /**
         * Creates a new parser.
         *
         * @param input          the input to parse
         *
         * @return the parser created
         */
        private Parser CreateParser(string input)
        {
            Parser parser = null;

            try {
                parser = new ArithmeticParser(new StringReader(input));
                parser.Prepare();
            } catch (ParserCreationException e) {
                Fail(e.Message);
            }
            return(parser);
        }