From 6856920fa330680df01dea669d9c762de6532fc4 Mon Sep 17 00:00:00 2001 From: josh Date: Fri, 21 Mar 2008 20:50:52 +0000 Subject: [PATCH] added proj2 .java files git-svn-id: svn://anubis/gvsu@75 45c1a28c-8058-47b2-ae61-ca45b979098e --- cs621/proj2/Calculator.java | 84 ++++++++++++++++++++++++ cs621/proj2/MyApp.java | 37 +++++++++++ cs621/proj2/Rational.java | 116 +++++++++++++++++++++++++++++++++ cs621/proj2/RationalStack.java | 60 +++++++++++++++++ 4 files changed, 297 insertions(+) create mode 100644 cs621/proj2/Calculator.java create mode 100644 cs621/proj2/MyApp.java create mode 100644 cs621/proj2/Rational.java create mode 100644 cs621/proj2/RationalStack.java diff --git a/cs621/proj2/Calculator.java b/cs621/proj2/Calculator.java new file mode 100644 index 0000000..6da9fe2 --- /dev/null +++ b/cs621/proj2/Calculator.java @@ -0,0 +1,84 @@ + +import java.util.StringTokenizer; + +/** + * Calculator implements a RPN calculator that can act on rational values + */ +public class Calculator +{ + private RationalStack stack = new RationalStack(); + + /** + * pushOperand adds the given token to the top of the RationalStack object + * @param val the token to add + */ + private void pushOperand(String val) + { + StringTokenizer tokens = new StringTokenizer(val, "/"); + if (tokens.hasMoreElements()) + { + int num = Integer.parseInt(tokens.nextToken()); + int denom = 1; + if (tokens.hasMoreElements()) + { + denom = Integer.parseInt(tokens.nextToken()); + } + stack.push(new Rational(num, denom)); + } + } + + /** + * doOperator performs the action specified by op on the top two elements + * of the RationalStack object + * @param op the operator to perform + */ + private void doOperator(String op) + { + if (stack.size() < 2) + return; + + Rational two = stack.pop(); + Rational one = stack.pop(); + Rational result = new Rational(); + if (op.equals("+")) + result = one.add(two); + else if (op.equals("-")) + result = one.subtract(two); + else if (op.equals("*")) + result = one.multiply(two); + else if (op.equals("\\")) + result = one.divide(two); + stack.push(result); + } + + /** + * run will evaluate the RPN expression and print its result + * @param expression the expression to evaluate + */ + public void run(String expression) + { + StringTokenizer tokens = new StringTokenizer(expression, " "); + while (tokens.hasMoreTokens()) + { + String item = tokens.nextToken(); + if (item.equals("+") || item.equals("-") || + item.equals("*") || item.equals("\\")) + { + doOperator(item); + } + else + { + pushOperand(item); + } + } + + if (stack.size() != 1) + { + System.out.println("Warning: malformed expression"); + } + if (!stack.isEmpty()) + { + System.out.println("Result is: " + stack.pop()); + } + } +} diff --git a/cs621/proj2/MyApp.java b/cs621/proj2/MyApp.java new file mode 100644 index 0000000..81d756d --- /dev/null +++ b/cs621/proj2/MyApp.java @@ -0,0 +1,37 @@ + +import java.io.BufferedReader; +import java.io.InputStreamReader; + +/** + * MyApp is the driver class for Calculator + */ +public class MyApp +{ + /** + * main is the entry point of the program + */ + public static void main (String args[]) + { + BufferedReader in = new BufferedReader(new InputStreamReader(System.in)); + Calculator myCalc = new Calculator(); + + /* loop reading input lines until the user is finished */ + for (;;) + { + System.out.println("Enter rational RPN expression (enter empty line to quit):"); + String line; + try + { + line = in.readLine(); + } + catch (Exception e) + { + System.out.println("Problem reading a line!"); + return; + } + if (line.equals("")) + break; + myCalc.run(line); + } + } +} diff --git a/cs621/proj2/Rational.java b/cs621/proj2/Rational.java new file mode 100644 index 0000000..9a5f83f --- /dev/null +++ b/cs621/proj2/Rational.java @@ -0,0 +1,116 @@ + +/** + * Rational is a class which can store a rational number of the form n/d + * where n and d are integers, n is the numerator, and d is the denominator + */ +public class Rational +{ + private int _numerator; + private int _denominator; + + /** + * This default constructor creates a Rational with value 0 + */ + public Rational() + { + this(0, 1); + } + + /** + * This constructor creates a Rational with value numerator/denominator + * @param numerator the numerator of the Rational + * @param denominator the denominator of the Rational + */ + public Rational(int numerator, int denominator) + { + _numerator = numerator; + _denominator = denominator; + reduce(); + } + + /** + * add adds this Rational to another + * @param other the Rational to add to this one + * @return the result of the addition + */ + public Rational add(Rational other) + { + return new Rational(_numerator * other._denominator + + other._numerator * _denominator, + _denominator * other._denominator); + } + + /** + * subtract subtracts another Rational from this one + * @param other the other Rational to subtract from this one + * @return the result of the subtraction + */ + public Rational subtract(Rational other) + { + return new Rational(_numerator * other._denominator - + other._numerator * _denominator, + _denominator * other._denominator); + } + + /** + * multiply multiplies this Rational with another + * @param other the other Rational to multiply with this one + * @return the result of the multiplication + */ + public Rational multiply(Rational other) + { + return new Rational(_numerator * other._numerator, + _denominator * other._denominator); + } + + /** + * divide divides another Rational into this one + * @param other the other Rational to divide into this one + * @return the result of the division + */ + public Rational divide(Rational other) + { + return new Rational(_numerator * other._denominator, + _denominator * other._numerator); + } + + /** + * gcd calculates the greatest common divisor between the + * numerator and the denominator + * @return the greatest common divisor + */ + private int gcd() + { + int m = _numerator; + int n = _denominator; + int r; + while (n != 0) + { + r = m % n; + m = n; + n = r; + } + return m; + } + + /** + * reduce reduces the Rational to simplest terms using the gcd + * @return itself + */ + public Rational reduce() + { + int thegcd = gcd(); + _numerator = _numerator / thegcd; + _denominator = _denominator / thegcd; + return this; + } + + /** + * toString returns a string representation of the Rational in n/m form + * @return a string representation of the Rational in n/m form + */ + public String toString() + { + return _numerator + (_denominator == 1 ? "" : "/" + _denominator); + } +} diff --git a/cs621/proj2/RationalStack.java b/cs621/proj2/RationalStack.java new file mode 100644 index 0000000..1020458 --- /dev/null +++ b/cs621/proj2/RationalStack.java @@ -0,0 +1,60 @@ + +import java.util.Vector; + +/** + * RationalStack provides a stack collection of Rational objects + */ +public class RationalStack +{ + private Vector _data = new Vector(); + + /** + * pop will remove and return the Rational object from the top of the stack + * @return the Rational at the top of the stack + */ + public Rational pop() + { + int sz = _data.size(); + if (sz > 0) + { + Rational r = _data.remove(sz-1); + return r; + } + return null; + } + + /** + * push will add a new Rational object to the top of the stack + * @param value the Rational object to add to the stack + */ + public void push(Rational value) + { + _data.add(value); + } + + /** + * isEmpty will return true if there are no Rational elements on the stack + * @return true if there are no Rational elements on the stack + */ + public boolean isEmpty() + { + return _data.isEmpty(); + } + + /** + * clear will remove all Rational objects from the stack + */ + public void clear() + { + _data.clear(); + } + + /** + * size returns the number of Rational objects in the stack + * @return the number of Rational objects in the stack + */ + public int size() + { + return _data.size(); + } +}