Difference between revisions of "CSC212 Homework 4 Solutions 2014"
Line 6: | Line 6: | ||
<br /> | <br /> | ||
<source lang="java"> | <source lang="java"> | ||
+ | /** | ||
+ | * evaluates RPN expressions known to be error free. | ||
+ | * @author D. Thiebaut | ||
+ | */ | ||
+ | |||
import java.util.Scanner; | import java.util.Scanner; | ||
import java.util.Stack; | import java.util.Stack; | ||
Line 11: | Line 16: | ||
@SuppressWarnings("unchecked") | @SuppressWarnings("unchecked") | ||
public class Hw4_1 { | public class Hw4_1 { | ||
− | static boolean debug = false; | + | static boolean debug = false; // used for debugging purposes |
+ | /** | ||
+ | * test validity of string as containing an operator | ||
+ | * @param word contains the operator (no extra spaces) | ||
+ | * @return true if +, *, -, or /, false otherwise | ||
+ | */ | ||
private static boolean isOperator( String word ) { | private static boolean isOperator( String word ) { | ||
String[] operators = new String[] { "+", "*", "-", "/" }; | String[] operators = new String[] { "+", "*", "-", "/" }; | ||
Line 21: | Line 31: | ||
} | } | ||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
+ | /** | ||
+ | * main entry point. | ||
+ | * @param args | ||
+ | */ | ||
public static void main(String[] args) { | public static void main(String[] args) { | ||
+ | // create a stack and a line scanner | ||
Stack stack = new Stack(); | Stack stack = new Stack(); | ||
Scanner lineScanner = new Scanner( System.in ); | Scanner lineScanner = new Scanner( System.in ); | ||
+ | // read lines until the end of input. | ||
while ( lineScanner.hasNextLine() ) { | while ( lineScanner.hasNextLine() ) { | ||
String line = lineScanner.nextLine(); | String line = lineScanner.nextLine(); | ||
+ | // skip blank lines | ||
if ( line.isEmpty() ) | if ( line.isEmpty() ) | ||
break; | break; | ||
+ | // create a word scanner for each line | ||
Scanner wordScanner = new Scanner( line ); | Scanner wordScanner = new Scanner( line ); | ||
while ( wordScanner.hasNext() ) { | while ( wordScanner.hasNext() ) { | ||
String word = wordScanner.next(); | String word = wordScanner.next(); | ||
+ | |||
//System.out.println( word ); | //System.out.println( word ); | ||
+ | |||
+ | // operate on operators | ||
if ( isOperator( word ) ) { | if ( isOperator( word ) ) { | ||
int top = (int) stack.pop(); | int top = (int) stack.pop(); | ||
Line 51: | Line 65: | ||
if ( word.equals( "*" ) ) stack.push( top * nextTop ); | if ( word.equals( "*" ) ) stack.push( top * nextTop ); | ||
if ( word.equals( "/" ) ) stack.push( nextTop / top ); | if ( word.equals( "/" ) ) stack.push( nextTop / top ); | ||
+ | |||
if (debug) System.out.println( "stack: " + stack ); | if (debug) System.out.println( "stack: " + stack ); | ||
continue; | continue; | ||
} | } | ||
+ | |||
+ | // otherwise, it's a number: push it. | ||
stack.push( (int) Integer.parseInt( word ) ); | stack.push( (int) Integer.parseInt( word ) ); | ||
+ | |||
if (debug) System.out.println( "stack: " + stack ); | if (debug) System.out.println( "stack: " + stack ); | ||
} | } | ||
+ | |||
+ | // no more words in this line: print the top of stack and clear it. | ||
System.out.println( stack.pop() ); | System.out.println( stack.pop() ); | ||
stack.clear(); | stack.clear(); | ||
} | } | ||
− | |||
} | } | ||
+ | } | ||
− | |||
</source> | </source> |