//chap12p6.cpp
//author
//The complete program listing of the program that defines
//and uses the class clockType
/*
Project 2, assigned 9/22/2006, due 9/29/2006
Develop the following member functions:
CTimes(void); // gives 00:00:00 times
CTimes(char ch); //'r' gives random times
void sortBubble(void); // sorts in increasing order
Use the test functions supplied
Submit the printed source code, sample run
*/
#include <iostream>
using namespace std;
class clockType
{
private:
int hr;
int min;
int sec;
public:
//clockType(void);
clockType(int hours=0, int minutes=0, int seconds=0);
clockType(char ch)
{
hr = rand()%24;
min = rand()%60;
sec = rand()%60;
};
void setTime(int hours, int minutes, int seconds);
void getTime(int &hours, int &minutes, int &seconds) const;
void printTime(void) const;
void incrementSeconds(void);
void incrementMinutes(void);
void incrementHours(void);
bool equalTime(const clockType &t) const;
bool operator == (const clockType &t) const;
bool operator != (const clockType &t) const;
bool operator < (const clockType &t) const;
friend ostream & operator<<(ostream &tom, const clockType &c);
friend bool operator > (const clockType &c1, const clockType &c2);
friend class CTimes;
};
const int COUNT = 10;
const int TEST_COUNT = 5;
class CTimes
{
private:
clockType ta[COUNT];
public:
CTimes(void); // gives 00:00:00 times
CTimes(char ch); //'r' gives random times
void sortBubble(void); // sorts in increasing order
void display(void);
friend ostream & operator<<(ostream &tom, const CTimes &c);
};
void test_operator_notequal(void);
void test_operator_lessthan(void);
void test_constructorDeafult_CTimes(void);
void test_constructorRandomR_CTimes(void);
void test_constructorSortBubble_CTimes(void);
void mainOld(void);
void main(void)
{
test_constructorDeafult_CTimes();
test_constructorRandomR_CTimes();
test_constructorSortBubble_CTimes();
}
void test_constructorDeafult_CTimes()
{
cout << "test_constructorDeafult_CTimes\n";
for (int i=0; i<TEST_COUNT; i++)
{
CTimes myTimes;
cout << myTimes << endl;
}
}
CTimes::CTimes(void)
{
//set all the times to 00:00:00
cout << "Missing code\n";
}
void test_constructorRandomR_CTimes()
{
cout << "test_constructorRandomR_CTimes\n";
for (int i=0; i<TEST_COUNT; i++)
{
CTimes myTimes('r');
cout << myTimes << endl;
}
}
CTimes::CTimes(char ch)
{
//set random times
if ('r' == ch)
cout << "Missing code\n";
else if ('d' == ch)
//put only distinct values in the object
/*
ALGORITHM0:
step0: put the first random value in ta[0]
step1: generate a new random value in tempClock for time
if tempClock <> ta[0] then
add this to collection
else
goto step1
step2: generate a new random value in tempClock for time
if tempClock <> ta[0] and tempClock <> ta[1]then
add this to collection
else
goto step2
....
step [COUNT-1]:generate a new random value in tempClock for time
if tempClock <> ta[0], ta[1], ta[2], ..., ta[COUNT-2] then
add this to collection
else
goto step [COUNT-1]
ALGORITHM1:
step0: put the first random value in ta[0]
for i=1 to COUNT-1 do the following:
step [i]:generate a new random value in tempClock for time
if tempClock <> ta[0], ta[1], ta[2], ..., ta[i-1] then
add this to collection
else
goto step [i]
ALGORITHM2: (step-wise refinement)
step0: put the first random value in ta[0]
for i=1 to COUNT-1 do the following:
{
do the following:
generate a new random value in tempClock for time
duplicate = false
for j = 0 to i-1
if tempClock = ta[j] then
duplicate = true
exit for loop
end for
if duplicate = false then
add this to collection
while duplicate = true
}
*/
{
//step0: put the first random value in ta[0]
ta[0] = clockType('r');
//for i=1 to COUNT-1 do the following:
for (int i=1; i<= COUNT-1; i++)
{
do
{
clockType tempClock('r');
bool duplicate = false;
for (int j = 0; j<=i-1; j++)
{
if (tempClock == ta[j])
{
duplicate = true;
break;
}
}
if (duplicate == false)
ta[i] = tempClock;
}
while (duplicate == true);
}
}
else
{
cout << "Missing code\n";
}
}
void test_constructorSortBubble_CTimes()
{
cout << "test_constructorSortBubble_CTimes\n";
for (int i=0; i<TEST_COUNT; i++)
{
CTimes myTimes('r');
cout << myTimes << endl;
myTimes.sortBubble();
cout << "After myTimes.sortBubble();\n";
cout << myTimes << endl;
}
}
void CTimes::sortBubble(void)
{
//sort times in increasing order
if (!(this->ta[1].min < this->ta[0].min))
cout << "swap\n";
cout << "Missing code\n";
}
void mainOld(void)
{
CTimes myTimes;
myTimes.display();
cout << endl << myTimes << endl;
return;
clockType a[10];
for (int i=0; i<10; i++)
{
//a[i] = clockType('c');
clockType temp('c');
a[i] = temp;
cout << i << ' ' << a[i] << endl;
}
clockType *ptr;
cout << "sizeof(ptr) = " << sizeof(ptr) << endl;
cout << "sizeof(a[0]) = " << sizeof(a[0]) << endl;
ptr = &a[0]; // ptr = a;
cout << *ptr << endl;
cout << ptr << endl;
cout << a << endl;
ptr++; //ptr = ptr+1;
cout << "ptr = " << ptr << endl;
cout << "*ptr = " << *ptr << endl;
ptr--; //ptr = ptr+1;
cout << "ptr = " << ptr << endl;
cout << "*ptr = " << *ptr << endl;
cout << "ptr+1 = " << ptr+1 << endl;
cout << "*(ptr+1) = " << *(ptr+1) << endl;
return;
clockType c0(10,20,50);
cout << "c0=";
cout << c0;
cout << endl<< endl;
clockType &x0 = c0;
cout << "x0=";
cout << x0;
cout << endl<< endl;
x0.incrementHours();
cout << "x0=";
cout << x0;
cout << endl<< endl;
cout << "c0=";
cout << c0;
cout << endl<< endl;
clockType *p;
p = &c0;
cout << p << endl;
cout << "c0=";
cout << *p;
cout << endl<< endl;
p = new clockType(1,2,3);
cout << p << endl;
cout << "c0=";
cout << *p;
cout << endl<< endl;
delete p;
p = new clockType(4,5,6);
cout << p << endl;
cout << "c0=";
cout << *p;
cout << endl<< endl;
delete p;
return;
clockType c1(100,20,50);
cout << "c1=";
cout << c1;
cout << endl;
clockType c2(10,20);
cout << "c2=";
cout << c2;
cout << endl;
clockType c3(10);
cout << "c3=";
cout << c3;
cout << endl;
clockType c4;
cout << "c4=";
cout << c4;
cout << endl;
return;
clockType myClock1(1,2,5);
cout << "myClock1=";
cout << myClock1;
cout << endl;
clockType myClock2(1,2,3);
cout << "myClock2=";
cout << myClock2;
cout << endl;
if (myClock1 > myClock1)
cout << "myClock1 is < myClock1\n";
else
cout << "myClock1 is NOT < myClock1\n";
if (myClock1 > myClock2)
cout << "myClock1 is < myClock2\n";
else
cout << "myClock1 is NOT < myClock2\n";
return;
//test_operator_notequal(); return;
test_operator_lessthan(); return;
clockType myClock(1,2,3);
//testing 2nd constructor
clockType yourClock(4,5,6);
if (myClock == yourClock)
cout << "myClock is equal to yourClock\n";
else
cout << "myClock is NOT equal to yourClock\n";
myClock = yourClock;
cout << "After myClock = yourClock;\n";
if (myClock == yourClock)
cout << "myClock is equal to yourClock\n";
else
cout << "myClock is NOT equal to yourClock\n";
//Set the time of myClock
myClock.setTime(5, 4, 30); //Line 1
cout << "Line 2: myClock: "; //Line 2
myClock.printTime(); //print the time of myClock Line 3
cout << endl; //Line 4
cout << "Line 5: yourClock: "; //Line 5
yourClock.printTime(); //print the time of yourClock Line 6
cout << endl; //Line 7
//Set the time of yourClock
yourClock.setTime(5, 45, 16); //Line 8
cout << "Line 9: After setting, yourClock: "; //Line 9
yourClock.printTime(); //print the time of yourClock Line 10
cout << endl; //Line 11
//Compare myClock and yourClock
if (myClock.equalTime(yourClock)) //Line 12
cout << "Line 13: Both times are equal."
<< endl; //Line 13
else //Line 14
cout << "Line 15: The two times are not equal."
<< endl; //Line 15
int hours;
int minutes;
int seconds;
cout << "Line 16: Enter the hours, minutes, and "
<< "seconds: "; //Line 16
cin >> hours >> minutes >> seconds; //Line 17
cout << endl; //Line 18
//Set the time of myClock using the value of the
//variables hours, minutes, and seconds
myClock.setTime(hours, minutes, seconds); //Line 19
cout << "Line 20: New myClock: "; //Line 20
myClock.printTime(); //print the time of myClock Line 21
cout << endl; //Line 22
//Increment the time of myClock by one second
myClock.incrementSeconds(); //Line 23
cout << "Line 24: After incrementing myClock by "
<< "one second, myClock: "; //Line 24
myClock.printTime(); //print the time of myClock Line 25
cout << endl; //Line 26
//Retrieve the hours, minutes, and seconds of the
//object myClock
myClock.getTime(hours, minutes, seconds); //Line 27
//Output the value of hours, minutes, and seconds
cout << "Line 28: hours = " << hours
<< ", minutes = " << minutes
<< ", seconds = " << seconds << endl; //Line 28
}//end main
ostream & operator << (ostream &tom, const CTimes ×Object)
{
for (int i=0; i<COUNT; i++)
tom << timesObject.ta[i] << endl;
return tom;
}
void CTimes::display(void)
{
for (int i=0; i<COUNT; i++)
cout << this->ta[i] << endl;
}
ostream &operator<<(ostream &tom, const clockType &c)
{
if (c.hr < 10)
tom << "0";
tom << c.hr << ":";
if (c.min < 10)
tom << "0";
tom << c.min << ":";
if (c.sec < 10)
tom << "0";
tom << c.sec;
return tom;
}
clockType::clockType(int hours, int minutes, int seconds)
{
//cout << "2nd Constructor is being called\n";
if (hours >= 0 && hours < 24)
hr = hours;
else
hr = 0;
if (minutes >= 0 && minutes < 60)
min = minutes;
else
min = 0;
if (seconds >= 0 && seconds < 60)
sec = seconds;
else
sec = 0;
}
//clockType::clockType(void)
// {
// cout << "default Constructor is being called\n";
// hr=min=sec=0;
// }
void clockType::setTime(int hours, int minutes, int seconds)
{
if (hours >= 0 && hours < 24)
//(*this).hr = hours;
//this->hr=hours;
hr = hours;
else
hr = 0;
if (minutes >= 0 && minutes < 60)
min = minutes;
else
min = 0;
if (seconds >= 0 && seconds < 60)
sec = seconds;
else
sec = 0;
}
void clockType::getTime(int& hours, int& minutes,
int& seconds) const
{
hours = hr;
minutes = min;
seconds = sec;
}
void clockType::incrementHours()
{
hr++;
if (hr > 23)
hr = 0;
}
void clockType::incrementMinutes()
{
min++;
if (min > 59)
{
min = 0;
incrementHours(); //increment hours
//this->incrementHours(); //increment hours
}
}
void clockType::incrementSeconds()
{
sec++;
if (sec > 59)
{
sec = 0;
incrementMinutes(); //increment minutes
}
}
void clockType::printTime() const
{
if (hr < 10)
cout << "0";
cout << hr << ":";
if (min < 10)
cout << "0";
cout << min << ":";
if (sec < 10)
cout << "0";
cout << sec;
}
bool clockType::equalTime(const clockType& otherClock) const
{
return (hr == otherClock.hr
&& min == otherClock.min
&& sec == otherClock.sec);
}
bool clockType::operator == (const clockType& otherClock) const
{
return (hr == otherClock.hr
&& min == otherClock.min
&& sec == otherClock.sec);
}
bool clockType::operator != (const clockType& otherClock) const
{
return !(*this == otherClock);
//if (otherClock == *this)
// return false;
//else
// return true;
//return (hr != otherClock.hr
// || min != otherClock.min
// || sec != otherClock.sec);
}
void test_operator_notequal(void)
{
clockType t1(1,2,3), t2(4,5,6);
t1.printTime(); cout << endl;
t2.printTime(); cout << endl;
if(t1 != t2)
cout << "Times are NOT equal\n";
else
cout << "Times are equal\n";
t1 = t2;
cout << "After t1 = t2;\n";
t1.printTime(); cout << endl;
t2.printTime(); cout << endl;
if(t1.operator != (t2))
cout << "Times are NOT equal\n";
else
cout << "Times are equal\n";
if(t1 != t2)
cout << "Times are NOT equal\n";
else
cout << "Times are equal\n";
}
bool clockType::operator < (const clockType& otherClock) const
{
/*
if h1 < h2 return true
if h1 > h2 return false
//that means h1 == h2
if m1 < m2 return true
if m1 > m2 return false
//that means m1 == m2 and h1==h2
if s1 < s2 return true
return false
*/
int h1, m1, s1, h2, m2, s2;
h1 = this->hr;
m1 = this->min;
s1 = this->sec;
h2 = otherClock.hr;
m2 = otherClock.min;
s2 = otherClock.sec;
if (h1 < h2) return true;
if (h1 > h2) return false;
////that means h1 == h2
if (m1 < m2) return true;
if (m1 > m2) return false;
////that means m1 == m2 and h1==h2
if (s1 < s2) return true;
return false;
}
bool operator > (const clockType &c1, const clockType &c2)
{
int h1, m1, s1, h2, m2, s2;
h1 = c1.hr;
m1 = c1.min;
s1 = c1.sec;
h2 = c2.hr;
m2 = c2.min;
s2 = c2.sec;
if (h1 > h2) return true;
if (h1 < h2) return false;
////that means h1 == h2
if (m1 > m2) return true;
if (m1 < m2) return false;
////that means m1 == m2 and h1==h2
if (s1 > s2) return true;
return false;
}
void test_operator_lessthan(void)
{
clockType t1(1,2,3), t2(4,5,6);
t1.printTime(); cout << endl;
t2.printTime(); cout << endl;
if(t1 < t2)
cout << "t1 is less than t2\n";
else
cout << "t1 is NOT less than t2\n";
t1 = t2;
cout << "After t1 = t2;\n";
t1.printTime(); cout << endl;
t2.printTime(); cout << endl;
if(t1 < t2)
cout << "t1 is less than t2\n";
else
cout << "t1 is NOT less than t2\n";
t2.setTime(4,5,5);
cout << "After t2.setTime(4,5,5);\n";
t1.printTime(); cout << endl;
t2.printTime(); cout << endl;
if(t1 < t2)
cout << "t1 is less than t2\n";
else
cout << "t1 is NOT less than t2\n";
t2.setTime(4,4,5);
cout << "After t2.setTime(4,4,5);\n";
t1.printTime(); cout << endl;
t2.printTime(); cout << endl;
if(t1 < t2)
cout << "t1 is less than t2\n";
else
cout << "t1 is NOT less than t2\n";
t2.setTime(3,5,5);
cout << "After t2.setTime(3,5,5);\n";
t1.printTime(); cout << endl;
t2.printTime(); cout << endl;
if(t1 < t2)
cout << "t1 is less than t2\n";
else
cout << "t1 is NOT less than t2\n";
}
|