Difference between revisions of "CSC212 Homework 4 Solutions 2014"
(Created page with "--~~~~ ---- <onlydft> =Problem 1= <br /> <source lang="java"> import java.util.Scanner; import java.util.Stack; @SuppressWarnings("unchecked") public class Hw4_1 { static b...") |
|||
Line 71: | Line 71: | ||
<br /> | <br /> | ||
<source lang="java"> | <source lang="java"> | ||
+ | /** | ||
+ | * evaluates RPN arithmetic expressions on ints using a stack. | ||
+ | * | ||
+ | */ | ||
import java.util.Scanner; | import java.util.Scanner; | ||
import java.util.Stack; | import java.util.Stack; | ||
Line 76: | Line 80: | ||
public class Hw4_2 { | public class Hw4_2 { | ||
− | + | static boolean debug = false; | |
− | + | ||
− | + | /** | |
− | + | * given s tring containing only a possible operator, returns true if it's a valid | |
− | + | * one (+, *, -, /) and false otherwise | |
− | + | * @param word the string containing the parameter (no extra space allowed) | |
− | + | * @return true if *, +, - or /, false otherwise | |
− | + | */ | |
− | + | private static boolean isOperator( String word ) { | |
− | + | String[] operators = new String[] { "+", "*", "-", "/" }; | |
− | + | for ( int i=0; i<operators.length; i++ ) | |
− | + | if ( word.equals(operators[i]) ) | |
− | + | return true; | |
+ | return false; | ||
+ | } | ||
+ | |||
+ | /** | ||
+ | * displays the word ERROR. | ||
+ | * @param i used in debugging mode, to indicate where the error occured. | ||
+ | */ | ||
+ | private static void error( int i ) { | ||
+ | System.out.println( "ERROR " );// + i ); | ||
+ | } | ||
− | + | /** | |
− | + | * main entry point. | |
− | + | * @param args | |
− | + | */ | |
− | + | public static void main(String[] args) { | |
− | + | boolean error; | |
− | + | Stack stack = new Stack(); | |
− | + | Scanner lineScanner = new Scanner( System.in ); | |
− | + | ||
− | + | // scan the lines of input | |
− | + | while ( lineScanner.hasNextLine() ) { | |
− | + | String line = lineScanner.nextLine().trim(); | |
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | + | // skip blank lines | |
+ | if ( line.isEmpty() ) | ||
+ | continue; | ||
+ | |||
+ | // scan each word in a line and either pushes it in stack if an int | ||
+ | // or pop numbers from the stack to evaluate operation. | ||
+ | error = false; | ||
+ | Scanner wordScanner = new Scanner( line ); | ||
+ | while ( wordScanner.hasNext() ) { | ||
+ | String word = wordScanner.next(); | ||
+ | |||
+ | // --- operator? --- | ||
+ | if ( isOperator( word ) ) { | ||
+ | if ( stack.size() < 2 ) { | ||
+ | error( 1 ); | ||
+ | if ( wordScanner.hasNextLine() ) wordScanner.nextLine(); | ||
+ | error = true; | ||
+ | break; | ||
+ | } | ||
+ | int top = (int) stack.pop(); | ||
+ | int nextTop = (int) stack.pop(); | ||
+ | if ( word.equals( "+" ) ) stack.push( top + nextTop ); | ||
+ | if ( word.equals( "-" ) ) stack.push( nextTop - top ); | ||
+ | if ( word.equals( "*" ) ) stack.push( top * nextTop ); | ||
+ | if ( word.equals( "/" ) ) { | ||
+ | if ( top != 0 ) | ||
+ | stack.push( nextTop / top ); | ||
+ | else { | ||
+ | if ( wordScanner.hasNextLine() ) wordScanner.nextLine(); | ||
+ | stack.clear(); | ||
+ | error( 2 ); | ||
+ | error = true; | ||
+ | break; | ||
+ | } | ||
+ | } | ||
+ | if (debug) System.out.println( "stack: " + stack ); | ||
+ | continue; | ||
+ | } | ||
+ | |||
+ | // --- no, then must be number --- | ||
+ | try { | ||
+ | stack.push( (int) Integer.parseInt( word ) ); | ||
+ | } | ||
+ | catch ( NumberFormatException e ) { | ||
+ | error( 3 ); | ||
+ | stack.clear(); | ||
+ | error = true; | ||
+ | break; | ||
+ | } | ||
+ | |||
+ | if (debug) System.out.println( "stack: " + stack ); | ||
+ | } | ||
+ | |||
+ | if ( error ) | ||
+ | continue; | ||
+ | |||
+ | if ( stack.size() != 1 ) { | ||
+ | error( 4 ); | ||
+ | stack.clear(); | ||
+ | } | ||
+ | else | ||
+ | System.out.println( stack.pop() ); | ||
+ | |||
+ | stack.clear(); | ||
+ | } | ||
+ | } | ||
+ | } | ||
− | |||
− | |||
</source> | </source> |