Q2) Simple calculator: You are to design a simple calculator using the ArrayStac
ID: 3907436 • Letter: Q
Question
Q2) Simple calculator:
You are to design a simple calculator using the ArrayStack implementation in Q1 to perform additions, subtractions, multiplications and divisions. The user may enter an arithmetic expression in infix using numbers (0 to 9), parentheses((,),{,},[,],<,>) and the four arithmetic operations (+, -, *, /) only.
The first step to do so is to create a utility class MyCalculator that will have the following methods:
a) Input of an expression and checking Balanced Parenthesis: public static Boolean isBalanced(String expression) This is a static method that will read a string representing an infix ((operand operation operand)) mathematical expression with parentheses from left to right and decide whether the brackets are balanced or not.
For example, (5*{7+22} is not valid, but (5*{7+22}) is valid. To discover whether a string is balanced each character is read in turn. The character is categorized as an opening parenthesis, a closing parenthesis, or another type of character. Values of the third category are ignored for now. When a value of the first category is encountered, the corresponding close parenthesis is stored in the stack. For example, when a “(“ is read, the character “)” is pushed on the stack. When a “{“ is encountered, the character pushed is “}”. The topmost element of the stack is therefore the closing value we expect to see in a well balanced expression. When a closing character is encountered, it is compared to the topmost item in the stack. If they match, the top of the stack is popped and execution continues with the next character. If they do not match an error is reported (display a message about the error and return false). An error is also reported if a closing character is read and the stack is empty. If the stack is empty when the end of the expression is reached then the expression is well balanced. For this application a parenthesis can be one of the following: -parantheses: ( ) - angle brackets: < > -curly braces: { } -square brackets: [ ] These must be defined as constants in the class.
b) Infix to Postfix conversion Write a method that converts Infix expressions to postfix expressions using your stack implementation: public static String infixToPostfix(String infix) The method only takes balanced expressions with balanced parenthesis. The return is a string that has the Postfix expression of the input string.
c) Evaluating a Postfix expression Write a method that evaluates a postfix expression: public static double evaluate(string postfix);
d) Your main class QuestionTwo To evaluate your methods create the class QuestionTwo It must contain a public static void main(String[] args) method to run your code. This main method should ask the user to input one infix 3/5 expression per line until the user types “q” or “Q”. After every input, it should first test if the expression has balanced paranthesis and tells the user if the expression is balanced or not. If the expression is balanced it will convert it into a postfix expression, displays the expression and then evaluates it and outputs the results and then ask for the next input. If the expression is not balanced it tells the user that and asks for a new input.
Some rules:
-You should assume no whitespace in the infix expression provided by the user. For example, your code should work for inputs like “2+(3×2)”. The postfix that you will print, however, should have tokens separated by whitespace. For example, “3×21” should have the postfix “3 21 ×”, and not “321×”. Here is a suggested part of your main that you can use
import java.util.Scanner;
public class QuestionTwo{
public static void main(String[] args) {
Scanner calcScan = new Scanner(System.in);
Boolean finished=false;
while (!finished) {
System.out.println("Enter a postfix expression or q to quit: ");
String expression = calcScan.nextLine();
System.out.println(expression);
expression.trim();//omits leading and trailing whitepsaces
System.out.println(expression);
if (expression.equalsIgnoreCase("q")) {
finished=true;
} else {
//use your MyCalculator methods here
}
}
}
}
Deliverables:
- MyCalculator.java
-QuestionTwo.java
Explanation / Answer
MyCalculator.java
public class MyCalculator {
private static char OPEN_PARENTHESIS = '(';
private static char OPEN_CURLY = '{';
private static char OPEN_SQUARE = '[';
private static char OPEN_ANGULAR = '<';
private static char CLOSE_PARENTHESIS = ')';
private static char CLOSE_CURLY = '}';
private static char CLOSE_SQUARE = ']';
private static char CLOSE_ANGULAR = '>';
//infix expression is without any spaces
public static String convertInfixToPostfix(String infixExpression)
{
char infix[] = infixExpression.toCharArray();
char ch, op;
StringBuilder postfix = new StringBuilder("");
ArrayStack<Character> operators = new ArrayStack<Character>(100);
for(int i = 0; i < infix.length; i++)
{
ch = infix[i];
if(Character.isDigit(ch)) //operands , just append to postfix expression
postfix.append(ch);
else if(ch == OPEN_PARENTHESIS || ch == OPEN_CURLY || ch == OPEN_ANGULAR || ch == OPEN_SQUARE)
{
//if its an opening parenthesis, just push onto stack
operators.push(ch);
}
else if(ch == CLOSE_PARENTHESIS || ch == CLOSE_CURLY || ch == CLOSE_ANGULAR || ch == CLOSE_SQUARE)
{
//if its an closing parenthis, pop and append all operators till a matching parenthesis is found
//pop out the matching parenthesis from stack
char matching= ' ';
if(ch == CLOSE_PARENTHESIS)
matching = OPEN_PARENTHESIS;
else if(ch == CLOSE_CURLY)
matching = OPEN_CURLY;
else if(ch == CLOSE_ANGULAR)
matching = OPEN_ANGULAR;
else if(ch == CLOSE_SQUARE)
matching = OPEN_SQUARE;
while ( (op = operators.peek()) != matching)
{
postfix.append(" " + op + " ");
operators.pop();
}
operators.pop();
}
else //operators
{
//pop and append all higher precedence operators on stack, then push this operator
postfix.append(' ');
while (!operators.isEmpty())
{
op = operators.peek();
boolean isParenthesis = (op == OPEN_ANGULAR || op == OPEN_SQUARE || op == OPEN_CURLY || op == OPEN_PARENTHESIS);
if(!isParenthesis && precedence(ch) <= precedence(op))
{
postfix.append(" " + op + " ");
operators.pop();
}
else
break;
}
operators.push(ch);
}
}
while(!operators.isEmpty())
{
postfix.append(" " + operators.pop());
}
return postfix.toString().replaceAll(" ", " ");
}
//returns the precedence of an operator
private static int precedence(char operator)
{
if(operator == '/' || operator =='*' || operator == '%')
return 2;
else if(operator == '+' || operator == '-')
return 1;
else
return 0;
}
//checks if an expression has balanced parenthesis
public static Boolean isBalanced(String expression)
{
char expr[] = expression.toCharArray();
ArrayStack<Character> stack = new ArrayStack<Character>(100);
char ch;
for(int i = 0; i < expr.length; i++)
{
ch = expr[i];
if(ch == OPEN_PARENTHESIS)
stack.push(CLOSE_PARENTHESIS);
else if(ch == OPEN_ANGULAR)
stack.push(CLOSE_ANGULAR);
else if (ch == OPEN_CURLY)
stack.push(CLOSE_CURLY);
else if(ch == OPEN_SQUARE)
stack.push(CLOSE_SQUARE);
else if(ch == CLOSE_PARENTHESIS)
{
if(!stack.isEmpty() && stack.peek() != CLOSE_PARENTHESIS)
return false;
else if(stack.isEmpty())
return false;
else
stack.pop();
}
else if(ch == CLOSE_ANGULAR)
{
if(!stack.isEmpty() && stack.peek() != CLOSE_ANGULAR)
return false;
else if(stack.isEmpty())
return false;
else
stack.pop();
}
else if(ch == CLOSE_SQUARE)
{
if(!stack.isEmpty() && stack.peek() != CLOSE_SQUARE)
return false;
else if(stack.isEmpty())
return false;
else
stack.pop();
}
else if(ch == CLOSE_CURLY)
{
if(!stack.isEmpty() && stack.peek() != CLOSE_CURLY)
return false;
else if(stack.isEmpty())
return false;
else
stack.pop();
}
}
if(stack.isEmpty())
return true;
else
return false;
}
//evaluates a postfix expression
public static double evaluatePostfix(String postfix)
{
String tokens[] = postfix.split(" ");
ArrayStack<Double> operands = new ArrayStack<Double>(100);
Double val;
for(int i = 0; i < tokens.length; ++i)
{
try
{
val = Double.parseDouble(tokens[i]);
operands.push(val);
}catch(NumberFormatException e) //token is not a number, its an operator
{
Double op2 = operands.pop();
Double op1= operands.pop();
char op = tokens[i].charAt(0);
if(op == '+')
operands.push(op1 + op2);
else if(op == '-')
operands.push(op1 - op2);
else if(op == '/')
operands.push(op1 / op2);
else if(op == 'x')
operands.push(op1 * op2);
else if(op == '%')
operands.push(op1 % op2);
}
}
return operands.pop();
}
}
QuestionTwo.java
import java.util.Scanner;
public class QuestionTwo {
public static void main(String[] args) {
Scanner calcScan = new Scanner(System.in);
Boolean finished = false;
while(!finished)
{
System.out.println("Enter an infix expression: ");
String infix = calcScan.nextLine().trim();
System.out.println("Infix Expression: " + infix);
if(infix.equalsIgnoreCase("q"))
finished = true;
else
{
if(MyCalculator.isBalanced(infix))
{
System.out.println("The expression is balanced");
String postfix = MyCalculator.convertInfixToPostfix(infix);
System.out.println("Postfix Expression: " + postfix);
System.out.println("Result : " + MyCalculator.evaluatePostfix(postfix));
}
else
{
System.out.println("The expression is NOT balanced");
}
}
}
calcScan.close();
}
}
output
Enter an infix expression:
[4%3]+<6x7>/3-{8/2}
Infix Expression: [4%3]+<6x7>/3-{8/2}
The expression is balanced
Postfix Expression: 4 3 % 6 7 x 3 / + 8 2 / -
Result : 11.0
Enter an infix expression:
2+(3x4)
Infix Expression: 2+(3x4)
The expression is balanced
Postfix Expression: 2 3 4 x +
Result : 14.0
Enter an infix expression:
3x21
Infix Expression: 3x21
The expression is balanced
Postfix Expression: 3 21 x
Result : 63.0
Enter an infix expression:
q
Infix Expression: q
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.