//CSet03.cpp
//Date 04/25/2003
//Author: AOU
/*
Added the following functions:
bool operator < (CNode node);
bool operator <= (CNode node);
bool operator != (CNode node);
*/
#include <iostream.h>
#include <stdlib.h>
const int UNDEFINED = -99;
const int MAX_VALUE = 99;
class CNode
{
private:
int m_info;
CNode *m_next;
public:
CNode(void);
CNode(int x);
void display(void) const;
CNode(char ch);
int get(void);
void set(int x);
friend ostream & operator << (ostream & tom, CNode node);
bool operator == (CNode node);
bool operator < (CNode node);
bool operator <= (CNode node);
bool operator != (CNode node);
};
class CSet
{
private:
CNode *m_first;
int m_n;
public:
CSet(void);
CSet(char ch);
CSet(CNode n);
void insert(int x);
CNode* seqSearch(int x);
void display(void) const;
void sortBubble(void);
void sortSelection(void);
void sortInsertion(void);
};
void main(void)
{
/*
CNode myNode;
myNode.display();
cout << endl;
CNode myNode1(55);
myNode1.display();
cout << endl;
CNode myNode2('r');
myNode2.display();
cout << endl;
cout << "info=" << myNode2.get() << endl;
myNode2.set(32);
cout << "info=" << myNode2.get() << endl;
cout << myNode2 << endl;
if (myNode1 == myNode2)
cout << "myNode1 is equal to myNode2\n";
else
cout << "myNode1 is NOT equal to myNode2\n";
myNode1 = myNode2;
if (myNode1 == myNode2)
cout << "myNode1 is equal to myNode2\n";
else
cout << "myNode1 is NOT equal to myNode2\n";
*/
{
cout << "Testing operator < \n";
cout << "-------------------\n";
CNode n1('r'), n2('r');
cout << n1 << endl << n2 << endl;
if (n1 < n2)
cout << "n1 is less than n2\n";
else
cout << "n1 is NOT less than n2\n";
CNode m1('r'), m2('r');
cout << m1 << endl << m2 << endl;
if (m1 < m2)
cout << "m1 is less than m2\n";
else
cout << "m1 is NOT less than m2\n";
}
{
cout << "Testing operator <= \n";
cout << "--------------------\n";
CNode n1('r'), n2('r');
cout << "n1=" << n1 << endl << "n2=" << n2 << endl;
if (n1 <= n2)
cout << "n1 is <= n2\n";
else
cout << "n1 is NOT <= n2\n";
if (n2 <= n1)
cout << "n2 is <= n1\n";
else
cout << "n2 is NOT <= n1\n";
}
{
cout << "Testing operator != \n";
cout << "--------------------\n";
CNode n1('r'), n2('r');
cout << "n1=" << n1 << endl << "n2=" << n2 << endl;
if (n1 != n2)
cout << "n1 is != n2\n";
else
cout << "n1 is = n2\n";
n1 = n2;
if (n1 != n2)
cout << "n1 is != n2\n";
else
cout << "n1 is = n2\n";
}
}
bool CNode::operator != (CNode node)
{
return (this->m_info != node.m_info);
}
bool CNode::operator <= (CNode node)
{
return (this->m_info <= node.m_info);
}
bool CNode::operator < (CNode node)
{
return (this->m_info < node.m_info);
}
bool CNode::operator == (CNode node)
{
return (this->m_info == node.m_info);
}
ostream & operator << (ostream & tom, CNode node)
{
tom << node.m_info;
return tom;
}
void CNode::set(int x)
{
this->m_info = x;
}
int CNode::get(void)
{
return this->m_info;
}
CNode::CNode(char ch)
{
if ('r' == ch)
this->m_info = rand()%(MAX_VALUE+1);
else
this->m_info = UNDEFINED;
}
CNode::CNode(void)
{
this->m_info = UNDEFINED;
this->m_next = NULL;
}
void CNode::display(void) const
{
cout << this->m_info;
}
CNode::CNode(int x)
{
this->m_info = x;
this->m_next = 0;
}
/*
SAMPLE RUN:
Testing operator <
-------------------
41
67
n1 is less than n2
34
0
m1 is NOT less than m2
Testing operator <=
--------------------
n1=69
n2=24
n1 is NOT <= n2
n2 is <= n1
Testing operator !=
--------------------
n1=78
n2=58
n1 is != n2
n1 is = n2
Press any key to continue
*/
|