Hey guys,
Im having a really hard time with this, I've been searching and trying to work at this for hours but cant get anything to work. If you guys could help it would be amazing. Essentially what I need to do is take this code thats been provided to me, and alter it to allow for Parentheses to be used in the Input.
Also, I need to have it so it will point out any errors when Infix is input, that is to say for the code to point out where exactly the error has occurred and what is wrong with it.
I've tried searching here, and googled for hours. Thank you very much in advance to anyone who can help!
When I try to input this into Eclipse right now it tells me that:
"Exception in thread "main" java.lang.Error: Unresolved compilation problems:
Cannot instantiate the type ArrayStack<String>
The method isEmpty() is undefined for the type ArrayStack<String>
The method push(String) is undefined for the type ArrayStack<String>
The method peek() is undefined for the type ArrayStack<String>
The method pop() is undefined for the type ArrayStack<String>
The method push(String) is undefined for the type ArrayStack<String>
The method isEmpty() is undefined for the type ArrayStack<String>
The method pop() is undefined for the type ArrayStack<String>
at PostedInfixToPostfix.<init>(PostedInfixToPostfix.java:17)
at Translate.main(Translate.java:14)"
Here is the code:
Translate.Java
import java.util.Scanner;
public class Translate {
public static void main (String[] args) {
String expression, again;
String result;
try {
Scanner in = new Scanner(System.in);
do {
InfixToPostfix translator = new InfixToPostfix();
System.out.println ("Enter a valid infix expression: ");
expression = in.nextLine();
result = translator.translate(expression);
System.out.println();
System.out.println ("This expression in postfix: " + result);
System.out.print ("Translate another expression [Y/N]? ");
again = in.nextLine();
System.out.println();
}
while (again.equalsIgnoreCase("y"));
}
catch (Exception IOException) {
System.out.println("Input exception reported");
}
}
}
InfixToPostfix.Java
import java.util.StringTokenizer;
public class PostedInfixToPostfix {
private final String ADD = "+";
private final String SUBTRACT = "-";
private final String MULTIPLY = "*";
private final String DIVIDE = "/";
private final int ADD_PREC = 2;
private final int MUL_PREC = 4;
private ArrayStack<String> stack;
public PostedInfixToPostfix() {
stack = new ArrayStack<String>();
}
public String translate (String expr) {
String token, outputString = "" ;
StringTokenizer tokenizer = new StringTokenizer (expr);
while (tokenizer.hasMoreTokens()) {
token = tokenizer.nextToken();
System.out.println("token is " + token) ;
if (!isOperator(token)) {
outputString += token + " " ;
}
else {
if (stack.isEmpty()) {
stack.push(token) ;
}
else {
if (precedence(stack.peek()) > precedence(token)) {
outputString += stack.pop() + " ";
}
stack.push(token) ;
}
}
}
while (!stack.isEmpty()) {
outputString += stack.pop() + " " ;
}
return outputString ;
}
private int precedence(String token) {
if (token.equals(ADD) || token.equals(SUBTRACT)) {
return ADD_PREC ;
}
else {
return MUL_PREC ;
}
}
private boolean isOperator (String token) {
return (token.equals(ADD) || token.equals(SUBTRACT) || token.equals(MULTIPLY) || token.equals(DIVIDE));
}
}
And PostfixEvaluator.Java
import java.util.StringTokenizer;
public class PostfixEvaluator
{
/** constant for addition symbol */
private final char ADD = '+';
/** constant for subtraction symbol */
private final char SUBTRACT = '-';
/** constant for multiplication symbol */
private final char MULTIPLY = '*';
/** constant for division symbol */
private final char DIVIDE = '/';
/** the stack */
private ArrayStack<Integer> stack;
/**
* Sets up this evalutor by creating a new stack.
*/
public PostfixEvaluator()
{
stack = new ArrayStack<Integer>();
}
/**
* Evaluates the specified postfix expression. If an operand is
* encountered, it is pushed onto the stack. If an operator is
* encountered, two operands are popped, the operation is
* evaluated, and the result is pushed onto the stack.
* @param expr String representation of a postfix expression
* @return int value of the given expression
*/
public int evaluate (String expr)
{
int op1, op2, result = 0;
String token;
StringTokenizer tokenizer = new StringTokenizer (expr);
while (tokenizer.hasMoreTokens())
{
token = tokenizer.nextToken();
if (isOperator(token))
{
op2 = (stack.pop()).intValue();
op1 = (stack.pop()).intValue();
result = evalSingleOp (token.charAt(0), op1, op2);
stack.push (new Integer(result));
}
else
stack.push (new Integer(Integer.parseInt(token)));
}
return result;
}
/**
* Determines if the specified token is an operator.
* @param token String representing a single token
* @return boolean true if token is operator
*/
private boolean isOperator (String token)
{
return ( token.equals("+") || token.equals("-") ||
token.equals("*") || token.equals("/") );
}
/**
* Peforms integer evaluation on a single expression consisting of
* the specified operator and operands.
* @param operation operation to be performed
* @param op1 the first operand
* @param op2 the second operand
* @return int value of the expression
*/
private int evalSingleOp (char operation, int op1, int op2)
{
int result = 0;
switch (operation)
{
case ADD:
result = op1 + op2;
break;
case SUBTRACT:
result = op1 - op2;
break;
case MULTIPLY:
result = op1 * op2;
break;
case DIVIDE:
result = op1 / op2;
}
return result;
}
}