Need help with LinkedList.
I am trying to figure out what I did wrong in this assignment. My objective is to convert from infix to postfix and evaluate it. I can't seem to get the correct end result. The test class is correct and aside from the evaluateRPN class, everything else has no errors. Where am I going wrong and what should I do to correct it?
Code:
package arithmetic;
import java.util.Stack;
import java.util.EmptyStackException;
import java.util.Scanner;
class Constants
{
static char LEFT_NORMAL;
static char RIGHT_NORMAL;
}
class Arithmetic
{
Stack stk;
String expression, postfix;
int length;
Arithmetic (String s)
{
expression = s;
postfix = "";
length = expression.length();
stk = new Stack();
}
boolean isBalance()
{
boolean fail = false;
try
{
stk.clear();
stk.clear();
for(int i=0; i<length; i++)
{
char token = expression.charAt(i);
if(token == Constants.LEFT_NORMAL)
{
stk.push(new Character(token));
}
else if(token == Constants.RIGHT_NORMAL)
{
stk.pop();
}
}
}
catch (EmptyStackException e)
{
System.out.println(e.toString());
fail = true;
}
if (stk.empty() && !fail)
return true;
else
return false;
}
void postfixExpression()
{
Scanner scan = new Scanner(expression);
char current;
while (scan.hasNext())
{
String token = scan.next();
if (isNumber(token))
postfix = postfix + token + " ";
else
{
current = token.charAt(0);
if (isParentheses(current))
{
if (stk.empty() || current == Constants.LEFT_NORMAL)
{
stk.push(new Character (current));
}
else if (current == Constants.RIGHT_NORMAL)
{
try
{
Character ch = (Character)stk.pop();
char top = ch.charValue();
while (top != Constants.LEFT_NORMAL)
{
postfix = postfix + top + " ";
ch = (Character)stk.pop();
top = ch.charValue();
}
}
catch(EmptyStackException e)
{
}
}
}
else if (isOperator(current))
{
if(stk.empty())
stk.push(new Character(current));
else
{
try
{
char top = (Character)stk.peek();
boolean higher = hasHigherPrecedence(top, current);
while(top != Constants.LEFT_NORMAL && higher)
{
postfix = postfix + stk.pop() + " ";
top = (Character)stk.peek();
}
stk.push(new Character(current));
}
catch(EmptyStackException e)
{
stk.push(new Character(current));
}
}
}
}
}
try
{
while (!stk.empty())
{
postfix = postfix + stk.pop() + " ";
}
}
catch(EmptyStackException e)
{
}
}
boolean isNumber(String str)
{
try
{
Integer.parseInt(str);
return true;
}
catch(NumberFormatException e)
{
return false;
}
}
boolean isParentheses(char current)
{
if(current == Constants.LEFT_NORMAL || current == Constants.RIGHT_NORMAL)
{
return true;
}
else
{
return false;
}
}
boolean isBracket(char current)
{
if(current == Constants.LEFT_NORMAL || current == Constants.RIGHT_NORMAL)
{
return true;
}
else
{
return false;
}
}
boolean isCurly(char current)
{
if(current == Constants.LEFT_NORMAL || current == Constants.RIGHT_NORMAL)
{
return true;
}
else
{
return false;
}
}
boolean isOperator(char ch)
{
switch(ch)
{
case '+':
case '-':
case '*':
case '/':
return true;
default:
return false;
}
}
boolean hasHigherPrecedence(char top, char current)
{
int topPrecedence = 0;
int currentPrecedence = 0;
switch(top)
{
case '+':
case '-':
topPrecedence = 0;
break;
case '*':
case '/':
topPrecedence = 1;
break;
}
switch(current)
{
case '+':
case '-':
currentPrecedence = 0;
break;
case '*':
case '/':
currentPrecedence = 1;
break;
}
if(topPrecedence >= currentPrecedence)
{
return true;
}
else
{
return false;
}
}
String getPostfix()
{
stk.clear();
Scanner scan = new Scanner(postfix);
while(scan.hasNext())
{
String token = scan.next();
char currentChar = token.charAt(0);
if(isNumber(token))
{
stk.push(token);
}
else if(isOperator(currentChar))
{
int t1 = 0, t2 = 0;
try
{
t1 = Integer.parseInt((String)stk.pop());
t2 = Integer.parseInt((String)stk.pop());
}
catch(EmptyStackException e)
{
return "The expression was invalid!";
}
int t3 = 0;
switch(currentChar)
{
case '+':
t3 = t2 + t1;
break;
case '-':
t3 = t2 - t1;
break;
case '*':
t3 = t2 * t1;
break;
case '/':
t3 = t2 / t1;
break;
}
stk.push(new Integer(t3).toString());
}
}
if(stk.size() == 1)
{
return stk.pop().toString();
}
else
{
String val = "";
try
{
while(true)
{
val = val + stk.pop().toString();
}
}
catch(EmptyStackException e)
{
return val;
}
}
}
public void evaluateRPN()
{
Stack stk=new Stack();
char ch;
for(int i=0;i<expression.length();i++)
{
ch=expression.charAt(i);
if(((int)expression.charAt(i))>=0&&((int)expression.charAt(i))<=9)
stk.push(i);
if(ch=='+'||ch=='-'||ch=='*'||ch=='/'||ch=='%')
{
double t1=(double)(stk.pop());
double t2=(double)(stk.pop());
switch(ch)
{
case '+':value=t2+t1; break;
case '-':value=t2-t1; break;
case '*':value=t2*t1; break;
case '%':value=t2%t1; break;
case '/':value=t2/t1; break;
}
stk.push(value);
}
}
value=(double)sc.pop();
}
}
here is the test class
Code:
package arithmetic;
class RPN
{
public static void main(String[] arg)
{
String s[] = {"5 + ) * ( 2",
" ( { [ } ) ] ",
" 2 + ] - 3 * 5 [ ",
"[( 2 + 3 ) * 5] * 8 ",
"5 * 10 + ( 15 - 20 ) ) - 25",
"5 * { 10 + ( 15 - 20 ) } - 25",
" 5 + [ 5 * { 10 + ( 15 - 20 ) } - 25 ] * 9"
};
for (int i = 0; i < s.length; i++)
{
Arithmetic a = new Arithmetic(s[i]);
if (a.isBalance())
{
System.out.println("Expression " + s[i] + " is balanced\n");
a.postfixExpression();
System.out.println("The post fixed expression is : " + a.getPostfix());
a.evaluateRPN();
System.out.println("The result is : " + a.getResult() + "\n" );
}
else
System.out.println("Expression is not balanced\n");
}
}
}