Example #1
0
        public void parse()
        {
            string[] tokens;
            string   token;                    // String variable for the individual token
            double   intWrapper;               // integer wrapper to get a primitive from
            string   oper;

            double op1, op2;                                 // the two operands to compute
            double element;                                  // the element to be pushed on the stack

            char[] delimiterChars = { ' ', ',', ':', '\t' }; //not all used
            tokens = str.Split(delimiterChars);

            foreach (string s in tokens)
            {
                token = s;
                oper  = token;
                if (this.isOperator(oper))
                {
                    op2 = stack.Pop();                   // pop the last 2 elements from the stack
                    op1 = stack.Pop();
                    this.compute(op1, op2, oper);        // and compute them
                    stack.Push(this.intermediateResult); // now stick the intermediateResult on the stack
                }
                else
                {                   // not an operator?
                    intWrapper = double.Parse(token);

                    element = intWrapper;

                    stack.Push(element);            // Push that element back onto the stack
                }
            }
        }