Results 1 to 3 of 3
- 02-16-2009, 10:29 PM #1
Member
- Join Date
- Jan 2009
- Posts
- 22
- Rep Power
- 0
Postfix Evaluation using a Stack, Help?
Alright so my task is to design a driver that accepts postfix expressions in the form of:
BC+
ACE^
A/B
etc.
First off, I must read input from the user in that form and get the integer value of the letters where:
public static int intValue(char letter)
{
int value =0;
if ( letter >= 'A' || letter <= 'Y')
value = (int) letter - 64;
else if (letter == 'Z')
value = 0;
return value;
}
Basically 'A' = 1, 'B' = 2, etc with 'Z' = 0.
I have gotten this far, but to no avail. I type in an expression such as BC+ and it does nothing but give me these errors:
Too few operands or too many operators.
Exception in thread "main" java.lang.StringIndexOutOfBoundsException: String index out of range: 2
at java.lang.String.charAt(String.java:687)
at StackClient.main(StackClient.java:52)
import java.util.Scanner;
public class StackClient {
/**
* @param args the command line arguments
*/
public static void main(String[] args)
{
char operator = 0;
String expression;
int result = 0, loc = 0;
System.out.println("\t\tProgram #2 Stacks\n\nThis program will read expressions from user input into\n" +
"a stack where the given expressions will be evaluated with different operators.\n" +
"It will also employ the use of push and pop to mutate and access the data in" +
"the stack.\n");
Stack theStack = new Stack(10);
Scanner scan = new Scanner(System.in);
System.out.println("Please enter an expression in postfix form using letters (A,B,C, etc.):");
expression = scan.next();
while(true)
{
if (theStack.isEmpty())
System.out.println("Too few operands or too many operators.");
else
theStack.pop();
if (theStack.isNotEmpty())
System.out.println("Too many operands.");
theStack.flush();
for (int i = loc; i <= expression.length(); i++)
theStack.push(theStack.intValue((char) expression.charAt(i)));
theStack.push(expression.charAt(loc));
if (expression.charAt(loc) == operator)
theStack.push(expression.charAt(loc));
switch (operator)
{
case '+': result = theStack.pop() + theStack.pop();
case '-': result = theStack.pop() - theStack.pop();
case '/': result = theStack.pop() / theStack.pop();
case '%': result = theStack.pop() % theStack.pop();
case '^': result = theStack.pop() * theStack.pop();
}
Where am I messing up? I need the program to take in those expressions and evaluate in postfix form. Do I have the loops wrong? Am I not searching the expression right? Any ideas?
- 02-16-2009, 10:38 PM #2
EDIT: I had an idea but was wrong when I tested. Sorry.
-MK12Last edited by MK12; 02-16-2009 at 10:45 PM.
Tell me if you want a cool Java logo avatar like mine and I'll make you one.
- 02-16-2009, 10:43 PM #3
Member
- Join Date
- Jan 2009
- Posts
- 22
- Rep Power
- 0
Similar Threads
-
[SOLVED] Boolean Expression Evaluation Framework
By priyanka.dandekar in forum Advanced JavaReplies: 8Last Post: 03-27-2010, 02:35 PM -
Help on Stack Implementation
By danver_2009 in forum New To JavaReplies: 1Last Post: 02-16-2009, 08:12 AM -
Postfix-Notation
By little_polarbear in forum New To JavaReplies: 9Last Post: 09-09-2008, 04:24 PM -
How to convert infix arithmetic expressions to postfix
By Java Tip in forum java.langReplies: 0Last Post: 04-15-2008, 07:36 PM -
How to parse postfix arithmetic expressions
By Java Tip in forum java.langReplies: 0Last Post: 04-15-2008, 07:36 PM


LinkBack URL
About LinkBacks
Reply With Quote
Bookmarks