//File: Project02_rect.cpp
//Date: 02/08/2002
//Authors: Us
////////////////////////////////////////////////////////////
//Project02
////////////////////////////////////////////////////////////
//Add area, operator <, operator <= to rect03.cpp
////////////////////////////////////////////////////////////
//Problem: define rectangle type and some operations for it
////////////////////////////////////////////////////////////
////////////////////////////////////////////////////////////
// includes
////////////////////////////////////////////////////////////
#include <iostream.h>
#include <stdlib.h>
#include <time.h>
////////////////////////////////////////////////////////////
// type TRectangle definition
////////////////////////////////////////////////////////////
const int TEST_COUNT = 5;
////////////////////////////////////////////////////////////
// type TRectangle definition
////////////////////////////////////////////////////////////
struct TRectangle
{
int length;
int width;
};
////////////////////////////////////////////////////////////
// prototype
////////////////////////////////////////////////////////////
void init(TRectangle &rec, const int a, const int b);
void init(TRectangle &rec, const char ch);
void display(const TRectangle &rec);
bool areEqual(const TRectangle &rec1, const TRectangle &rec2);
bool operator == (const TRectangle &rec1, const TRectangle &rec2);
bool operator != (const TRectangle &rec1, const TRectangle &rec2);
ostream & operator << (ostream &bob, const TRectangle &rec);
int area(const TRectangle &rec);
bool operator < (const TRectangle &rec1, const TRectangle &rec2);
bool operator <= (const TRectangle &rec1, const TRectangle &rec2);
istream & operator >> (istream &bob, TRectangle &rec);
////////////////////////////////////////////////////////////
// prototype test functions
////////////////////////////////////////////////////////////
void testInit1(void);
void testInit2(void);
void testOperatorEqual(void);
void testOperatorNotEqual(void);
void testArea(void);
void testOperatorLessThan(void);
void testOperatorLessThanOrEqualTo(void);
void testOperatorExtraction(void);
////////////////////////////////////////////////////////////
// main
////////////////////////////////////////////////////////////
void main(void)
{
srand(time(NULL));
//testInit1();
//testInit2();
//testOperatorEqual();
//testOperatorNotEqual();
testArea();
testOperatorLessThan();
testOperatorLessThanOrEqualTo();
testOperatorExtraction();
}
////////////////////////////////////////////////////////////
// operator >>
////////////////////////////////////////////////////////////
istream & operator >> (istream &bob, TRectangle &rec)
{
int tLength;
do
{
cout << "Length: ";
bob >> tLength;
}
while (tLength <= 0);
rec.length = tLength;
int tWidth;
do
{
cout << "Width: ";
bob >> tWidth;
}
while (tWidth <= 0);
rec.width = tWidth;
return bob;
}
/*++++++++++++++++++++++++++++++++++++++++++++++++++++++++*/
// testOperatorExtraction
/*++++++++++++++++++++++++++++++++++++++++++++++++++++++++*/
void testOperatorExtraction(void)
{
cout << "test operator >> \n";
cout << "=================\n";
for (int i=1; i<=TEST_COUNT; i++)
{
TRectangle rect;
cin >> rect;
cout << rect;
cout << "------------------\n";
}
}
////////////////////////////////////////////////////////////
// area
////////////////////////////////////////////////////////////
int area(const TRectangle &rect)
{
return rect.length*rect.width;
}
/*++++++++++++++++++++++++++++++++++++++++++++++++++++++++*/
// testArea
/*++++++++++++++++++++++++++++++++++++++++++++++++++++++++*/
void testArea(void)
{
cout << "test area(rec)\n";
cout << "==============\n";
for (int i=1; i<=TEST_COUNT; i++)
{
TRectangle rect;
init(rect, 'r');
cout << rect;
cout << "Area = " << area(rect) << endl;
cout << "------------------\n";
}
}
////////////////////////////////////////////////////////////
// operator <
////////////////////////////////////////////////////////////
bool operator < (const TRectangle &rec1, const TRectangle &rec2)
{
return area(rec1) < area(rec2);
}
/*++++++++++++++++++++++++++++++++++++++++++++++++++++++++*/
// testOperatorLessThan
/*++++++++++++++++++++++++++++++++++++++++++++++++++++++++*/
void testOperatorLessThan(void)
{
cout << "test operator < \n";
cout << "================\n";
for (int i=1; i<=TEST_COUNT; i++)
{
TRectangle rec1, rec2;
init(rec1, 'r');
init(rec2, 'r');
cout << "rec1 = " << rec1;
cout << "rec2 = " << rec2;
cout << "area of rec1 = " << area(rec1) << endl;
cout << "area of rec2 = " << area(rec2) << endl;
if (rec1 < rec2)
cout << "area of rec1 < area of rec2\n";
else
cout << "area of rec1 is not < area of rec2\n";
cout << "------------------\n";
}
}
////////////////////////////////////////////////////////////
// operator <=
////////////////////////////////////////////////////////////
bool operator <= (const TRectangle &rec1, const TRectangle &rec2)
{
return area(rec1) <= area(rec2);
}
/*++++++++++++++++++++++++++++++++++++++++++++++++++++++++*/
// testOperatorLessThanOrEqualTo
/*++++++++++++++++++++++++++++++++++++++++++++++++++++++++*/
void testOperatorLessThanOrEqualTo(void)
{
cout << "test operator <= \n";
cout << "=================\n";
for (int i=1; i<=TEST_COUNT; i++)
{
TRectangle rec1, rec2;
init(rec1, 'r');
init(rec2, 'r');
cout << "rec1 = " << rec1;
cout << "rec2 = " << rec2;
cout << "area of rec1 = " << area(rec1) << endl;
cout << "area of rec2 = " << area(rec2) << endl;
if (rec1 <= rec2)
cout << "area of rec1 <= area of rec2\n";
else
cout << "area of rec1 not <= area of rec2\n";
cout << "------------------\n";
}
}
////////////////////////////////////////////////////////////
// init
////////////////////////////////////////////////////////////
void init(TRectangle &rec, const int a, const int b)
{
if (a < 0)
rec.length = -a;
else
rec.length = a;
if (b < 0)
rec.width = -b;
else
rec.width = b;
if (0 == a)
rec.length = 1;
if (0 == b)
rec.width = 1;
}
/*++++++++++++++++++++++++++++++++++++++++++++++++++++++++*/
// testInit1
/*++++++++++++++++++++++++++++++++++++++++++++++++++++++++*/
void testInit1(void)
{
cout << "init(TRectangle &rec, const int a, const int b)\n";
cout << "===============================================\n";
for (int i=1; i<=TEST_COUNT; i++)
{
TRectangle r1;
int a = -10 + rand()%21;
int b = -10 + rand()%21;
cout << "a= " << a << ", b= " << b << endl;
cout << "Before init(r1, a, b);\n";
cout <<r1;
init(r1, a, b);
cout << "After init(r1, a, b);\n";
cout << r1;
TRectangle *p1;
p1 = new TRectangle;
cout << "Before init(*p1, a, b);\n";
cout << *p1;
init(*p1, a, b);
cout << "Before init(*p1, a, b);\n";
cout << *p1;
delete p1;
cout << "-----------------------\n";
}
}
////////////////////////////////////////////////////////////
// init
////////////////////////////////////////////////////////////
void init(TRectangle &rec, const char ch)
{
if (('r' == ch) || ('R' == ch))
{
rec.length = 1 + rand()%10;
rec.width = 1 + rand()%10;
}
else
{
rec.length = 1;
rec.width = 1;
}
}
/*++++++++++++++++++++++++++++++++++++++++++++++++++++++++*/
// testInit2
/*++++++++++++++++++++++++++++++++++++++++++++++++++++++++*/
void testInit2(void)
{
cout << "init(TRectangle &rec, const char ch)\n";
cout << "====================================\n";
for (int i=1; i<=TEST_COUNT; i++)
{
TRectangle r1;
char ch;
char characters[] = {'r', 'R', 'x'};
int a = rand()%3;
ch = characters[a];
cout << "ch= " << ch << endl;
cout << "Before init(r1, ch);\n";
cout <<r1;
init(r1, ch);
cout << "After init(r1, ch);\n";
cout << r1;
TRectangle *p1;
p1 = new TRectangle;
cout << "Before init(*p1, ch);\n";
cout << *p1;
init(*p1, ch);
cout << "After init(*p1, ch);\n";
cout << *p1;
delete p1;
cout << "-----------------------\n";
}
}
////////////////////////////////////////////////////////////
// display
////////////////////////////////////////////////////////////
void display(const TRectangle &rec)
{
cout << "rect(" << rec.length << ", "
<< rec.width << ')' << endl;
}
////////////////////////////////////////////////////////////
// areEqual
////////////////////////////////////////////////////////////
bool areEqual(const TRectangle &rec1, const TRectangle &rec2)
{
return (rec1.length == rec2.length)
&& (rec1.width == rec2.width);
}
////////////////////////////////////////////////////////////
// operator ==
////////////////////////////////////////////////////////////
bool operator == (const TRectangle &rec1, const TRectangle &rec2)
{
return (rec1.length == rec2.length)
&& (rec1.width == rec2.width);
}
/*++++++++++++++++++++++++++++++++++++++++++++++++++++++++*/
// testOperatorEqual
/*++++++++++++++++++++++++++++++++++++++++++++++++++++++++*/
void testOperatorEqual(void)
{
cout << "bool operator == (rec1, rec2)\n";
cout << "=============================\n";
for (int i=1; i<=TEST_COUNT; i++)
{
TRectangle r1, r2;
init(r1, 'r');
init(r2, 'r');
cout << "Initially\n";
cout << "r1=" << r1;
cout << "r2=" << r2;
if (r1 == r2)
cout << "Are Equal\n";
else
cout << "Not Equal\n";
r1 = r2;
cout << "After r1 = r2;\n";
cout << "r1=" << r1;
cout << "r2=" << r2;
if (r1 == r2)
cout << "Are Equal\n";
else
cout << "Not Equal\n";
cout << "-----------------------\n";
}
}
////////////////////////////////////////////////////////////
// operator !=
////////////////////////////////////////////////////////////
bool operator != (const TRectangle &rec1, const TRectangle &rec2)
{
return !(rec1 == rec2);
}
/*++++++++++++++++++++++++++++++++++++++++++++++++++++++++*/
// testOperatorNotEqual
/*++++++++++++++++++++++++++++++++++++++++++++++++++++++++*/
void testOperatorNotEqual(void)
{
cout << "bool operator != (rec1, rec2)\n";
cout << "=============================\n";
for (int i=1; i<=TEST_COUNT; i++)
{
TRectangle r1, r2;
init(r1, 'r');
init(r2, 'r');
cout << "Initially\n";
cout << "r1=" << r1;
cout << "r2=" << r2;
if (r1 != r2)
cout << "Not Equal\n";
else
cout << "Are Equal\n";
r1 = r2;
cout << "After r1 = r2;\n";
cout << "r1=" << r1;
cout << "r2=" << r2;
if (r1 != r2)
cout << "Not Equal\n";
else
cout << "Are Equal\n";
cout << "-----------------------\n";
}
}
////////////////////////////////////////////////////////////
// operator << for output
////////////////////////////////////////////////////////////
ostream & operator << (ostream &bob, const TRectangle &rec)
{
bob << "rect(" << rec.length << ", "
<< rec.width << ')' << endl;
return bob;
}
/**********************************************************/
// SAMPLE OUTPUT
/**********************************************************/
/*
test area(rec)
==============
rect(1, 9)
Area = 9
------------------
rect(7, 2)
Area = 14
------------------
rect(3, 6)
Area = 18
------------------
rect(1, 8)
Area = 8
------------------
rect(6, 6)
Area = 36
------------------
test operator <
================
rec1 = rect(5, 5)
rec2 = rect(8, 2)
area of rec1 = 25
area of rec2 = 16
area of rec1 is not < area of rec2
------------------
rec1 = rect(9, 7)
rec2 = rect(1, 2)
area of rec1 = 63
area of rec2 = 2
area of rec1 is not < area of rec2
------------------
rec1 = rect(9, 10)
rec2 = rect(4, 2)
area of rec1 = 90
area of rec2 = 8
area of rec1 is not < area of rec2
------------------
rec1 = rect(10, 3)
rec2 = rect(8, 3)
area of rec1 = 30
area of rec2 = 24
area of rec1 is not < area of rec2
------------------
rec1 = rect(9, 6)
rec2 = rect(9, 4)
area of rec1 = 54
area of rec2 = 36
area of rec1 is not < area of rec2
------------------
test operator <=
=================
rec1 = rect(8, 1)
rec2 = rect(10, 1)
area of rec1 = 8
area of rec2 = 10
area of rec1 <= area of rec2
------------------
rec1 = rect(9, 10)
rec2 = rect(4, 2)
area of rec1 = 90
area of rec2 = 8
area of rec1 not <= area of rec2
------------------
rec1 = rect(2, 7)
rec2 = rect(3, 4)
area of rec1 = 14
area of rec2 = 12
area of rec1 not <= area of rec2
------------------
rec1 = rect(4, 2)
rec2 = rect(1, 2)
area of rec1 = 8
area of rec2 = 2
area of rec1 not <= area of rec2
------------------
rec1 = rect(1, 9)
rec2 = rect(7, 2)
area of rec1 = 9
area of rec2 = 14
area of rec1 <= area of rec2
------------------
test operator >>
=================
Length: 1
Width: -2
Width: 2
rect(1, 2)
------------------
Length: 3
Width: 4
rect(3, 4)
------------------
Length: 5
Width: 6
rect(5, 6)
------------------
Length: 7
Width: 8
rect(7, 8)
------------------
Length: 9
Width: 10
rect(9, 10)
------------------
Press any key to continue
*/
|