1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146
|
#include <iostream>
//#include "Fract.h"
#include <iostream>
using namespace std;
// ==================================
class Fraction {
private:
int num, den; // Numerator and denominator.
public:
Fraction() {set(0, 1);}
Fraction(int n, int d) {set(n, d);}
Fraction(int n) {set(n, 1);}
Fraction(const Fraction &src);
void set(int n, int d)
{num = n; den = d; normalize();}
int get_num() const {return num;}
int get_den() const {return den;}
Fraction add(const Fraction &other);
Fraction operator+(const Fraction &other)
{return add(other);}
friend ostream &operator<<(ostream &os, Fraction &fr);
private:
void normalize(); // Convert to standard form.
int gcf(int a, int b); // Greatest Common Factor.
int lcm(int a, int b); // Lowest Common Denom.
};
//=============================================
// Note: Fract.cpp must be compiled and linked into
// the project.
class FloatFraction : public Fraction {
public:
FloatFraction(double n){set_float(n, 1);}
void set_float(double dn, double dd);
FloatFraction() {set(0, 1);}
FloatFraction(int n, int d) {set(n, d);}
FloatFraction(int n) {set(n, 1);}
FloatFraction(const FloatFraction &src)
{set(src.get_num(), src.get_den()); }
double get_float() {
double x = get_num();
return x / get_den();
}
};
int main() {
FloatFraction floatfract1(2.2), floatfract2;
Fraction fract1(1,1), fract2;
floatfract2 = floatfract1 + fract1;
cout << fract2;
return 0;
}
void FloatFraction::set_float(double dn, double dd){
int n, d;
n = static_cast<int> (dn * 1000);
d = static_cast<int> (dd * 1000);
// cout << "Setting: " << n << " / " << d << endl;
set(n, d);
}
//===========================================
Fraction::Fraction(Fraction const &src) {
num = src.num;
den = src.den;
}
// Normalize: put fraction into standard form, unique
// for each mathematically different value.
//
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;
}
// Greatest Common Factor
//
int Fraction::gcf(int a, int b){
if (b == 0)
return abs(a);
else
return gcf(b, a%b);
}
// Lowest Common Multiple
//
int Fraction::lcm(int a, int b){
int n = gcf(a, b);
return a / n * b;
}
Fraction Fraction::add(const Fraction &other) {
int lcd = lcm(den, other.den);
int quot1 = lcd/den;
int quot2 = lcd/other.den;
return Fraction(num * quot1 + other.num * quot2,
lcd);
}
/*Fraction Fraction::add(const Fraction &other) {
int lcd = lcm(get_den(), other.get_den());
int quot1 = lcd/get_den();
int quot2 = lcd/other.get_den();
return Fraction(get_num() * quot1 + other.get_num() * quot2,
lcd);
}*/
// ---------------------------------------------------
// FRACTION CLASS FRIEND FUNCTION
ostream &operator<<(ostream &os, Fraction &fr) {
os << fr.num << "/" << fr.den;
return os;
}
//=================================================
|