home *** CD-ROM | disk | FTP | other *** search
- // \EXAMPLES\EX1002.CPP
- // main program to exercise Fraction classs
- //---------------------------------------------------------------
-
- // files in this example:
- // %F,15,EX10021.H%EX10021.H definition of the class Fraction
- // %F,15,EX10021.CPP%EX10021.CPP member functions of the class Fraction
- // EX1002.CPP this file
- //-------------------------------------------------------------
-
- #include "EX10021.H"
- #include <iostream.h>
-
- //-------------------------------------------------------------
- const int nfrac = 4; // 4 fraction objects
-
- void print1(Fraction* F, int n); // print in various formats
-
- void sort1(Fraction* F, int n); // sort and print
-
- //--------------------------------------------------------------
-
- void main()
- { Fraction f[nfrac]; // in array for convenience
- // user input values for Fraction objects
- int i;
- cout << "Enter 4 fractions (e.g. 1/2 -4/6 3 8/5 ): ";
- for ( i = 0; i < nfrac; i++)
- cin >> f[i]; // overloaded >>
- Fraction a = f[0]; // save for future use
- Fraction b = f[1]; // save for future use
- Fraction c = f[2]; // save for future use
- Fraction d = f[3]; // save for future use
- // manipulate and print array of Fractions
- print1(f, nfrac);
- sort1(f, nfrac);
- // arithmetic on Fractions a, b, c, d
- cout << "ARITHMETIC EXPRESSIONS:"<< endl;
- cout << "binary + : a + b \t\t\t= "
- << (a + b) << endl;
- cout << "binary * : c * d \t\t\t= "
- << (c * d) << endl;
- cout << "binary * - / : a * b - c / d\t\t= "
- << (a * b - c / d) << endl;
- cout << "mixed mode : a + 2.5 - d\t\t= "
- << (a + 2.5 - d) << endl;
- cout << "unary - : -b\t\t\t= "
- << (-b) << endl;
- cout << "prefix ++ : ++d\t\t\t= "
- << (++d) << "\t\td =\t"
- << d << endl;
- cout << "postfix -- : d--\t\t\t= "
- << (d--) << "\t\td =\t"
- << d << endl;
- cout << "unary + : +d\t\t\t= "
- << (+d) << endl;
- cout << "mixed mode : 2 / b + 0.125 * c \t= "
- << (2 / b + 0.125 * c) << endl;
- cout << "average : (a + b + c + d)/4\t= "
- << ((a + b + c + d)/4) << endl;
- cout << "END";
- }
-
- //--------------------------------------------------------------
- // Print elements of array f of n Fractions:
- // as improper and proper fractions
- // inverted, converted to floating point and truncated to int
- //--------------------------------------------------------------
- void print1(Fraction* f, int n)
- { int i;
- cout << endl << "\t\t\ta\tb\tc\td" << endl;
- cout << "IMPROPER FRACTION:\t";
- for( i = 0; i < n; i++) // output
- cout << f[i] << "\t"; // overloaded <<
- cout << endl <<" PROPER FRACTION:\t";
- for( i = 0; i < n; i++) // display as proper
- f[i].display(cout) << "\t"; // member function
- cout << endl <<" INVERSE:\t";
- for( i = 0; i < n; i++) // print the inverse
- { print(f[i].invert(), cout); // friend function
- cout << "\t";
- f[i].invert(); } // member function
- cout << endl << " REAL:\t";
- for( i = 0; i < n; i++) // convert to double
- cout << real(f[i]) << "\t"; // friend function
- cout << endl << " TRUNC:\t";
- for( i = 0; i < n; i++) // truncate to int
- cout << trunc(f[i]) << "\t"; // friend function
- cout << endl << endl;
- }
-
- //--------------------------------------------------------------
- // A bubble sort to order array f of n Fractions
- // and print the elements in ascending order
- //--------------------------------------------------------------
- void sort1(Fraction* f, int n)
- { int i,j;
- Fraction swap; // local object
- for( i = 0; i < n-1; i++) // bubble sort
- for( j = 1; j < n-i; j++) // overloaded
- if( f[j-1] > f[j] ) // operators < =
- { swap = f[j]; f[j] = f[j-1]; f[j-1] = swap; }
- cout << " SORTED ASCENDING:\t";
- cout << f[0];
- for( i = 1; i < n; i++) // overloaded > <<
- cout << ( f[i] > f[i-1] ? " <\t" : " =\t" )
- << f[i];
- cout << endl << endl;
- };
-
- //---------------------------------------------------------------
-