import java.util.Stack; public class EvaluateString{ public static int

import java.util.Stack; public class EvaluateString{    public static int evaluate(String expression)    {        char tokens = expression.toCharArray();          // Stack for numbers: ‘values’        Stack values = new Stack();         // Stack for Operators: ‘ops’        Stack ops = new Stack();         for (int i = 0; i < tokens.length; i++)        {             // Current token is a whitespace, skip it            if (tokensi == ' ')                continue;             // Current token is a number, push it to stack for numbers            if (tokensi >= ‘0’ && tokensi <= '9')            {                StringBuffer sbuf = new StringBuffer();                // There may be more than one digits in number                while (i < tokens.length && tokensi >= ‘0’ && tokensi <= '9')                    sbuf.append(tokensi++);                values.push(Integer.parseInt(sbuf.toString()));            }             // Current token is an opening brace, push it to 'ops'            else if (tokensi == '(')                ops.push(tokensi);             // Closing brace encountered, solve entire brace            else if (tokensi == ')')            {                while (ops.peek() != '(')                  values.push(applyOp(ops.pop(), values.pop(), values.pop()));                ops.pop();            }             // Current token is an operator.            else if (tokensi == '+' || tokensi == '-' ||                     tokensi == '*' || tokensi == '/')            {                // While top of 'ops' has same or greater precedence to current                // token, which is an operator. Apply operator on top of 'ops'                // to top two elements in values stack                while (!ops.empty() && hasPrecedence(tokensi, ops.peek()))                  values.push(applyOp(ops.pop(), values.pop(), values.pop()));                 // Push current token to 'ops'.                ops.push(tokensi);            }        }         // Entire expression has been parsed at this point, apply remaining        // ops to remaining values        while (!ops.empty())            values.push(applyOp(ops.pop(), values.pop(), values.pop()));         // Top of 'values' contains result, return it        return values.pop();    }     // Returns true if 'op2' has higher or same precedence as 'op1',    // otherwise returns false.    public static boolean hasPrecedence(char op1, char op2)    {        if (op2 == '(' || op2 == ')')            return false;        if ((op1 == '*' || op1 == '/') && (op2 == '+' || op2 == '-'))            return false;        else            return true;    }     // A utility method to apply an operator 'op' on operands 'a'     // and 'b'. Return the result.    public static int applyOp(char op, int b, int a)    {        switch (op)        {        case '+':            return a + b;        case '-':            return a - b;        case '*':            return a * b;        case '/':            if (b == 0)                throw new                UnsupportedOperationException("Cannot divide by zero");            return a / b;        }        return 0;    }     // Driver method to test above methods    public static void main(String args)    {        System.out.println(EvaluateString.evaluate("10 + 2 * 6"));        System.out.println(EvaluateString.evaluate("100 * 2 + 12"));        System.out.println(EvaluateString.evaluate("100 * ( 2 + 12 )"));        System.out.println(EvaluateString.evaluate("100 * ( 2 + 12 ) / 14"));    }}

x

Hi!
I'm Harold!

Would you like to get a custom essay? How about receiving a customized one?

Check it out