Question: Write a program that implements a simple calculator with the following assumptions: 1. Unary operators (as in -2) are illegal. 2. All operations, including division,
Write a program that implements a simple calculator with the following assumptions:
1. Unary operators (as in -2) are illegal.
2. All operations, including division, are integer operations.
3. The input expression contains no spaces and no illegal characters.
4. The input expression is a syntactically correct infix expression (you dont have to verify this).
5. The input values range from 0-9. Multi-digit values are illegal.
6. The operators have their standard precedence.
The calculator should behave as follows, in a loop:
1. Prompt the user to enter an infix expression, with some details about what operators are valid.
2. Read in the expression.
3. Evaluate the result of the expression.
4. Display the result on the screen (or print out E if division by 0 occurred). The only valid characters are: 0123456789+-*/()Q. The Q is only valid if it appears alone on a line, and it signals to quit the program. You are not responsible for handling invalid expressions. You may implement some error-handling, or you may let your program crash or return invalid results.
Example run: Enter an infix expression using +, -, *, /, or () (or Q to quit): 5*(3+2) 25 2+2+2+2+2/2 9 ((5)-(9))/(4) -1 Q Exiting program.
Test your application with the supplied Stack ADT implementation. You may want to declare your reference variables as type StackInterface<> rather than a specific implementation to simplify switching implementations. Optionally, test your application with your own stack ADT implementation to make sure that your code works with multiple stack implementations.
************************************************************************************************************************************
package edu.wit.cs.comp2000; public class InfixEvaluator { // Calculates the result of the exp parameter and returns the result public static int evalExpression(String exp) { //STUB return -1; } public static void main(String[] args) { // Implement your console read/write loop in main // input... evalExpression("pass the input as a string"); // output... } } ************************************************************************************************************************************
package edu.wit.cs.comp2000;
/**
An interface for the ADT stack.
@author Frank M. Carrano
@author Timothy M. Henry
@version 4.0
*/
public interface StackInterface
{
/** Adds a new entry to the top of this stack.
@param newEntry An object to be added to the stack. */
public void push(T newEntry);
/** Removes and returns this stack's top entry.
@return The object at the top of the stack.
@throws EmptyStackException if the stack is empty before the operation. */
public T pop();
/** Retrieves this stack's top entry.
@return The object at the top of the stack.
@throws EmptyStackException if the stack is empty. */
public T peek();
/** Detects whether this stack is empty.
@return True if the stack is empty. */
public boolean isEmpty();
/** Removes all entries from this stack. */
public void clear();
} // end StackInterface
************************************************************************************************************************************
package edu.wit.cs.comp2000;
import java.util.Vector;
import java.util.EmptyStackException;
/**
A class of stacks whose entries are stored in a vector.
@author Frank M. Carrano
@author Timothy M. Henry
@version 4.0
*/
public final class VectorStack
{
private Vector
private boolean initialized = false;
private static final int DEFAULT_CAPACITY = 50;
private static final int MAX_CAPACITY = 10000;
public VectorStack()
{
this(DEFAULT_CAPACITY);
} // end default constructor
public VectorStack(int initialCapacity)
{
checkCapacity(initialCapacity);
stack = new Vector<>(initialCapacity); // Size doubles as needed
initialized = true;
} // end constructor
// 6.17
public void push(T newEntry)
{
checkInitialization();
stack.add(newEntry);
} // end push
// 6.18
public T peek()
{
checkInitialization();
if (isEmpty())
throw new EmptyStackException();
else
return stack.lastElement();
} // end peek
// 6.19
public T pop()
{
checkInitialization();
if (isEmpty())
throw new EmptyStackException();
else
return stack.remove(stack.size() - 1);
} // end pop
// 6.20
public boolean isEmpty()
{
return stack.isEmpty();
} // end isEmpty
// 6.20
public void clear()
{
stack.clear();
} // end clear
// Throws an exception if this object is not initialized.
private void checkInitialization()
{
if (!initialized)
throw new SecurityException ("VectorStack object is not initialized properly.");
} // end checkInitialization
// Throws an exception if the client requests a capacity that is too large.
private void checkCapacity(int capacity)
{
if (capacity > MAX_CAPACITY)
throw new IllegalStateException("Attempt to create a stack " +
"whose capacity exceeds " +
"allowed maximum.");
} // end checkCapacity
} // end VectorStack
Step by Step Solution
There are 3 Steps involved in it
Get step-by-step solutions from verified subject matter experts
