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 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161
|
#include <iostream>
using namespace std;
#include "Fraction.h"
#include<cmath>
// Test code for Fraction class
int main()
{
Fraction f1(5, 3);
Fraction f2(7);
Fraction f3;
Fraction f5(55, 33);
// BASIC ASSIGNEMENT OPERATOR
// Fraction& operator=(const Fraction&);
cout << "Assingment (Before): f3 = " << f3 << ". f1 = " << f1 << endl;
f3 = f1;
cout << "Assingment (After): f3 = " << f3 << ". f1 = " << f1 << endl;
f1 = Fraction(5, 12);
// UNARY ARITHMETIC OPERATORS
// Fraction operator-(); // Operand 'this' implicit
f3 = -f1;
cout << "Unary Minus: f3 = " << f3 << ". f1 = " << f1 << endl;
// Fraction operator+();
// Fraction operator--(); // Pre-decrement. Dividendo
f5 = Fraction::sc_fUnity;
cout << "Pre-Decrement (Before): f3 = " << f3 << ". f5 = " << f5 << endl;
f3 = --f1;
cout << "Pre-Decrement (After): f3 = " << f3 << ". f1 = " << f1 << endl;
// Fraction operator--(int); // Post-decrement. Lazy Dividendo
f3 = Fraction::sc_fUnity;
cout << "Post-Decrement (Before): f3 = " << f3 << ". f1 = " << f1 << endl;
f3 = f1--;
cout << "Post-Decrement (After): f3 = " << f3 << ". f1 = " << f1 << endl;
// Fraction operator++(); // Pre-increment. Componendo
f3 = Fraction::sc_fUnity;
cout << "Pre-Increment (Before): f3 = " << f3 << ". f1 = " << f1 << endl;
f3 = ++f1;
cout << "Pre-Increment (After): f3 = " << f3 << ". f1 = " << f1 << endl;
// Fraction operator++(int); // Post-increment. Lazy Componendo
f3 = Fraction::sc_fUnity;
cout << "Post-Increment (Before): f3 = " << f3 << ". f1 = " << f1 << endl;
f3 = f1++;
cout << "Post-Increment (After): f3 = " << f3 << ". f1 = " << f1 << endl;
// ADVANCED ASSIGNEMENT OPERATORS
// Fraction& operator+=(const Fraction&);
f1 = Fraction(5, 12);
f2 = Fraction(7, 18);
f3 = f2;
f2 += f1;
cout << "+=: f2 = " << f2 << ". f1 = " << f1 << ". f2 (before) = " << f3 << endl;
f3 = f2;
f2 += f2;
cout << "+=: f2 = " << f2 << ". f2 (before) = " << f3 << endl;
// Fraction& operator-=(const Fraction&);
f1 = Fraction(5, 12);
f2 = Fraction(7, 18);
f3 = f2;
f2 -= f1;
cout << "-=: f2 = " << f2 << ". f1 = " << f1 << ". f2 (before) = " << f3 << endl;
f3 = f2;
f2 -= f2;
cout << "-=: f2 = " << f2 << ". f2 (before) = " << f3 << endl;
// Fraction& operator*=(const Fraction&);
f1 = Fraction(5, 12);
f2 = Fraction(7, 18);
f3 = f2;
f2 *= f1;
cout << "*=: f2 = " << f2 << ". f1 = " << f1 << ". f2 (before) = " << f3 << endl;
f3 = f2;
f2 *= f2;
cout << "*=: f2 = " << f2 << ". f2 (before) = " << f3 << endl;
// Fraction& operator/=(const Fraction&);
f1 = Fraction(5, 12);
f2 = Fraction(7, 18);
f3 = f2;
f2 /= f1;
cout << "/=: f2 = " << f2 << ". f1 = " << f1 << ". f2 (before) = " << f3 << endl;
f3 = f2;
f2 /= f2;
cout << "/=: f2 = " << f2 << ". f2 (before) = " << f3 << endl;
// Fraction& operator%=(const Fraction&);
f1 = Fraction(7, 18);
f2 = Fraction(5, 12);
f3 = f2;
f2 %= f1;
cout << "%=: f2 = " << f2 << ". f1 = " << f1 << ". f2 (before) = " << f3 << endl;
f3 = f2;
f2 %= f2;
cout << "%=: f2 = " << f2 << ". f2 (before) = " << f3 << endl;
// ARRAY INDEX OPERATOR
// Fraction& operator[](unsigned int);
Fraction f[2];
f1 = Fraction(7, 18);
f2 = Fraction(5, 12);
f[0] = f1;
f[1] = f2;
cout << "[]: f[0] = " << f[0] << ". f[1] = " << f[1] << endl;
// CONVERSION OPERATORS
// operator int();
f[0] *= 11;
cout << "(int): (int)f[0] = " << (int)f[0] << ". f[0] = " << f[0] << endl;
// operator double();
cout << "(double): (double)f[0] = " << (double)f[0] << ". f[0] = " << f[0] << endl;
// SPECIAL OPERATORS
// Fraction operator!(); // Inverts a fraction
f[1] *= 11;
cout << "Inversion: 1/f[1] = " << !f[1] << ". f[1] = " << f[1] << endl;
// int operator&(); // Gets the Numerator
f1 = Fraction(7, 18);
cout << "Numerator: f1.Num = " << &f1 << ". f1 = " << f1 << endl;
// unsigned int operator~(); // Gets the Denominator
cout << "Denominator: f1.Deno = " << ~f1 << ". f1 = " << f1 << endl;
// COMPOSITE SCENARIO
f1 = Fraction(7, 18);
f2 = Fraction(5, 12);
f3 = Fraction(16, 9);
f[0] = f1;
f[1] = f2;
Fraction fTmp;
fTmp = f1 - f2++ * f3 + f[0]/f[1];
cout << "Composite: fTmp = " << fTmp << ". f1 = " << f1 << ". f2 = " << f2 << ". f3 = " << f3 << ". f[0] = "
<< f[0] << ". f[1] = " << f[1] << endl;
return 0;
}
|