rect02
Home ] Up ]

 

//rect02.cpp
//Date:    02/06/2002
//Authors: Us


////////////////////////////////////////////////////////////
//Problem: define rectangle type and some operations for it
////////////////////////////////////////////////////////////


////////////////////////////////////////////////////////////
// includes
////////////////////////////////////////////////////////////
#include <iostream.h>
#include <stdlib.h>


////////////////////////////////////////////////////////////
// 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);


////////////////////////////////////////////////////////////
// main
////////////////////////////////////////////////////////////
void main(void)
  {
  TRectangle r1;
  init(r1, 'r');
  display(r1);

  TRectangle r2;
  init(r2, 'r');
  display(r2);

  init(r2, 1, 3);
  
  if (areEqual(r1, r2))
      cout << "Are Equal\n";
    else
      cout << "Not Equal\n";

  r1 = r2;
  cout << "After r1=r2;\n";
  display(r1);
  display(r2);

  if (r1 == r2)
      cout << "Are Equal\n";
    else
      cout << "Not Equal\n";

  init(r2, 3, 3);
  display(r1);
  display(r2);

  if (r1 != r2)
      cout << "Not Equal\n";
    else
      cout << "Are Equal\n";

  //cout << r1 << endl;
  }


////////////////////////////////////////////////////////////
// init
////////////////////////////////////////////////////////////
void init(TRectangle &rec, const int a, const int b)
  {
  rec.length = a;
  rec.width = b;
  }


////////////////////////////////////////////////////////////
// init
////////////////////////////////////////////////////////////
void init(TRectangle &rec, const char ch)
  {
  if (('r' == ch) || ('R' == ch))
    {
    rec.length = rand()%10;
    rec.width  = rand()%10;
    }
  else
    {
    rec.length = 0;
    rec.width  = 0;
    }
  }


////////////////////////////////////////////////////////////
// display
////////////////////////////////////////////////////////////
void display(const TRectangle &rec)
  {
  cout << 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);
  }


////////////////////////////////////////////////////////////
// operator !=
////////////////////////////////////////////////////////////
bool operator != (const TRectangle &rec1, const TRectangle &rec2)
  {
  return !(rec1 == rec2); 
  }