Nattack

Untitled

Jul 20th, 2017
222
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 2.87 KB | None | 0 0
  1. import java.io.IOException;
  2. import java.util.*;
  3. import java.util.concurrent.Callable;
  4.  
  5. /**
  6.     Calc
  7.     @author R.J. Trenchard 100281657
  8.     @version 19072017-100281657
  9.     @see <a href="https://d2l.langara.bc.ca/d2l/le/calendar/88736/event/2558791/detailsview#2558791">a10: Calculator</a>
  10. */
  11. public class Calc implements Callable<Integer> {
  12.  
  13.     private final Scanner sc;
  14.     private final Deque<Integer> stack;
  15.  
  16.  
  17.     /**
  18.         Calc
  19.         @param String anIn an input string to interpret.
  20.     */
  21.     public Calc(String anIn) {
  22.         this(new Scanner(anIn));
  23.     }
  24.  
  25.     /**
  26.         Calc
  27.         @param Scanner aScanner an input scanner to interpret.
  28.     */
  29.     public Calc(Scanner aScanner) {
  30.         sc = aScanner;
  31.         stack = new LinkedList<Integer>();
  32.     }
  33.  
  34.     /**
  35.         call
  36.         evaluates the calculation
  37.         @throws RuntimeException when Scanner gives an IO exception
  38.         @throws IllegalArgumentException when an invalid operator is provided
  39.         @throws IllegalStateException if the stack is not a size of one by the end of the calculation, or if the stack runs out of elements.
  40.         @return the final result as type Integer
  41.     */
  42.     public Integer call() throws RuntimeException, IllegalArgumentException, IllegalStateException {
  43.         try {
  44.             while (sc.hasNext()) {
  45.                 if (sc.ioException() != null) {
  46.                     throw sc.ioException();
  47.                 } else if (sc.hasNextInt()) {
  48.                      stack.push(sc.nextInt());
  49.                 } else {
  50.                     stack.push(operate(sc.next(),stack.pop(),stack.pop()));
  51.                 }
  52.             }
  53.             if (stack.size() != 1) {
  54.                 throw new IllegalStateException("List elements not equal to one");
  55.             }
  56.         }
  57.         catch (IOException e) {                                     // IO exception found in Scanner
  58.             throw new RuntimeException(e);
  59.         }
  60.         catch (IllegalArgumentException e) {            // IllegalArgumentException found in operate
  61.             throw new IllegalArgumentException(e);
  62.         }
  63.         catch (NoSuchElementException e) {              // NoSuchElementException when stack is empty
  64.             throw new IllegalStateException(e);
  65.         }
  66.         catch (IllegalStateException e) {                   // IllegalStateException found in stack size
  67.             throw new IllegalStateException(e);
  68.         }
  69.         finally {
  70.             sc.close();
  71.         }
  72.         return stack.pop();
  73.     }
  74.  
  75.     /**
  76.         operate
  77.         performs an operation
  78.         @param String operator
  79.         @param int i2 the lhs value to operate on
  80.         @param int i1 the rhs value to operate on
  81.         @throws IllegalArgumentException Throws when an invalid operator is given.
  82.         @return the integer result
  83.     */
  84.     private static int operate(String operator, int i2, int i1) throws IllegalArgumentException {
  85.         if (operator.length() == 1) {
  86.             switch (operator.charAt(0)) {
  87.                 case '+':
  88.                     return i1 + i2;
  89.                 case '-':
  90.                     return i1 - i2;
  91.                 case '*':
  92.                     return i1 * i2;
  93.                 case '/':
  94.                     return i1 / i2;
  95.                 case '%':
  96.                     return i1 % i2;
  97.                 case '^':
  98.                     return (int)Math.pow(i1, i2);
  99.                 default:
  100.                     throw new IllegalArgumentException(operator);
  101.             }
  102.         } else {
  103.             throw new IllegalArgumentException(operator);
  104.         }
  105.     }
  106. }
Add Comment
Please, Sign In to add comment