#include #include #include #include #include "Fraction.h" using namespace std; int main() { // Test default constructor, and that print of 0 / N displays 0 Fraction f1; cout << "Fraction f1 = " ; f1.print(); cout << endl; // Test alternate constructor, and that newly created fractions are correctly reduced Fraction f2(3, 12); cout << "Fraction constructed as 3 / 12 resulted in: "; f2.print(); cout << endl; // Test setValue method and that pring of N / 1 prints out only N f2.setValue(5, 1); cout << "Fraction set to 5 / 1 resulted in: "; f2.print(); cout << endl; cout << endl; // Test print out of fractions greater than 1 f2.setValue(23,3); cout << "Fraction set to 23 / 3 resulted in: "; f2.print(); cout << endl; // Test out the other accessor methods. cout << "Fraction's float value = " << f2.getValue() << endl; cout << "Fraction's neumerator = " << f2.numerator() << endl; cout << "Fraction's denominator = " << f2.denominator() << endl; cout << endl; // Test arithmetic operations using randomly created Fractions int n, d; Fraction l, r, res; srand(time(NULL)); // seed random number generator using current time n = rand()%19 + 1; d = rand()%19 + 1; l.setValue(n,d); n = rand()%19 + 1; d = rand()%19 + 1; r.setValue(n,d); res = l.add(r); l.print(); cout << " + "; r.print(); cout << " = "; res.print(); cout << endl << endl; n = rand()%19 + 1; d = rand()%19 + 1; l.setValue(n,d); n = rand()%19 + 1; d = rand()%19 + 1; r.setValue(n,d); res = l.sub(r); l.print(); cout << " - "; r.print(); cout << " = "; res.print(); cout << endl << endl; n = rand()%19 + 1; d = rand()%19 + 1; l.setValue(n,d); n = rand()%19 + 1; d = rand()%19 + 1; r.setValue(n,d); res = l.mult(r); l.print(); cout << " * "; r.print(); cout << " = "; res.print(); cout << endl << endl; n = rand()%19 + 1; d = rand()%19 + 1; l.setValue(n,d); n = rand()%19 + 1; d = rand()%19 + 1; r.setValue(n,d); res = l.div(r); l.print(); cout << " / "; r.print(); cout << " = "; res.print(); cout << endl << endl; // likewise test comparison operators bool bres; n = rand()%19 + 1; d = rand()%19 + 1; l.setValue(n,d); n = rand()%19 + 1; d = rand()%19 + 1; r.setValue(n,d); if (l.equal(r)) { l.print(); cout << " is equal to "; r.print(); cout << endl; } else { l.print(); cout << " is not equal to "; r.print(); cout << endl; } n = rand()%19 + 1; d = rand()%19 + 1; l.setValue(n,d); n = rand()%19 + 1; d = rand()%19 + 1; r.setValue(n,d); if (l.lessthan(r)) { l.print(); cout << " is less than "; r.print(); cout << endl; } else { l.print(); cout << " is not less than "; r.print(); cout << endl; } n = rand()%19 + 1; d = rand()%19 + 1; l.setValue(n,d); n = rand()%19 + 1; d = rand()%19 + 1; r.setValue(n,d); if (l.greaterthan(r)) { l.print(); cout << " is greater than "; r.print(); cout << endl; } else { l.print(); cout << " is not greater than "; r.print(); cout << endl; } system("pause"); }