Advertisement
bwukki

Rational Numbers Project (cs162)

Apr 28th, 2018
208
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 8.48 KB | None | 0 0
  1. //by Curtis Michels
  2. //main.cpp
  3.  
  4. #include <iostream>
  5. #include "Rationals.h"
  6.  
  7. using namespace std;
  8.  
  9. void test() {
  10.     Rational x{-1,2};
  11.     Rational y{5};
  12.  
  13.  
  14.     cout << x.toString() << endl;
  15.     cout << y.toString() << endl;
  16.  
  17.     Rational z{x+y};
  18.  
  19.     cout << "x + y = " << z.toString() << endl;
  20.     cout << "x - y = " << (x-y).toString() << endl;
  21. }
  22.  
  23. void test2() {
  24.     cout << GCD(54,888);
  25. }
  26.  
  27. void test3() {
  28.     Rational x{2,4};
  29.     Rational y{1,8};
  30.     cout << "x + y = " << (x+y).toString() << endl;
  31.     cout << "x * y = " << (x*y).toString() << endl;
  32.     cout << "x / y = " << (x/y).toString() << endl;
  33. }
  34.  
  35. void test4() {
  36.     Rational x{2,4};
  37.     Rational y{2,4};
  38.     Rational z{2,6};
  39.     Rational g{4,8};
  40.     Rational h{1};
  41.     cout << "x " << x << endl;
  42.     cout << "y " << y << endl;
  43.     cout << "z " << z << endl;
  44.     cout << "g " << g << endl;
  45.     cout <<endl;
  46.     cout << (x == y) << endl;
  47.     cout << (x == z) << endl;
  48.     cout << endl;
  49.     cout << (x == g) << endl;
  50.     cout << (x != y) << endl;
  51.     cout << (x != z) << endl;
  52.     cout << (x != g) << endl;
  53.     cout << endl;
  54.     cout << (x<h) << endl;
  55.     cout << (x>h) << endl;
  56.     cout << (x>y) << endl;
  57.     cout << (x<y) << endl;
  58.     cout << endl;
  59.     cout << (x<=h) << endl;
  60.     cout << (x>=h) << endl;
  61.     cout << (x>=y) << endl;
  62.     cout << (x<=y) << endl;
  63.  
  64. }
  65.  
  66. int main()
  67. {
  68.     test();
  69.     test2();
  70.     test3();
  71.     test4();
  72.     return 0;
  73. }
  74.  
  75.  
  76. //by Curtis Michels
  77. //Rationals.h
  78.  
  79. #ifndef RATIONALS_H
  80. #define RATIONALS_H
  81. #include <iostream>
  82. #include <sstream>
  83. #include <vector>
  84. #include <math.h>
  85. #include <stdexcept>
  86.  
  87. int GCD(const int, const int); //returns greatest common divisor between two numbers
  88.  
  89. class Rational {
  90.     friend std::ostream& operator<<(std::ostream&,const Rational);
  91. public:
  92.     Rational(); //default constructor, defaults to a numerator of 0 and denominator of 1
  93.     Rational(const int Numerator, const int Denominator); //constructor that takes ints for the num/denom
  94.     Rational(const int Numerator); //constructor that only takes numerator, default denominator is 1
  95.  
  96.     int getNumerator(); //returns numerator
  97.     int getDenominator(); //returns denominator
  98.  
  99.     void setNumerator(const int); //sets numerator, can be <= 0
  100.     void setDenominator(const int); //sets denominator, must be > 0
  101.  
  102.     std::string toString(); //outputs the rational number as a string
  103.  
  104.     Rational reduce(); //reduces the fraction, keeping the correct sign
  105.  
  106.     Rational operator+(const Rational); //properly adds two rational numbers
  107.     Rational operator-(const Rational); //properly subtracts two rational numbers
  108.     Rational operator*(const Rational); //multiplies two rationals
  109.     Rational operator/(const Rational); //divides two rationals
  110.     bool operator==(const Rational); //checks if two rationals are the same
  111.     bool operator!=(const Rational); //checks if two rationals are not the same
  112.     bool operator<(const Rational); //checks if a rational is less than another
  113.     bool operator>(const Rational); //checks if a rational is bigger than another
  114.     bool operator<=(const Rational); //checks if a rational is less than or equal to another
  115.     bool operator>=(const Rational); //checks if a rational is large than or equal to another
  116.  
  117. private:
  118.     int numerator{}; //can be any integer
  119.     int denominator{}; //cannot be <= 0
  120. };
  121.  
  122.  
  123.  
  124.  
  125. #endif // RATIONALS_H
  126.  
  127.  
  128. //by Curtis Michels
  129. //Rationals.cpp
  130.  
  131. #include "Rationals.h"
  132.  
  133. Rational::Rational():numerator{0}, denominator{1}{ //default constructor, initializes fraction to 0
  134. }
  135.  
  136. Rational::Rational(int inNumerator, int inDenominator){ //constructor that takes in two ints
  137.     setNumerator(inNumerator);
  138.     setDenominator(inDenominator);
  139. }
  140.  
  141. Rational::Rational(int inNumerator){ //constructor that takes in just the numerator
  142.     setNumerator(inNumerator);
  143.     setDenominator(1);
  144. }
  145.  
  146. std::ostream& operator<<(std::ostream& oin,Rational in) { //overloads left shift operator to work with cout and similar, depends on toString function
  147.     std::cout << in.toString();
  148.     return oin;
  149. }
  150.  
  151. int Rational::getNumerator() { //returns numerator
  152.     return numerator;
  153. }
  154.  
  155. int Rational::getDenominator() { //returns denominator
  156.     return denominator;
  157. }
  158.  
  159. void Rational::setNumerator(int in) { //sets numerator, can be anything including <= 0
  160.     numerator = in;
  161. }
  162.  
  163. void Rational::setDenominator(int in) { //sets denominator, must be > 0
  164.     if (in > 0) {
  165.             denominator = in;
  166.     }
  167.     else{
  168.         throw std::invalid_argument("Denominator must be > 0");
  169.     }
  170. }
  171.  
  172. std::string Rational::toString() { //returns a string with numerator and denominator separated by a slash
  173.     std::stringstream result;
  174.     result << getNumerator() << "/" << getDenominator();
  175.     return result.str();
  176. }
  177.  
  178. Rational Rational::reduce() { //reduces fraction by finding and dividing both numerator and denominator by greatest common factor
  179.     Rational result{};
  180.     int gcd = GCD(getNumerator(),getDenominator());
  181.     result.setNumerator(getNumerator()/gcd);
  182.     result.setDenominator(getDenominator()/gcd);
  183.     return result;
  184. }
  185.  
  186. int GCD(int in1, int in2) { //finds greatest common factor via recursive Euclidian Algorithm, non-member function
  187.     if (in1 < 0) { in1 = in1 * -1;} //this line ensures it always returns a positive number
  188.     if (in1 == in2) {return in1;} //doesn't bother doing anything more if both inputs are equal
  189.     int result{};
  190.     int divisor{};
  191.     int remainder{};
  192.     if(in1 > in2) {
  193.         divisor = in2;
  194.         remainder = in1%in2;
  195.     }
  196.     else {
  197.         divisor = in1;
  198.         remainder = in2%in1;
  199.     }
  200.     if (remainder == 0) {result = divisor;}
  201.     else {result = GCD(divisor,remainder);}
  202.     return result;
  203. }
  204.  
  205. Rational Rational::operator+(Rational in) { //adds and returns two rational numbers, reduces to most simplified form
  206.     Rational result{};
  207.     result.setNumerator(this->getNumerator() * in.getDenominator() + in.getNumerator() * this->getDenominator());
  208.     result.setDenominator(this->getDenominator() * in.getDenominator());
  209.     return result.reduce();
  210. }
  211.  
  212. Rational Rational::operator-(Rational in) { //subtracts and returns two rational numbers (Depends on + overloaded operator), reduces to most simplified form
  213.     Rational result{};
  214.     result.setNumerator(-1 * in.getNumerator());
  215.     result.setDenominator(in.getDenominator());
  216.     return (*this+result);
  217. }
  218.  
  219. Rational Rational::operator*(Rational in) { //multiplies two fractions, reduces to most simplified form
  220.     Rational result{};
  221.     result.setNumerator(this->getNumerator() * in.getNumerator());
  222.     result.setDenominator(this->getDenominator() * in.getDenominator());
  223.     return result.reduce();
  224.  
  225. }
  226.  
  227. Rational Rational::operator/(Rational in) { //divides two rational numbers, reduces to most simplified form, will throw an error if you try to divide by 0
  228.     Rational result{};
  229.     result.setNumerator(in.getDenominator()); //these two lines flip the numerator and denominator so we can multiply by the reciprocal
  230.     result.setDenominator(in.getNumerator());
  231.     result = result * *this;
  232. }
  233.  
  234. bool Rational::operator==(Rational in) { //compares if two rational numbers are the same, (reduces both to most simplified form BEFORE comparing)
  235.     bool result{false};
  236.     if (this->reduce().getNumerator() == in.reduce().getNumerator() && this->reduce().getDenominator() == in.reduce().getDenominator()) {result = true;}
  237.     return result;
  238. }
  239.  
  240. bool Rational::operator!=(Rational in) {
  241.     bool result{!(*this==in)};
  242.     return result;
  243. }
  244.  
  245. bool Rational::operator<(Rational in) { //checks if a rational is less than another
  246.     bool result{false};
  247.     double compare1{this->getNumerator()/this->getDenominator()};
  248.     double compare2{in.getNumerator()/in.getDenominator()};
  249.     if (compare1 < compare2) {result = true;}
  250.     return result;
  251.  
  252. }
  253.  
  254. bool Rational::operator>(const Rational in) { //checks if a rational is bigger than another
  255.     bool result{false};
  256.     if (*this != in) { result = !(*this<in); }
  257.     return result;
  258. }
  259.  
  260. bool Rational::operator<=(Rational in) { //checks if a rational is less than or = to another
  261.     bool result{false};
  262.     double compare1{this->getNumerator()/this->getDenominator()};
  263.     double compare2{in.getNumerator()/in.getDenominator()};
  264.     if (compare1 <= compare2) {result = true;}
  265.     return result;
  266.  
  267. }
  268.  
  269. bool Rational::operator>=(const Rational in) { //checks if a rational is bigger than or = to another
  270.     bool result{false};
  271.     result = !(*this<in);
  272.     return result;
  273. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement