Guest User

Untitled

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