Advertisement
Guest User

Untitled

a guest
Oct 23rd, 2017
65
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 1.20 KB | None | 0 0
  1. /*
  2. Brandon Altuchow
  3. ID: 0207256
  4. 10/17/17
  5. Journal 2 - Constructors
  6. */
  7. #include <iostream>
  8. #include <string>
  9. #include "Fraction.h"
  10.  
  11. using namespace std;
  12.  
  13. Fraction::Fraction()
  14. {
  15.     this->setFraction(1, 1);
  16. }
  17. Fraction::Fraction(int num, int den)
  18. {
  19.     this->setFraction(num, den);
  20. }
  21. Fraction::Fraction(string &f)
  22. {
  23.     this->setFraction(f.at(0) - 48, f.at(2) - 48);
  24. }
  25. void Fraction::setFraction(int n, int d)
  26. {
  27.     this->num = n;
  28.     this->den = d;
  29. }
  30. Fraction Fraction::add(const Fraction &f)
  31. {
  32.     Fraction tmp;
  33.     tmp.num = (this->num * f.den) + (f.num * this->den);
  34.     tmp.den = f.den * this->den;
  35.     return tmp;
  36. }
  37. Fraction Fraction::sub(const Fraction &f)
  38. {
  39.     Fraction tmp;
  40.     tmp.num = (this->num * f.den) - (f.num * this->den);
  41.     tmp.den = f.den * this->den;
  42.     return tmp;
  43. }
  44. Fraction Fraction::mul(const Fraction &f)
  45. {
  46.     Fraction tmp;
  47.     tmp.num = this->num * f.num;
  48.     tmp.den = this->den * f.den;
  49.     return tmp;
  50. }
  51. Fraction Fraction::div(const Fraction &f)
  52. {
  53.     Fraction tmp;
  54.     tmp.num = this->num * f.den;
  55.     tmp.den = this->den * f.num;
  56.     if (tmp.den < 0)
  57.     {
  58.         tmp.num *= -1;
  59.         tmp.den *= -1;
  60.     }      
  61.     return tmp;
  62. }
  63. void Fraction::printFraction()
  64. {
  65.     cout << this->num << "/" << this->den << endl;
  66. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement