Rational Test


Program Listing

RationalTest.cpp

/************************************************************************
*                                                                       *
*   Rational Test                                                       *
*                                                                       *
************************************************************************/

#include <iostream>
#include <string>
#include "rational.h"

using namespace std;

int main() {

//  Test the Rational constructors

    cout << "Test the constructors" << endl << endl;
    Rational r;
    cout << "The value of r is " << r << endl;
    Rational s(2);
    cout << "The value of s is " << s << endl;
    Rational t(2, 3);
    cout << "The value of t is " << t << endl;
    Rational u = 3;
    cout << "The value of u is " << u << endl;
    Rational v = Rational(4, 5);
    cout << "The value of v is " << v << endl;
    Rational w = s + t;
    cout << "The value of w is " << w << endl;

//  Test the extraction and insertion operators

    cout << endl << "Test the extraction and insertion operators" << endl << endl;
    cout << "Enter a rational value:  " << flush;
    cin >> r;
    cout << "r = " << r << endl;

//  Construct some Rational constants

    cout << endl << "Test the arithmetic operators" << endl << endl;
    r = Rational(1, 3);
    s = Rational(1, 4);
    cout << r << " + " << s << " = " << r + s << endl;
    cout << r << " - " << s << " = " << r - s << endl;
    cout << r << " * " << s << " = " << r * s << endl;
    cout << r << " / " << s << " = " << r / s << endl;

//  Rational type cast

    cout << endl << "Test mixing of ints, floats, and Rationals and type-casting" << endl << endl;
    cout << "Rational + int:  " << r << " + " << 2 << " = " << r + 2 << endl;
    cout << "int + Rational:  " << 2 << " + " << r << " = " << 2 + r << endl;
    cout << "Rational + float:  " << r << " + " << 2.8 << " = " << r + 2.8 << endl;
    cout << "float + Rational:  " << 2.8 << " + " << r << " = " << 2.8 + r << endl;
    cout << "Assign int 2 to Rational" << endl;
    r = 2;
    cout << "r = " << r << endl;
    cout << "Assign float 2.8 to Rational" << endl;
    r = 2.8;
    cout << "r = " << r << endl;
    cout << "Type-cast int 2 as Rational" << endl;
    cout << "(Rational) " << 2 << " is " << (Rational) 2 << endl;
    cout << "Type-cast float 2.8 as Rational" << endl;
    cout << "(Rational) " << 2.8 << " is " << (Rational) 2.8 << endl;

//  Test compound assignment

    cout << endl << "Test the compound assignments" << endl << endl;
    r = Rational(1, 3);
    s = Rational(1, 4);
    cout << "r = " << r << endl;
    cout << "Add " << s << " to r" << endl;
    r += s;
    cout << "r = " << r << endl;
    cout << "Subtract " << s << " from r" << endl;
    r -= s;
    cout << "r = " << r << endl;
    cout << "Multiply r by " << s << endl;
    r *= s;
    cout << "r = " << r << endl;
    cout << "Divide r by " << s << endl;
    r /= s;
    cout << "r = " << r << endl;
    
//  Test increment and decrement

    cout << endl << "Test increment and decrement" << endl << endl;
    cout << "r = " << r << endl;
    cout << "(Pre-increment) r = " << ++r << endl;
    cout << "(Post-increment) r = " << r++ << endl;
    cout << "r = " << r << endl;
    cout << "(Pre-decrement) r = " << --r << endl;
    cout << "(Post-decrement) r = " << r-- << endl;
    cout << "r = " << r << endl;
    
//  Test the relational operators

//  Test operators when r == s

    cout << endl << "Test the relational operators when r == s" << endl << endl;

    r = s;
    if (r == s)
        cout << r << " == " << s << endl;
    else
        cout << r << " != " << s << endl;

    if (r != s)
        cout << r << " != " << s << endl;
    else
        cout << r << " == " << s << endl;

    if (r < s)
        cout << r << " < " << s << endl;
    else
        cout << r << " >= " << s << endl;

    if (r > s)
        cout << r << " > " << s << endl;
    else
        cout << r << " <= " << s << endl;

    if (r <= s)
        cout << r << " <= " << s << endl;
    else
        cout << r << " > " << s << endl;

    if (r >= s)
        cout << r << " >= " << s << endl;
    else
        cout << r << " < " << s << endl;

//  Test relational operators when r < s

    cout << endl << "Test the relational operators when r < s" << endl << endl;

    r = s - 1;
    if (r == s)
        cout << r << " == " << s << endl;
    else
        cout << r << " != " << s << endl;

    if (r != s)
        cout << r << " != " << s << endl;
    else
        cout << r << " == " << s << endl;

    if (r < s)
        cout << r << " < " << s << endl;
    else
        cout << r << " >= " << s << endl;

    if (r > s)
        cout << r << " > " << s << endl;
    else
        cout << r << " <= " << s << endl;

    if (r <= s)
        cout << r << " <= " << s << endl;
    else
        cout << r << " > " << s << endl;

    if (r >= s)
        cout << r << " >= " << s << endl;
    else
        cout << r << " < " << s << endl;

//  Test relational operators when r > s

    cout << endl << "Test the relational operators when r > s" << endl << endl;

    r = s + 1;
    if (r == s)
        cout << r << " == " << s << endl;
    else
        cout << r << " != " << s << endl;

    if (r != s)
        cout << r << " != " << s << endl;
    else
        cout << r << " == " << s << endl;

    if (r < s)
        cout << r << " < " << s << endl;
    else
        cout << r << " >= " << s << endl;

    if (r > s)
        cout << r << " > " << s << endl;
    else
        cout << r << " <= " << s << endl;

    if (r <= s)
        cout << r << " <= " << s << endl;
    else
        cout << r << " > " << s << endl;

    if (r >= s)
        cout << r << " >= " << s << endl;
    else
        cout << r << " < " << s << endl;

//  Test the other member functions

    cout << endl << "Test the other member functions" << endl << endl;
    r = Rational(8, 3);
    cout << "r = " << r << endl;
    cout << "The integer part of " << r << " is " << r.IntPart() << endl;
    cout << "The fractional part of " << r << " is " << r.FracPart() << endl;
    cout << "The nearest integer to " << r << " is " << r.Round() << endl;
    cout << r << " is approximately equal to " << r.Approx() << endl;
    cout << r << " rounded to the nearest 8th is ";
    cout << r.RatApprox(8) << endl;
    cout << r << " rounded to the nearest 10th is ";
    cout << r.RatApprox(10) << endl;
    
    cout << endl << "Good-bye" << endl;
        
    return 0;
}

Sample Run

Test the constructors

The value of r is 0
The value of s is 2
The value of t is 2/3
The value of u is 3
The value of v is 4/5
The value of w is 8/3

Test the extraction and insertion operators

Enter a rational value:  2/3
r = 2/3

Test the arithmetic operators

1/3 + 1/4 = 7/12
1/3 - 1/4 = 1/12
1/3 * 1/4 = 1/12
1/3 / 1/4 = 4/3

Test mixing of ints, floats, and Rationals and type-casting

Rational + int:  1/3 + 2 = 7/3
int + Rational:  2 + 1/3 = 7/3
Rational + float:  1/3 + 2.8 = 7/3
float + Rational:  2.8 + 1/3 = 7/3
Assign int 2 to Rational
r = 2
Assign float 2.8 to Rational
r = 2
Type-cast int 2 as Rational
(Rational) 2 is 2
Type-cast float 2.8 as Rational
(Rational) 2.8 is 2

Test the compound assignments

r = 1/3
Add 1/4 to r
r = 7/12
Subtract 1/4 from r
r = 1/3
Multiply r by 1/4
r = 1/12
Divide r by 1/4
r = 1/3

Test increment and decrement

r = 1/3
(Pre-increment) r = 4/3
(Post-increment) r = 4/3
r = 7/3
(Pre-decrement) r = 4/3
(Post-decrement) r = 4/3
r = 1/3

Test the relational operators when r == s

1/4 == 1/4
1/4 == 1/4
1/4 >= 1/4
1/4 <= 1/4
1/4 <= 1/4
1/4 >= 1/4

Test the relational operators when r < s

-3/4 != 1/4
-3/4 != 1/4
-3/4 < 1/4
-3/4 <= 1/4
-3/4 <= 1/4
-3/4 < 1/4

Test the relational operators when r > s

5/4 != 1/4
5/4 != 1/4
5/4 >= 1/4
5/4 > 1/4
5/4 > 1/4
5/4 >= 1/4

Test the other member functions

r = 8/3
The integer part of 8/3 is 2
The fractional part of 8/3 is 2/3
The nearest integer to 8/3 is 3
8/3 is approximately equal to 2.66667
8/3 rounded to the nearest 8th is 21/8
8/3 rounded to the nearest 10th is 27/10

Good-bye



Return to Lectures page


e-mail me at robbk@hsc.edu

This page was last modified on Tue Dec 7 16:04:30 1999 .

URL: http://people.hsc.edu/faculty-staff/robbk/Coms261/Examples/RationalTest.html

Return to Coms 261 home page