Expression

Expression - /* * CS20S08 PA2 * Esra * 30 May 2008 */ import

Info iconThis preview shows pages 1–2. Sign up to view the full content.

View Full Document Right Arrow Icon
/* * CS20S08 PA2 * Esra * 30 May 2008 */ import java.util.StringTokenizer; import java.util.Stack; public class Expression { private Stack<Nodee> generalStack; private Stack<String> operatorStack; private Nodee rootOfExpression; public Expression(String s) { if(isValidParenthesis(s)){ //If (assumption: enough space around parentheses) then, no need this for-loop for(int i=0; i< s.length(); i++){ if( s.charAt(i)=='('){ s = s.substring(0, i) + " ( " + s.substring(i+1); i+=2; } else if( s.charAt(i)==')'){ s = s.substring(0, i) + " ) " + s.substring(i+1); i+=2; } }//end for generalStack = new Stack<Nodee>(); operatorStack = new Stack<String>(); String temp; StringTokenizer st = new StringTokenizer(s); while(st.hasMoreTokens()){ temp=st.nextToken(); if(temp.equals("(")){//lp operatorStack.push(temp); } else if(temp.equals(")")){//rp String top = operatorStack.peek(); while(!top.equals("(")){ String op = operatorStack.pop(); top = operatorStack.peek(); Nodee right = generalStack.pop(); Nodee left = generalStack.pop();
Background image of page 1

Info iconThis preview has intentionally blurred sections. Sign up to view the full version.

View Full DocumentRight Arrow Icon
Image of page 2
This is the end of the preview. Sign up to access the rest of the document.

Page1 / 4

Expression - /* * CS20S08 PA2 * Esra * 30 May 2008 */ import

This preview shows document pages 1 - 2. Sign up to view the full document.

View Full Document Right Arrow Icon
Ask a homework question - tutors are online