Aaaaa988

Untitled

Sep 27th, 2020
601
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. #include "TFrac.h"
  2. #include "pch.h"
  3. int TFrac::gcd(int a, int b) {
  4.     a = abs(a);
  5.     b = abs(b);
  6.     while (b) {
  7.         a %= b;
  8.         swap(a, b);
  9.     }
  10.     return a;
  11. }
  12.  
  13. TFrac::TFrac() {
  14.     this->numerator = 0;
  15.     this->denominator = 1;
  16. }
  17.  
  18. TFrac::TFrac(int a, int b) {
  19.     if (a < 0 && b < 0) {
  20.         a *= -1;
  21.         b *= -1;
  22.     }
  23.     else if (b < 0 && a > 0) {
  24.         b *= -1;
  25.         a *= -1;
  26.     }
  27.     else if (a == 0 && b == 0 || b == 0 || a == 0 && b == 1) {
  28.         this->numerator = 0;
  29.         this->denominator = 1;
  30.         return;
  31.     }
  32.     this->numerator = a;
  33.     this->denominator = b;
  34.     int gcdResult = gcd(a, b);
  35.     if (gcdResult > 1) {
  36.         this->numerator /= gcdResult;
  37.         this->denominator /= gcdResult;
  38.     }
  39. }
  40.  
  41. TFrac::TFrac(string fraction) {
  42.     if (fraction.find('/') == string::npos || fraction.size() < 3) {
  43.         this->numerator = 0;
  44.         this->denominator = 1;
  45.         return;
  46.     }
  47.  
  48.     string strNumerator;
  49.     string strDenominator;
  50.     unsigned int strPointer = 0;
  51.     while (fraction[strPointer] != '/') {
  52.         strNumerator.push_back(fraction[strPointer]);
  53.         strPointer++;
  54.     }
  55.     strPointer++;
  56.     while (strPointer < fraction.size()) {
  57.         strDenominator.push_back(fraction[strPointer]);
  58.         strPointer++;
  59.     }
  60.  
  61.     if (strDenominator.empty() || strNumerator.empty()) {
  62.         this->numerator = 0;
  63.         this->denominator = 1;
  64.         return;
  65.     }
  66.  
  67.     this->numerator = stoi(strNumerator);
  68.     this->denominator = stoi(strDenominator);
  69.  
  70.     int gcdResult = gcd(this->numerator, this->denominator);
  71.     if (gcdResult > 1) {
  72.         this->numerator /= gcdResult;
  73.         this->denominator /= gcdResult;
  74.     }
  75. }
  76.  
  77. TFrac::TFrac(const TFrac & anotherFrac) {
  78.     this->numerator = anotherFrac.numerator;
  79.     this->denominator = anotherFrac.denominator;
  80. }
  81.  
  82. TFrac TFrac::operator-() {
  83.     TFrac temp(-this->numerator, this->denominator);
  84.     return temp;
  85. }
  86.  
  87. TFrac TFrac::add(const TFrac & anotherFrac) {
  88.     TFrac temp;
  89.     temp.numerator = this->numerator * anotherFrac.denominator
  90.         + anotherFrac.numerator * this->denominator;
  91.     temp.denominator = this->denominator * anotherFrac.denominator;
  92.     int gcdResult = gcd(temp.numerator, temp.denominator);
  93.     if (gcdResult > 1) {
  94.         temp.numerator /= gcdResult;
  95.         temp.denominator /= gcdResult;
  96.     }
  97.     return temp;
  98. }
  99.  
  100. TFrac TFrac::multiply(const TFrac & anotherFrac) {
  101.     TFrac temp;
  102.     temp.numerator = this->numerator * anotherFrac.numerator;
  103.     temp.denominator = this->denominator * anotherFrac.denominator;
  104.     int gcdResult = gcd(temp.numerator, temp.denominator);
  105.     if (gcdResult > 1) {
  106.         temp.numerator /= gcdResult;
  107.         temp.denominator /= gcdResult;
  108.     }
  109.     return temp;
  110. }
  111.  
  112. TFrac TFrac::substract(const TFrac & anotherFrac) {
  113.     TFrac temp;
  114.     if (this->numerator == anotherFrac.numerator &&
  115.         this->denominator == anotherFrac.denominator) {
  116.         return TFrac(0, 1);
  117.     }
  118.     temp.numerator = this->numerator * anotherFrac.denominator
  119.         - anotherFrac.numerator * anotherFrac.numerator;
  120.     temp.denominator = this->denominator * anotherFrac.denominator;
  121.     int gcdResult = gcd(temp.numerator, temp.denominator);
  122.     if (gcdResult > 1) {
  123.         temp.numerator /= gcdResult;
  124.         temp.denominator /= gcdResult;
  125.     }
  126.     return temp;
  127. }
  128.  
  129. TFrac TFrac::divide(const TFrac & anotherFrac) {
  130.     if (anotherFrac.numerator == 0 && anotherFrac.denominator == 1) {
  131.         exit(1);
  132.     }
  133.     TFrac temp;
  134.     temp.numerator = this->numerator * anotherFrac.denominator;
  135.     temp.denominator = this->denominator * anotherFrac.numerator;
  136.     if (temp.numerator < 0 && temp.denominator < 0) {
  137.         temp.numerator *= -1;
  138.         temp.denominator *= -1;
  139.     }
  140.     int gcdResult = gcd(temp.numerator, temp.denominator);
  141.     if (gcdResult > 1) {
  142.         temp.numerator /= gcdResult;
  143.         temp.denominator /= gcdResult;
  144.     }
  145.     return temp;
  146. }
  147.  
  148. TFrac TFrac::square() {
  149.     return TFrac(this->numerator * this->numerator, this->denominator * this->denominator);
  150. }
  151.  
  152. TFrac TFrac::reverse() {
  153.     return TFrac(this->numerator > 0 ? this->denominator : -this->denominator,
  154.         this->numerator > 0 ? this->numerator : -this->numerator);
  155. }
  156.  
  157. bool TFrac::operator==(const TFrac & anotherFrac) {
  158.     if (this->numerator == anotherFrac.numerator && this->denominator == anotherFrac.denominator)
  159.         return true;
  160.     else
  161.         return false;
  162. }
  163.  
  164. bool TFrac::operator>(const TFrac & anotherFrac) {
  165.     if (float(this->numerator) / float(this->denominator)
  166.         > float(anotherFrac.numerator) / float(anotherFrac.denominator))
  167.         return true;
  168.     else
  169.         return false;
  170. }
  171.  
  172. TFrac::~TFrac() {}
  173.  
  174. int TFrac::getNumeratorNumber() {
  175.     return this->numerator;
  176. }
  177.  
  178. int TFrac::getDenominatorNumber() {
  179.     return this->denominator;
  180. }
  181.  
  182. string TFrac::getNumeratorString() {
  183.     return string(to_string(this->numerator));
  184. }
  185.  
  186. string TFrac::getDenominatorString() {
  187.     return string(to_string(this->denominator));
  188. }
  189.  
  190. string TFrac::getFractionString() {
  191.     return string(to_string(this->numerator) + '/' + to_string(this->denominator));
  192. }
RAW Paste Data