CMSC 350 Project 1 The first programming project involves wr
CMSC 350 Project 1
The first programming project involves writing a program that evaluates infix expressions of unsigned integers using two stacks. The program should consist of three classes. The main class should create a GUI that allows the user input an infix expression and displays the result. The GUI should look as follows:
The GUI must be generated by code that you write. You may not use a drag-and-drop GUI generator.
The second class should contain the code to perform the infix expression evaluation. The pseudocode for performing that evaluation is shown below:
tokenize the string containing the expression while there are more tokens get the next token if it is an operand push it onto the operand stack else if it is a left parenthesis push it onto the operator stack else if it is a right parenthesis while top of the operator stack not a left parenthesis pop two operands and an operator perform the calculation push the result onto the operand stack
else if it is an operator
while the operator stack is not empty and the operator at the top of the stack has higher or the same precedence than the current operator pop two operands and perform the calculation push the result onto the operand stack push the current operator on the operators stack while the operator stack is not empty pop two operands and an operator perform the calculation push the result onto the operand stack the final result is a the top of the operand stack
Be sure to add any additional methods needed to eliminate any duplication of code.
Your program is only expected to perform correctly on syntactically correct infix expressions that contain integer operands and the four arithmetic operators + - * /. It should not, however, require spaces between tokens. The usual precedence rules apply. The division performed should be integer division. A check should be made for division by zero. Should the expression contain division by zero, a checked exception DivideByZero should be thrown by the method that performs the evaluation and caught in the main class, where a JOptionPane window should be displayed containing an error message.
Solution
Question has multiple parts, I\'m providing solution for 1st full part. Java code for evaluating InFix notation.
import java.util.Stack;
public class EvaluateString
{
public static int evaluate(String expression)
{
char[] tokens = expression.toCharArray();
// Stack for numbers: \'values\'
Stack<Integer> values = new Stack<Integer>();
// Stack for Operators: \'ops\'
Stack<Character> ops = new Stack<Character>();
for (int i = 0; i < tokens.length; i++)
{
// Current token is a whitespace, skip it
if (tokens[i] == \' \')
continue;
// Current token is a number, push it to stack for numbers
if (tokens[i] >= \'0\' && tokens[i] <= \'9\')
{
StringBuffer sbuf = new StringBuffer();
// There may be more than one digits in number
while (i < tokens.length && tokens[i] >= \'0\' && tokens[i] <= \'9\')
sbuf.append(tokens[i++]);
values.push(Integer.parseInt(sbuf.toString()));
}
// Current token is an opening brace, push it to \'ops\'
else if (tokens[i] == \'(\')
ops.push(tokens[i]);
// Closing brace encountered, solve entire brace
else if (tokens[i] == \')\')
{
while (ops.peek() != \'(\')
values.push(applyOp(ops.pop(), values.pop(), values.pop()));
ops.pop();
}
// Current token is an operator.
else if (tokens[i] == \'+\' || tokens[i] == \'-\' ||
tokens[i] == \'*\' || tokens[i] == \'/\')
{
// 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(tokens[i], ops.peek()))
values.push(applyOp(ops.pop(), values.pop(), values.pop()));
// Push current token to \'ops\'.
ops.push(tokens[i]);
}
}
// 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
DivideByZero(\"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\"));
}
}



