Reputation: 15
Hey guys so heres my question. I've got a class that manipulates a stack of Fractions and it is an RPN Evaluator. I'm newer to Java and only know basic data structures. This is a project for a class but I'm kinda bummed. I need to print out the expression that I used OR print out the expression until the RPN expression isn't valid denoted by valid = false/true
I have a specific way I have to print it out and ill give an example but I cannot figure out how to do it... I have a queue available to me to use but I must use both a stack and a queue. I realize the code is yuck but its because I haven't begun my cleanup of that class yet. Here is an example of the output I need if the inputs are as follows.... Note that the input is in quotes minus the quotes
Input************************************Output
Here is my class(I know it's sloppy...and the rules are very restrictive on what I can and can't use. No linked lists etc....)
import java.util.Scanner;
public class RpnEvaluator
{
private final int MAX_TOKEN = 20;
private Scanner stdin;
private int count = 0;
public void run() throws java.io.IOException
{
runOnce();
}
public boolean isOperator(String input)
{
String[] oprtr = {"+", "-", "*"};
for(String choice: oprtr)
if(choice.equals(input))
return true;
return false;
}
public boolean isOperation(String input)
{
if(input.startsWith("(", 0))
return true;
return false;
}
public Fraction runOperation(String choice, Fraction op2, Fraction op1)
{
Fraction newFract = new Fraction();
if(choice.equals("*"))
newFract = new Fraction(op1.times(op2));
else if(choice.equals("+"))
newFract = new Fraction(op1.plus(op2));
else if(choice.equals("-"))
newFract = new Fraction(op1.minus(op2));
return newFract;
}
public void runOnce()
{
String readIn = "";
boolean valid = true;
Fraction op1 = null, op2 = null, answer = null, myFract;
Queue myQueue = new Queue(MAX_TOKEN);
Stack myStack= new Stack(MAX_TOKEN);
stdin = new Scanner(System.in);
while(stdin.hasNext() && valid == true)
{
readIn = stdin.next();
if(readIn.equals("#"))
{
break;
}
else if(!isOperator(readIn) && isOperation(readIn))
{
myFract = new Fraction(readIn);
myStack.push(myFract);
}
else if(isOperator(readIn))
{
if(myStack.isEmpty())
valid = false;
else
op2 = (Fraction)myStack.pop();
if(myStack.isEmpty())
valid = false;
else
op1 = (Fraction)myStack.pop();
myStack.push(runOperation(readIn, op2, op1));
}
else
valid = false;
}
if(myStack.isEmpty())
valid = false;
else
answer = (Fraction)myStack.pop();
if(!myStack.isEmpty())
valid = false;
if(valid == false)
{
System.out.print("Expression " + ++count + ": ");
System.out.println("Invalid Expression");
}
else
{
System.out.println("Expression " + ++count + ": ");
System.out.println("The value is: " + answer.toString());
}
clear(myStack, myQueue);
}
public void clear(Stack myStack, Queue myQueue)
{
myStack.clear();
myQueue.clear();
}
}
Upvotes: 0
Views: 80
Reputation: 1468
Ok it is very unclear what you are asking and your code doesn't make too much sense and the fraction variable fraction is unknown.
and just a note here always clean up your code as write it since later you will never know where it is and will always be confusing.
try http://www.planet-source-code.com/vb/scripts/search.asp?lngWId=2.
for some examples, they always have what i need.
and if you are new to java and want to know more i would suggest the book Java Programming: From Problem Analysis to Program Design, it explains everything from main method to GUI design.
and in future please explain your problem in more detail, we have no idea what you are asking.
Thanks.
Upvotes: 0
Reputation: 310875
I've got a class that manipulates a stack of Fractions and it is an RPN Evaluator.
No it isn't. It's an attempt, but it doesn't handle parentheses at all, or operator precedence. You need to look up the Dijsktra shunting-yard algorithm. If this is an assignment I have no doubt that this algorithm was mentioned in class, probably at great length.
I realize the code is yuck but its because I haven't begun my cleanup of that class yet.
The best way to cleanup a class is not to fill it with dirt in the first place. Writing code that has to be subsequently removed is a double waste of time.
Upvotes: 1