Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- /*
- CH08-320143
- Fraction.cpp
- Taiyr Begeyev
- t.begeyev@jacobs-university.de
- */
- #include <iostream>
- #include "Fraction.h"
- using namespace std;
- // default constructor
- Fraction::Fraction() {
- numerator = 0;
- denominator = 1;
- }
- // parametric constructors
- Fraction::Fraction(int a, int b) {
- int tmp_gcd = gcd(abs(a), abs(b));
- numerator = a / tmp_gcd;
- denominator = b / tmp_gcd;
- // if both are negative, then make them positive
- if (numerator < 0 && denominator < 0) {
- numerator = abs(numerator);
- denominator = abs(denominator);
- }
- cout << numerator << " " << denominator << endl;
- }
- Fraction::Fraction(string s) {
- int length = s.length();
- // find the position of the /
- int pos = s.find("/");
- // base case
- // if it is not found
- if (pos == -1)
- throw "Invalid data or logical error"; // exception
- // extract numerator, lefthand side from /
- string num1 = s.substr(0, pos);
- // extract denominator, righthand side from /
- string num2 = s.substr(pos + 1, length - pos - 1);
- // base case
- // if we don't have anything between sign and / or after the second sign
- if (num1.length() < 1 || num2.length() < 1)
- throw "Invalid data or logical error"; // exception
- // extract signs
- char sign1 = s[0];
- char sign2 = s[pos + 1];
- // check if there are signs in front of the numbers
- if (!isdigit(num1[0]) || !isdigit(num2[0])) {
- if (sign1 == '+' || sign1 == '-') {
- num1.erase(0, 1); // remove sign from the string
- }
- if (sign2 == '+' || sign2 == '-') {
- num2.erase(0, 1); // remove sign from the string
- }
- }
- // redefine the pos of / after deleting sign
- pos = s.find("/");
- // check whether there is a number for numerator
- bool isNumber = true;
- for (int i = 0; i < pos; i++) {
- if (!isdigit(num1[i])) {
- isNumber = false;
- break;
- }
- }
- // if it is, then convert it to int
- if (isNumber)
- numerator = stoi(num1); // string to int
- else
- throw "Invalid data or logical error"; // exception
- // check whether there is a number for denominator
- isNumber = true;
- for (int i = 0; i < num2.length() - 1; i++) {
- if (!isdigit(num2[i])) {
- isNumber = false;
- break;
- }
- }
- // if it is, then convert it to int
- if (isNumber)
- denominator = stoi(num2); // string to int
- else
- throw "Invalid data or logical error"; // exception
- if (sign1 == '-')
- numerator = -numerator;
- if (sign2 == '-')
- denominator = -denominator;
- // use greatest common divisor function to reduce fraction
- int tmp_gcd = gcd(abs(numerator), abs(denominator));
- numerator /= tmp_gcd;
- denominator /= tmp_gcd;
- cout << numerator << " " << denominator << endl;
- }
- // copy constructor
- Fraction::Fraction(const Fraction& myFraction) {
- numerator = myFraction.numerator;
- denominator = myFraction.denominator;
- }
- // destructor
- Fraction::~Fraction() {}
- /* Operator overloading */
- // relational operators
- bool Fraction::operator<(const Fraction& myFraction) {
- }
- int Fraction::gcd(int a, int b)
- {
- int tmp_gcd = 1;
- // Implement GCD of two numbers;
- for (int i = 1; i <= a && i <= b; i++)
- {
- if (a % i == 0 && b % i == 0)
- {
- tmp_gcd = i;
- }
- }
- return tmp_gcd;
- }
- int Fraction::lcm(int a, int b)
- {
- return a * b / gcd(a, b);
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement