Advertisement
Guest User

Untitled

a guest
Mar 26th, 2019
87
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 3.56 KB | None | 0 0
  1. /*
  2. CH08-320143
  3. Fraction.cpp
  4. Taiyr Begeyev
  5. t.begeyev@jacobs-university.de
  6. */
  7. #include <iostream>
  8. #include "Fraction.h"
  9. using namespace std;
  10.  
  11. // default constructor
  12. Fraction::Fraction() {
  13.     numerator = 0;
  14.     denominator = 1;
  15. }
  16.  
  17. // parametric constructors
  18. Fraction::Fraction(int a, int b) {
  19.     int tmp_gcd = gcd(abs(a), abs(b));
  20.     numerator = a / tmp_gcd;
  21.     denominator = b / tmp_gcd;
  22.  
  23.     // if both are negative, then make them positive
  24.     if (numerator < 0 && denominator < 0) {
  25.         numerator = abs(numerator);
  26.         denominator = abs(denominator);
  27.     }
  28.     cout << numerator << " " << denominator << endl;
  29. }
  30.  
  31. Fraction::Fraction(string s) {
  32.     int length = s.length();
  33.     // find the position of the /
  34.     int pos = s.find("/");
  35.     // base case
  36.     // if it is not found
  37.     if (pos == -1)
  38.         throw "Invalid data or logical error"; // exception
  39.  
  40.     // extract numerator, lefthand side from /
  41.     string num1 = s.substr(0, pos);
  42.     // extract denominator, righthand side from /
  43.     string num2 = s.substr(pos + 1, length - pos - 1);
  44.     // base case
  45.     // if we don't have anything between sign and /  or  after the second sign
  46.     if (num1.length() < 1 || num2.length() < 1)
  47.         throw "Invalid data or logical error"; // exception
  48.  
  49.     // extract signs
  50.     char sign1 = s[0];
  51.     char sign2 = s[pos + 1];
  52.  
  53.     // check if there are signs in front of the numbers
  54.     if (!isdigit(num1[0]) || !isdigit(num2[0])) {
  55.         if (sign1 == '+' || sign1 == '-') {
  56.             num1.erase(0, 1); // remove sign from the string
  57.         }
  58.         if (sign2 == '+' || sign2 == '-') {
  59.             num2.erase(0, 1); // remove sign from the string
  60.         }
  61.     }
  62.     // redefine the pos of / after deleting sign
  63.     pos = s.find("/");
  64.  
  65.     // check whether there is a number for numerator
  66.     bool isNumber = true;
  67.     for (int i = 0; i < pos; i++) {
  68.         if (!isdigit(num1[i])) {
  69.             isNumber = false;
  70.             break;
  71.         }
  72.     }
  73.     // if it is, then convert it to int
  74.     if (isNumber)
  75.         numerator = stoi(num1); // string to int
  76.     else
  77.         throw "Invalid data or logical error"; // exception
  78.    
  79.     // check whether there is a number for denominator
  80.     isNumber = true;
  81.     for (int i = 0; i < num2.length() - 1; i++) {
  82.         if (!isdigit(num2[i])) {
  83.             isNumber = false;
  84.             break;
  85.         }
  86.     }
  87.     // if it is, then convert it to int
  88.     if (isNumber)
  89.         denominator = stoi(num2); // string to int
  90.     else
  91.         throw "Invalid data or logical error"; // exception
  92.  
  93.     if (sign1 == '-')
  94.         numerator = -numerator;
  95.     if (sign2 == '-')
  96.         denominator = -denominator;
  97.  
  98.     // use greatest common divisor function to reduce fraction
  99.     int tmp_gcd = gcd(abs(numerator), abs(denominator));
  100.     numerator /= tmp_gcd;
  101.     denominator /= tmp_gcd;
  102.     cout << numerator << " " << denominator << endl;
  103. }
  104.  
  105. // copy constructor
  106. Fraction::Fraction(const Fraction& myFraction) {
  107.     numerator = myFraction.numerator;
  108.     denominator = myFraction.denominator;
  109. }
  110.  
  111. // destructor
  112. Fraction::~Fraction() {}
  113.  
  114. /* Operator overloading */
  115.  
  116. // relational operators
  117. bool Fraction::operator<(const Fraction& myFraction) {
  118.  
  119. }
  120.  
  121. int Fraction::gcd(int a, int b)
  122. {
  123.     int tmp_gcd = 1;
  124.  
  125.     // Implement GCD of two numbers;
  126.     for (int i = 1; i <= a && i <= b; i++)
  127.     {
  128.         if (a % i == 0 && b % i == 0)
  129.         {
  130.             tmp_gcd = i;
  131.         }
  132.     }
  133.  
  134.     return tmp_gcd;
  135. }
  136.  
  137. int Fraction::lcm(int a, int b)
  138. {
  139.     return a * b / gcd(a, b);
  140. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement