Fraction Class With More Overloaded Ops

Previous      Next      f2.cc      frac.2.h      frac.2.cc

#include <stdlib.h>
#include <stdio.h>
#include <fstream>
#include <iostream>
#include "frac.2.h"
using namespace std;

int main () {
   Fraction a(2,3);
   cout << "The a fraction is " << a;
   getchar();
   
   Fraction b(3,4);
   cout << "The b fraction is " << b;
   getchar();
	
   cout << "--- try arithmetic operations --\n";
   cout << a << " * " << b << " = " << a*b;
   getchar();
	
   cout << a << " / " << b << " = " << a/b;
   getchar();
	
   cout << a << " + " << b << " = " << a+b;
   getchar();
	
   cout << a << " - " << b << " = " << a-b;
   getchar();
	
   cout << "(a - b)*b = " << (a-b)*b;
   getchar();
	
   cout << a*2 << " " << b*5;
   getchar();

   Fraction c(0,1);
   Fraction d(0,2);
   cout << "--- try = -----\n";
   cout << "c=" << c << " d=" << d << "\n";
   c = d = b;
   cout << "c=" << c << " d=" << d;
   getchar();

   cout << "--- try postincrement ++ -------\n";
   cout << "c=" << c++ << " : " << c << "\n";
   cout << "c=" << c;
   getchar();

   int i=5;
   cout << "i=" << i++ << " : " << i << "\n";
   cout << "--- try preincrement ++ -------\n";
   cout << "c=" << ++c << "\n";
   cout << "c=" << c;
   getchar();

   cout << "--- conversion to double ---\n";
   cout << "c=" << (double)c;
   getchar();
   
   cout << "--- try reading from file ---\n";
   fstream fin;
   fin.open("fracfile.dat", ios::in);
   d.read(fin);
   fin.close();
   cout << "d=" << d;
   getchar();

   cout << "--- try reading from standard input ---\n";
   cout << "Give numerator and denominator: ";
   cin >> d;
   cout << "d=" << d;
   getchar();

   cout << "\n-- try reading from file again ---\n";
   fin.open("fracfile.dat", ios::in);
   fin >> d;
   fin.close();
   cout << d << "\n";
}