0 Replies - 1202 Views - Last Post: 26 September 2010 - 09:08 PM Rate Topic: -----

#1 slixtrix  Icon User is offline

  • D.I.C Head

Reputation: 2
  • View blog
  • Posts: 62
  • Joined: 24-April 10

Java, postfix evaluator help

Posted 26 September 2010 - 09:08 PM

In my data structures class, we have to make 4 different classes (programs) one that basically is the stack class (we have to create our own), one to make sure equations are balanced with parenthesis, brackets or braces, one to evaluate postfix equations, and one to evaluate infix to postfix (but not evaluate the actual equation)...and all of these have to be able to read from a seperate text file (Scanners)

as of now, i have written the Stack class, and the verification class, which are below

class stackOb {
	int top;
	char stack[];
	int maxLen;
	
	public stackOb(int maxLen){
		stack = new char[maxLen];
		top = -1;
		this.maxLen = maxLen;
	}
	
	public char peek() {
		return stack[top];
	}
	
	public void push(char item){
		top++;
		stack[top]= item;
	}
	
	public char pop(){
		char a;
		a=stack[top];
		top--;
		return a;
	}
	
	public boolean empty(){
		if(top == -1){
			return true;
		}else{
			return false;
		}
	}
	
	public void reset(){
		top = -1;
	}
	
	public void showStack(){
		int j;
		
		System.out.println(" ");
		System.out.println("Stack contents ...");
		for(j = top;j > -1; j--) {
			System.out.println(stack[j]);
		}
		System.out.println(" ");
	}
	
	public void showStack0toTop(){
		int j;
		
		System.out.println(" ");
		System.out.println("Stack contents ...");
		for(j = 0; j <= top; j++) {
			System.out.print(stack[j]);
		}
		System.out.println(" ");
	}
	
}


and


import java.util.Scanner;
import java.io.*;
	
public class verifyEq {
	String fname;
	
	public verifyEq(){
		System.out.println("starting verifyEq() method...");
		getFileName();
		readFileContents();
	}
	
	public void getFileName() {
		Scanner in = new Scanner(System.in);
		System.out.println("Enter file name please.");
		fname = in.nextLine();
		System.out.println("You entered " + fname);
	}
	
	public void readFileContents() {
		boolean looping;
		DataInputStream in;
		String line;
		int j, len;
		stackOb myStack = new stackOb(10);
 
		try {
			in = new DataInputStream(new FileInputStream(fname));
			looping = true;
			while (looping) {
				if (null == (line = in.readLine())) {
					looping = false;
					in.close();
				}
				else {
					myStack.reset();
					System.out.println("line: " + line);
					len = line.length();
					for (j = 0; j < len; j++) {
 
						if ((myStack.empty()==true) && ((line.charAt(j) == ')')||(line.charAt(j) == ']')||(line.charAt(j) == '}'))) {
							System.out.println("NOT VALID! - (closer with no opener)");
							break;
						}
 
						else if ((line.charAt(j) == '(')||(line.charAt(j) == '[')||(line.charAt(j) == '{')) {
							myStack.push(line.charAt(j));
						}
 
						if (line.charAt(j) == ')') {
							if (myStack.peek() != '(') {
								System.out.println("INVALID! - (mismatch of opener/closer)");
								break;
							}
							else if (myStack.peek() == '(') {
								myStack.pop();
							}
						}
						else if (line.charAt(j) == ']') {
							if (myStack.peek() != '[') {
								System.out.println("INVALID! - (mismatch of opener/closer)");
								break;
							}
							else if (myStack.peek() == '[') {
								myStack.pop();
							}
						}
						else if (line.charAt(j) == '}') {
							if (myStack.peek() != '{') {
								System.out.println("INVALID! - (mismatch of opener/closer)");
								break;
							}
							else if (myStack.peek() == '{') {
								myStack.pop();
							}
						}
 
					}
 
					if (myStack.empty()==false) {
						System.out.print("NOT VALID! - ITEMS STILL ON STACK!");
						myStack.showStack();
					}
				}
			}
		}
 
		catch(IOException e) {
			System.out.println("Error " + e);
		} //end catch
	}
	
	public static void main(String[] args) {
		System.out.println("START main method...");
		verifyEq test = new verifyEq();
		System.out.println("END main method...");
	}
	
}


now im stuck on the postfix evaluation class which is below(i used the same format as the verifyEq class hoping i could reuse the code for the postfix evaluator):


import java.util.Scanner;
import java.io.*;

class postfixEvaluator {
	String fname;
	
	public postfixEvaluator(){
		System.out.println("starting postfixEvaluator() method...");
		getFileName();
		readFileContents();
	}
	
	public void getFileName() {
		Scanner in = new Scanner(System.in);
		System.out.println("Enter file name please.");
		fname = in.nextLine();
		System.out.println("You entered " + fname);
	}
	
	public void readFileContents() {
		boolean looping;
		DataInputStream in;
		String line;
		int j, len;
		stackOb myStack = new stackOb(10);
 
		try {
			in = new DataInputStream(new FileInputStream(fname));
			looping = true;
			while (looping) {
				if (null == (line = in.readLine())) {
					looping = false;
					in.close();
				}
				else {
					myStack.reset();
					System.out.println("line: " + line);
					len = line.length();
					
				}
			}
		}
 
		catch(IOException e) {
			System.out.println("Error " + e);
		} //end catch
	}
	
	public static void main(String[] args){
		System.out.println("Start!...");
		postfixEvaluator postfix = new postfixEvaluator();
		System.out.println("Finish!...");
	}
	
	
}


now, after the line that says

len = line.length();


im stuck on where to begin on this...any help would be greatly appreciated.

Is This A Good Question/Topic? 0
  • +

Page 1 of 1