#include <iostream>
#include <string>
#include <stdlib.h>
#include <cstring>
using namespace std;
class Fraction
{
private:
int num, den; // numerator & demoninator
public:
Fraction() { set(0, 1); }
Fraction(int n, int d) { set(n, d); }
void set(int n, int d) { num = n; den = d; normalize(); }
int get_num() { return num; }
int get_den() { return den; }
Fraction add(Fraction other);
Fraction mult(Fraction other);
Fraction(Fraction const &src);
Fraction(char* s);
private:
void normalize(); // convert to standard form
int gcf(int a, int b); // greatest common factor
int lcm(int a, int b); // lowest common denominatro
};
int main()
{
Fraction f1(3, 4);
Fraction f2(f1);
Fraction f3 = f1.add(f2);
cout << "The value of f3 is ";
cout << f3.get_num() << "/";
cout << f3.get_den() << endl;
Fraction a = "1/2", b = "1/3";
//Fraction arr_of_fract[4] = {"1/2", "1/3", "3/4"};
cout << "The value of a is ";
cout << a.get_num() << "/";
cout << a.get_den() << endl;
return 0;
}
Fraction::Fraction(char* s)
{
int n = 0;
int d = 1;
char *p1 = strtok(s, "/, ");
char *p2 = strtok(NULL, "/, ");
if(p1 != NULL)
n = atoi(p1);
if(p2 != NULL)
d = atoi(p2);
set(n, d);
}
Fraction::Fraction(Fraction const &src)
{
cout << "Now calling copy constructor." << endl;
num = src.num;
den = src.den;
}
void Fraction::normalize()
{
// handle cases involving 0
if(den == 0 || num == 0)
{
num = 0;
den = 1;
}
// put neg, sign in numerator only
if(den < 0)
{
num *= -1;
den *= -1;
}
// factor out GCF from numerator and denominator
int n = gcf(num, den);
num = num / n;
den = den / n;
}
int Fraction::gcf(int a, int b)
{
if(b == 0)
return abs(a);
else
return gcf(b, a%b);
}
int Fraction::lcm(int a, int b)
{
int n = gcf(a, b);
return a / n * b;
}
Fraction Fraction::add(Fraction other)
{
Fraction fract;
int lcd = lcm(den, other.den);
int quot1 = lcd / den;
int quot2 = lcd / other.den;
fract.set(num * quot1 + other.num * quot2, lcd);
return fract;
}
Fraction Fraction::mult(Fraction other)
{
Fraction fract;
fract.set(num * other.num, den * other.den);
return fract;
}
In the code above I'm having trouble understanding most every line in the c-string constructor, Fraction(char* s)
Could someone give me a line by line explanation please? Thanks.