//Project25.cpp
//Date 11/02/2001
//Author: Us
////////////////////////////////////////////////////
//includes
////////////////////////////////////////////////////
#include <iostream.h>
#include <string.h>
#include <stdlib.h>
#include <time.h>
////////////////////////////////////////////////////
// constants
////////////////////////////////////////////////////
const int MAX_LEN = 20;
const char *testNames[] =
{
"Chair", "Table", "Spoon", "Knife",
"Phone", "Couch", "Drive", "Mouse",
"Clock", "Cable", "Plier", "Paper",
"Stand", "Stool"
};
const int testWeights[] =
{
11, 22, 33, 44, 55, 66,
77, 88, 99, 12, 13, 14,
15, 16
};
const int MAX_NAMES = sizeof(testNames)/sizeof(testNames[0]);
const int MAX_ITEMS = 10;
////////////////////////////////////////////////////
// test functions
////////////////////////////////////////////////////
void testInput(void);
void testSetName(void);
void testSetWeight(void);
void testConstructor(void);
void testConstructorCh(void);
void testConstructorCopy(void);
void testSet(void);
void testOperatorEqual(void);
void testGetName(void);
void testGetWeight(void);
void testOperatorLessThan(void);
void testOperatorAssign(void);
void testOperatorInsertion(void);
void testOperatorExtraction(void);
void testConstructorCInventory(void);
void testConstructor2CInventory(void);
void testConstructor3CInventory(void);
void testSortBubbleCInventory(void);
void testIsSortedCInventory(void);
void testShuffleCInventory(void);
void testOperatorAssignCInventory(void);
void testSortInsertionCInventory(void);
void testSortByCountingCInventory(void);
void testDisplayDistinctCInventory(void);
void testInsertCInventory(void);
////////////////////////////////////////////////////
// CItem class
////////////////////////////////////////////////////
class CItem
{
private:
char m_name[MAX_LEN+1];
int m_weight;
public:
CItem(void);
CItem(char name[], int weight);
CItem(char ch);
CItem(const CItem &item);
void input(void);
void display(void) const;
void set(char name[], int weight);
void setName(char name[]);
void setWeight(int weight);
char * getName(void);
int getWeight(void);
bool operator < (const CItem &i2);
CItem & operator = (const CItem &i2);
friend bool operator ==(const CItem &i1, const CItem &i2);
friend ostream & operator << (ostream &bob, const CItem &item);
friend istream & operator >> (istream &joe, CItem &item);
};
////////////////////////////////////////////////////
// CInventory class
////////////////////////////////////////////////////
class CInventory
{
private:
int count;
CItem items[MAX_ITEMS];
void swap(CItem &i1, CItem &i2)
{
CItem tempItem;
tempItem = i1;
i1 = i2;
i2 = tempItem;
};
public:
CInventory(void);
CInventory(int n);
CInventory(char ch);
~CInventory(void){cout << "destructor called\n";}
void sortBubble(void);
bool isSorted(void);
void shuffle(void);
void sortSelection(void);
void sortInsertion(void);
void sortByCounting(void);
void displayDistinct(void);
bool insert(const CItem &item); //11/02/2001
CInventory & operator = (const CInventory &inv2);
bool operator == (const CInventory &inv2);
bool operator != (const CInventory &inv2);
friend ostream & operator << (ostream &bob, const CInventory &inv);
friend istream & operator >> (istream &joe, CInventory &inv);
};
////////////////////////////////////////////////////
// main function
////////////////////////////////////////////////////
void main(void)
{
srand( (unsigned)time( NULL ) );
//testInput();
//testConstructor();
//testConstructorCh();
//testConstructorCopy();
//testSetName();
//testSetWeight();
//testSet();
//testOperatorEqual();
//testGetName();
//testGetWeight();
//testOperatorLessThan();
//testOperatorAssign();
//testOperatorInsertion();
//testOperatorExtraction();
//mainProject03();
//testConstructorCInventory();
//testConstructor2CInventory();
//testConstructor3CInventory();
//testSortBubbleCInventory();
//testIsSortedCInventory();
//testShuffleCInventory();
//testSortInsertionCInventory();
//testSortByCountingCInventory();
//testOperatorAssignCInventory();
//testDisplayDistinctCInventory();
testInsertCInventory();
}
////////////////////////////////////////////////////
// bool CInventory::insert(const CItem &item)
/*insert item to inventory
if count = MAX_ITEMS then return false
items[count] = item
count++
return true
*/
////////////////////////////////////////////////////
bool CInventory::insert(const CItem &item) //11/02/2001
{
if (MAX_ITEMS == count)
return false;
else
{
items[count] = item;
count++;
return true;
}
}
//++++++++++++++++++++++++++++++++++++++++++++++++++
// testInsertCInventory
//++++++++++++++++++++++++++++++++++++++++++++++++++
void testInsertCInventory(void)
{
cout << "testInsertCInventory\n";
cout << "====================\n";
for (int i=0; i<=5; i++)
{
CInventory *invPtr;
if (rand()%2)
invPtr = new CInventory('r');
else
invPtr = new CInventory(MAX_ITEMS);
cout << "Original\n";
cout << *invPtr;
CItem item('r');
if(invPtr->insert(item))
cout << "success\n";
else
cout << "OVERFLOW\n";
cout << "After inserting " << item;
cout << *invPtr;
cout << "-------------------\n";
}
}
////////////////////////////////////////////////////
// void displayDistinct(void);
/*
input: [23, 21, 23, 13, 21, 21, 16, 13]
output:[23, 21, 13, 16]
Algorithm0:
display the first value
From 2nd to last display if not displayed
Algorithm1:
display items[0]
for i=1 to count-1 do the following
display items[i] if not displayed
Algorithm2:
display items[0]
for i=1 to count-1 do the following
display items[i] if it does not match with any previous value
Algorithm3:
display items[0]
for i=1 to count-1 do the following
if items[i] <> items[0..i-1] then display it
Algorithm4:
display items[0]
for i=1 to count-1 do the following
visited = false
for j=0 to i-1 do the following
if items[i] = items[j] then visited = true
end for
if not visited then display items[i]
end for
Algorithm5:
display items[0]
for i=1 to count-1 do the following
visited = false
for j=0 to i-1 do the following
if items[i] = items[j] then
visited = true
get out of this for loop
end if
end for
if not visited then display items[i]
end for
Algorithm6:
if count <= 0 exit function
display items[0]
for i=1 to count-1 do the following
visited = false
for j=0 to i-1 do the following
if items[i] = items[j] then
visited = true
get out of this for loop
end if
end for
if not visited then display items[i]
end for
Algorithm7:
for i=0 to count-1 do the following
visited = false
for j=0 to i-1 do the following
if items[i] = items[j] then
visited = true
get out of this for loop
end if
end for
if not visited then display items[i]
end for
*/
////////////////////////////////////////////////////
void CInventory::displayDistinct(void)
////////////////////////////////////////////////////
{
for (int i=0; i<=count-1; i++)
{
bool visited = false;
for(int j=0; j<=i-1; j++)
if (items[i] == items[j])
{
visited = true;
break;
}
if (!visited) cout << items[i];
}
}
//++++++++++++++++++++++++++++++++++++++++++++++++++
// testDisplayDistinctCInventory
//++++++++++++++++++++++++++++++++++++++++++++++++++
void testDisplayDistinctCInventory(void)
{
cout << "testDisplayDistinctCInventory\n";
cout << "=============================\n";
for (int i=0; i<=5; i++)
{
CInventory yourInv('r');
cout << "Original\n";
cout << yourInv;
cout << "DIstinct\n";
yourInv.displayDistinct();
cout << "-------------------\n";
}
for (i=0; i<=5; i++)
{
CInventory *invPtr;
invPtr = new CInventory('r');
cout << "Original\n";
cout << invPtr << endl;
cout << *invPtr;
//delete invPtr;
cout << invPtr << endl;
cout << *invPtr;
//cout << "DIstinct\n";
//yourInv.displayDistinct();
cout << "-------------------\n";
}
}
//++++++++++++++++++++++++++++++++++++++++++++++++++
// testSortByCountingCInventory
//++++++++++++++++++++++++++++++++++++++++++++++++++
void testSortByCountingCInventory(void)
{
cout << "testSortByCountingCInventory\n";
cout << "============================\n";
for (int i=0; i<=5; i++)
{
CInventory yourInv('r');
cout << yourInv;
cout << "After sortByCounting\n";
yourInv.sortByCounting();
cout << yourInv;
if (yourInv.isSorted())
cout << "SORTED\n";
else
cout << "NOT SORTED NOT SORTED\n";
cout << "-------------------\n";
}
}
//void sortByCounting(void);
//Sorting by counting
/*
items[] = [66, 37, 29, 28, 61, 98]
counts[]= { 0, 0, 0, 0, 0, 0}
[66, 37, 29, 28, 61, 98]
{ 0, 0, 0, 0, 0, 0}
[66, 37, 29, 28, 61, 98]
{ 4, 0, 0, 0, 0, 1}
[66, [37, 29, 28, 61, 98]
{ 4, [2, 0, 0, 1, 2}
[66, 37, [29, 28, 61, 98]
{ 4, 2, [1, 0, 2, 3}
[66, 37, 29, [28, 61, 98]
{ 4, 2, 1, [0, 3, 4}
[66, 37, 29, 28, [61, 98]
{ 4, 2, 1, 0, [3, 5}
[66, 37, 29, 28, 61, 98] is items[]
{ 4, 2, 1, 0, 3, 5} is counts[]
place values based on counts
[28, 29, 37, 61, 66, 98] is tItems[]
0 1 2 3 4 5
Version 0:
Initialize the counts[]
Update the counts[] by incrementing
Place values in a new list based on counts[]
Copy new list to old list
Version 1:
//Initialize the counts[]
for i=0 to count-1
counts[i] = 0
//Update the counts[] by incrementing
for i=0 to count-2
for j=i+1 to count-1
if items[i] > items[j] then
counts[i]++
else
counts[j]++
end if
end for
end for
//Place values in a tItems[] based on counts[]
for i=0 to count-1
tItems[counts[i]] = items[i]
end for
//Copy new list to old list
for i=0 to count-1
items[i] = tItems[i]
end for
*/
void CInventory::sortByCounting(void)
{
//Initialize the counts[]
int counts[MAX_ITEMS];
for (int i=0; i<=count-1; i++)
counts[i] = 0;
int k;
cout << "counts[]: ";
for (k=0; k<=count-1; k++)
cout << counts[k] << ' ';
cout << endl;
//Update the counts[] by incrementing
int j;
for (i=0; i<=count-2; i++) //because last value is lonely
for (j=i+1; j<=count-1; j++)
{
if (items[j] < items[i])
counts[i]++;
else
counts[j]++;
cout << "counts[]: ";
for (k=0; k<=count-1; k++)
cout << counts[k] << ' ';
cout << endl;
}
//Place values in a tItems[] based on counts[]
CItem tItems[MAX_ITEMS];
for (i=0; i<=count-1; i++)
tItems[counts[i]] = this->items[i];
//Copy new list to old list
for (i=0; i<=count-1; i++)
this->items[i] = tItems[i];
}
//++++++++++++++++++++++++++++++++++++++++++++++++++
// testSortInsertionCInventory
//++++++++++++++++++++++++++++++++++++++++++++++++++
void testSortInsertionCInventory(void)
{
cout << "testSortInsertionCInventory\n";
cout << "===========================\n";
for (int i=0; i<=5; i++)
{
CInventory yourInv('r');
cout << yourInv;
cout << "After sortInsertion\n";
yourInv.sortInsertion();
cout << yourInv;
if (yourInv.isSorted())
cout << "SORTED\n";
else
cout << "NOT SORTED NOT SORTED\n";
cout << "-------------------\n";
}
}
//void CInventory::sortInsertion(void)
/*
/* Algorithm:
[66], 37, 29, 28, 61, 98
[37, 66], 29, 28, 61, 98
[29, 37, 66], 28, 61, 98
[28, 29, 37, 66], 61, 98
[28, 29, 37, 61, 66], 98
[28, 29, 37, 61, 66, 98]
Version 0:
---------
if count <= 1 then exit function
do the following count-1 times using i=1 to count-1
insert the ith value s.t. items[0..i] are sorted
Version 1:
---------
if count <= 1 then exit function
do the following count-1 times using i=1 to count-1
//insert the ith value s.t. items[0..i] are sorted
k = i
step1:
if item[k] >= items[k-1] then exit for
else
swap items[k], items[k-1]
k = k-1
if k > 0 then goto step1:
Version 2:
---------
if count <= 1 then exit function
do the following count-1 times using i=1 to count-1
//insert the ith value s.t. items[0..i] are sorted
k = i
do
{
if item[k] >= items[k-1] then
exit do (inner)
else
swap items[k], items[k-1]
k = k-1
end if
}
while (k>0)
end do (outer)
Version 3:
---------
if count <= 1 then exit function
do the following count-1 times using i=1 to count-1
//insert the ith value s.t. items[0..i] are sorted
k = i
while k > 0
if item[k] >= items[k-1] then
exit do (inner)
else
swap items[k], items[k-1]
k = k-1
end if
end while
end do (outer)
Version 4:
---------
do the following count-1 times using i=1 to count-1
//insert the ith value s.t. items[0..i] are sorted
k = i
while k > 0
if item[k] >= items[k-1] then
exit do (inner)
else
swap items[k], items[k-1]
k = k-1
end if
end while
end do (outer)
*/
void CInventory::sortInsertion(void)
{
int k;
for (int i=1; i<= count-1; i++)
{
k = i;
while (k > 0)
{
if (!(items[k] < items[k-1]))
break;
else
{
swap(items[k], items[k-1]);
k = k-1;
}
}
}
}
////////////////////////////////////////////////////
//void CInventory::sortSelection(void)
/* Algorithm:
[66, 37, 29, 28, 61]
[61, 37, 29, 28] 66
[28, 37, 29,] 61 66
[28, 29,] 37, 61 66
[28,]29, 37, 61 66
Version #0
Given: items[0..count-1]
n = count
do the following count-1 times
put the index of the max value in iMax
swap items[iMax], items[n-1]
n = n-1
end do
Version #1
Given: items[0..count-1]
n = count
do the following count-1 times
//put the index of the max value in iMax
iMax = 0
do the following n-1 times using k=1 to n-1
if items[k] > items[iMax] then
iMax = k
end if
end do
swap items[iMax], items[n-1]
cout << *this
n = n-1
end do
testSortSelection
do the following 5 times
CInventory testInv('r')
display "Original inv"
display testInv
tetInv.sortSelection()
display "Sorted inv"
end do
*/
////////////////////////////////////////////////////
// sortSelection(void)
////////////////////////////////////////////////////
void CInventory::sortSelection(void)
{
}
////////////////////////////////////////////////////
// operator =
////////////////////////////////////////////////////
CInventory & CInventory::operator = (const CInventory &inv2)
{
this->count = inv2.count;
for (int i=0; i<this->count; i++)
this->items[i] = inv2.items[i];
return *this;
}
//++++++++++++++++++++++++++++++++++++++++++++++++++
// testOperatorAssignCInventory
//++++++++++++++++++++++++++++++++++++++++++++++++++
void testOperatorAssignCInventory(void)
{
cout << "testOperatorAssignCInventory\n";
cout << "============================\n";
for (int i=1; i<=5; i++)
{
CInventory yourInv('r');
CInventory myInv('R');
CInventory hisInv('r');
cout << "yourInv = " << yourInv << endl;
cout << "myInv = " << myInv << endl;
cout << "hisInv = " << hisInv << endl;
yourInv = myInv = hisInv;
cout << "After yourInv = myInv = hisInv;\n";
cout << "yourInv = " << yourInv << endl;
cout << "myInv = " << myInv << endl;
cout << "hisInv = " << hisInv << endl;
cout << "----------------------\n";
}
}
//++++++++++++++++++++++++++++++++++++++++++++++++++
// testShuffleCInventory
//++++++++++++++++++++++++++++++++++++++++++++++++++
void testShuffleCInventory(void)
{
cout << "testShuffleCInventory\n";
cout << "=====================\n";
for (int i=0; i<=5; i++)
{
CInventory yourInv('r');
cout << yourInv;
if (yourInv.isSorted())
cout << "SORTED\n";
else
cout << "NOT SORTED NOT SORTED\n";
cout << "After sortBubble\n";
yourInv.sortBubble();
cout << yourInv;
if (yourInv.isSorted())
cout << "SORTED\n";
else
cout << "NOT SORTED NOT SORTED\n";
cout << "After shuffle\n";
yourInv.shuffle();
cout << yourInv;
if (yourInv.isSorted())
cout << "SORTED\n";
else
cout << "NOT SORTED NOT SORTED\n";
cout << "-------------------\n";
}
}
////////////////////////////////////////////////////
// shuffle function
////////////////////////////////////////////////////
void CInventory::shuffle(void)
{
if (this->count <= 1)
return;
CItem tempItem;
int pick1, pick2;
for (int i=1; i<=2*this->count; i++)
{
pick1 = rand()%this->count;
pick2 = rand()%this->count;
swap(items[pick1], items[pick2]);
/*
tempItem = items[pick1];
items[pick1] = items[pick2];
items[pick2] = tempItem;
*/
}
}
//++++++++++++++++++++++++++++++++++++++++++++++++++
// testIsSortedCInventory
//++++++++++++++++++++++++++++++++++++++++++++++++++
void testIsSortedCInventory(void)
{
cout << "testIsSortedCInventory\n";
cout << "======================\n";
for (int i=0; i<=5; i++)
{
CInventory yourInv('r');
cout << yourInv;
if (yourInv.isSorted())
cout << "SORTED\n";
else
cout << "NOT SORTED NOT SORTED\n";
cout << "After sortBubble\n";
yourInv.sortBubble();
cout << yourInv;
if (yourInv.isSorted())
cout << "SORTED\n";
else
cout << "NOT SORTED NOT SORTED\n";
cout << "-------------------\n";
}
}
////////////////////////////////////////////////////
// isSorted function
////////////////////////////////////////////////////
bool CInventory::isSorted(void)
{
for (int i=0; i<=count-2; i++)
if (items[i+1] < items[i])
return false;
return true;
}
//++++++++++++++++++++++++++++++++++++++++++++++++++
// testSortBubbleCInventory
//++++++++++++++++++++++++++++++++++++++++++++++++++
void testSortBubbleCInventory(void)
{
cout << "testSortBubbleCInventory\n";
cout << "========================\n";
for (int i=0; i<=5; i++)
{
CInventory yourInv('r');
cout << yourInv;
cout << "After sortBubble\n";
yourInv.sortBubble();
cout << yourInv;
cout << "-------------------\n";
}
}
////////////////////////////////////////////////////
// sortBubble function
////////////////////////////////////////////////////
void CInventory::sortBubble(void)
{
bool sorted;
int i;
CItem tempItem;
do
{
sorted = true;
for (i=0; i<=count-2; i++)
{
if (items[i+1] < items[i])
{
//use swap
tempItem = items[i];
items[i] = items[i+1];
items[i+1] = tempItem;
sorted = false;
}
}
}
while (!sorted);
}
//++++++++++++++++++++++++++++++++++++++++++++++++++
// testConstructor3CInventory
//++++++++++++++++++++++++++++++++++++++++++++++++++
void testConstructor3CInventory(void)
{
cout << "testConstructor3CInventory\n";
cout << "=========================\n";
for (int i=1; i<=5; i++)
{
CInventory yourInv('r');
CInventory myInv('R');
CInventory hisInv('s');
cout << yourInv;
cout << endl;
cout << myInv;
cout << endl;
cout << hisInv;
cout << "-------------------\n";
}
}
////////////////////////////////////////////////////
// construct inventory of random random items
////////////////////////////////////////////////////
CInventory::CInventory(char ch)
{
if (('r' == ch) || ('R' == ch))
{
this->count = rand()%MAX_ITEMS;
for (int i=0; i<=this->count-1; i++)
{
CItem item('r');
this->items[i] = item;
}
}
else
this->count =0;
}
//++++++++++++++++++++++++++++++++++++++++++++++++++
// testConstructor2CInventory
//++++++++++++++++++++++++++++++++++++++++++++++++++
void testConstructor2CInventory(void)
{
cout << "testConstructor2CInventory\n";
cout << "=========================\n";
for (int i=-1; i<=5; i++)
{
CInventory yourInv(i);
cout << yourInv;
cout << "-------------------\n";
}
}
////////////////////////////////////////////////////
// construct inventory of n random items
////////////////////////////////////////////////////
CInventory::CInventory(int n)
{
if (n < 0)
n = -n;
this->count = n;
for (int i=0; i<=n-1; i++)
{
CItem item('r');
this->items[i] = item;
}
}
////////////////////////////////////////////////////
// operator << overloaded for CInventory
////////////////////////////////////////////////////
ostream & operator << (ostream &bob, const CInventory &inv)
{
bob << "inventory(" << inv.count << ")=\n";
for (int i=0; i<=inv.count-1; i++)
bob << inv.items[i];
return bob;
}
//++++++++++++++++++++++++++++++++++++++++++++++++++
// testConstructorCInventory
//++++++++++++++++++++++++++++++++++++++++++++++++++
void testConstructorCInventory(void)
{
cout << "testConstructorCInventory\n";
cout << "=========================\n";
for (int i=1; i<=5; i++)
{
CInventory yourInv;
cout << yourInv;
cout << "-------------------\n";
}
}
////////////////////////////////////////////////////
// default constructor for CInventory
////////////////////////////////////////////////////
CInventory::CInventory(void)
{
count = 0;
}
////////////////////////////////////////////////////
//overload >> for CItem
////////////////////////////////////////////////////
istream & operator >> (istream &joe, CItem &item)
{
cout << "item m_name: ";
joe >> item.m_name;
cout << "item m_weight: ";
joe >> item.m_weight;
return joe;
}
//++++++++++++++++++++++++++++++++++++++++++++++++++
// test operator >>
//++++++++++++++++++++++++++++++++++++++++++++++++++
void testOperatorExtraction(void)
{
cout << "Test operator >>\n";
cout << "=================\n";
for (int i=1; i<=5; i++)
{
CItem item1;
cin >> item1;
cout << item1;
cout << "---------------------------\n";
}
}
////////////////////////////////////////////////////
//overload << for CItem
////////////////////////////////////////////////////
ostream & operator << (ostream &bob, const CItem &item)
{
bob << '[' << item.m_name << ", ";
bob << item.m_weight << ']' << endl;
return bob;
}
//++++++++++++++++++++++++++++++++++++++++++++++++++
// test operator <<
//++++++++++++++++++++++++++++++++++++++++++++++++++
void testOperatorInsertion(void)
{
cout << "Test operator <<\n";
cout << "=================\n";
for (int i=1; i<=5; i++)
{
CItem item1('r');
cout << "item1 = "; item1.display();
cout << "item1 = " << item1 << endl;
cout << item1 << item1;
cout << "---------------------------\n";
}
}
////////////////////////////////////////////////////
// operator = overloaded
////////////////////////////////////////////////////
CItem & CItem::operator = (const CItem &i2)
{
strcpy(m_name, i2.m_name);
this->m_weight = i2.m_weight;
return *this;
}
//++++++++++++++++++++++++++++++++++++++++++++++++++
// testOperatorAssign
//++++++++++++++++++++++++++++++++++++++++++++++++++
void testOperatorAssign(void)
{
cout << "Test operator =\n";
cout << "===============\n";
for (int i=1; i<=5; i++)
{
CItem item1('r'), item2('r'), item3('r');
cout << "item1 = "; item1.display();
cout << "item2 = "; item2.display();
cout << "item3 = "; item3.display();
item1 = item2 = item3;
//item1.operator= (item2.operator= item3);
//item1.operator = (item2.operator = (item3));
item2.operator = (item3);
item1.operator = (item2);
cout << "After item1 = item2 = item3;\n";
cout << "item1 = "; item1.display();
cout << "item2 = "; item2.display();
cout << "item3 = "; item3.display();
cout << "---------------------------\n";
}
}
////////////////////////////////////////////////////
// operator < overloaded for CItem class objects
// returns true if weight of i1 is less than that of i2
// else returns false
////////////////////////////////////////////////////
bool CItem::operator < (const CItem &i2)
{
return (this->m_weight < i2.m_weight);
/*
if (this->m_weight < i2.m_weight)
return true;
else
return false;
*/
}
//++++++++++++++++++++++++++++++++++++++++++++++++++
// testOperatorLessThan
//++++++++++++++++++++++++++++++++++++++++++++++++++
void testOperatorLessThan(void)
{
cout << "Test operator <\n";
cout << "===============\n";
for (int i=1; i<=5; i++)
{
CItem item1('r'), item2('r'), item3('r');
cout << "item1 = "; item1.display();
cout << "item2 = "; item2.display();
cout << "item3 = "; item3.display();
if (item1 < item2)
cout << "item1 is lighter than item2\n";
else
cout << "item1 is not lighter than item2\n";
if (item1 < item3)
cout << "item1 is lighter than item3\n";
else
cout << "item1 is not lighter than item3\n";
cout << "---------------------------\n";
}
}
////////////////////////////////////////////////////
// getWeight
// returns the weight
////////////////////////////////////////////////////
int CItem::getWeight(void)
{
return m_weight;
};
//++++++++++++++++++++++++++++++++++++++++++++++++++
// testGetWeight
//++++++++++++++++++++++++++++++++++++++++++++++++++
void testGetWeight(void)
{
cout << "test getWeight\n";
for (int i=1; i<=5; i++)
{
CItem i('r');
cout << "item = "; i.display();
cout << "weight = " << i.getWeight() << endl;
cout << "-----------------------------\n";
}
}
////////////////////////////////////////////////////
// getName
// returns the name
////////////////////////////////////////////////////
char * CItem::getName(void)
{
return m_name;
};
//++++++++++++++++++++++++++++++++++++++++++++++++++
// testGetName
//++++++++++++++++++++++++++++++++++++++++++++++++++
void testGetName(void)
{
cout << "test getName\n";
for (int i=1; i<=5; i++)
{
CItem i('r');
cout << "item = "; i.display();
cout << "name = " << i.getName() << endl;
cout << "-----------------------------\n";
}
}
////////////////////////////////////////////////////
// operator ==
// compares two items for equality
////////////////////////////////////////////////////
bool operator ==(const CItem &i1, const CItem &i2)
{
//return (c1&&c2);
if (i1.m_weight != i2.m_weight)
return false;
if (strcmp(i1.m_name, i2.m_name) != 0)
return false;
return true;
}
//++++++++++++++++++++++++++++++++++++++++++++++++++
// testOperatorEqual
//++++++++++++++++++++++++++++++++++++++++++++++++++
void testOperatorEqual(void)
{
cout << "Test operator equal\n";
cout << "===================\n";
for (int i=1; i<=5; i++)
{
CItem item1('r'), item2('r'), item3(item1);
cout << "item1 = "; item1.display();
cout << "item2 = "; item2.display();
cout << "item3 = "; item3.display();
if (item1 == item2)
cout << "item1 is equal to item2\n";
else
cout << "item1 is not equal to item2\n";
if (item1 == item3)
cout << "item1 is equal to item3\n";
else
cout << "item1 is not equal to item3\n";
cout << "---------------------------\n";
}
}
////////////////////////////////////////////////////
// setWeight(int weight)
// sets the m_weight data member to weight
////////////////////////////////////////////////////
void CItem::setWeight(int weight)
{
m_weight = weight;
}
//++++++++++++++++++++++++++++++++++++++++++++++++++
// testSetWeight
//++++++++++++++++++++++++++++++++++++++++++++++++++
void testSetWeight(void)
{
cout << "Test setWeight(weight)\n";
for (int i=1; i<=5; i++)
{
CItem item('r');
item.display();
int tWeight = rand()%100;
cout << "tWeight = " << tWeight << endl;
item.setWeight(tWeight);
cout << "After item1.setWeight(tWeight);\n";
item.display();
cout << "---------------------------\n";
}
}
////////////////////////////////////////////////////
// setName(char name)
// sets the m_name data member to name
////////////////////////////////////////////////////
void CItem::setName(char name[])
{
strcpy(m_name, name);
}
//++++++++++++++++++++++++++++++++++++++++++++++++++
// testSetName
//++++++++++++++++++++++++++++++++++++++++++++++++++
void testSetName(void)
{
cout << "Test setName(name)\n";
for (int i=1; i<=5; i++)
{
CItem item('r');
item.display();
int i = rand()%MAX_NAMES;
char tName[MAX_LEN+1];
strcpy(tName, testNames[i]);
cout << "tName = \"" << tName << "\"" << endl;
item.setName(tName);
cout << "After item1.setName(tName);\n";
item.display();
cout << "---------------------------\n";
}
}
////////////////////////////////////////////////////
//CItem (item) copy constructor
// constructs an object which is a copy given object
////////////////////////////////////////////////////
CItem::CItem(const CItem &item)
{
strcpy(this->m_name, item.m_name);
this->m_weight = item.m_weight;
//(*this).m_weight = item.m_weight;
//cout << "copy constructor called\n";
}
//++++++++++++++++++++++++++++++++++++++++++++++++++
// testConstructorCopy
//++++++++++++++++++++++++++++++++++++++++++++++++++
void testConstructorCopy(void)
{
cout << "Test copy Constructor\n";
for (int i=1; i<=5; i++)
{
CItem item1('r');
CItem item2(item1);
item1.display();
item2.display();
}
}
////////////////////////////////////////////////////
//CItem (ch) constructor
// constructs an object with random name and weight
////////////////////////////////////////////////////
CItem::CItem(char ch)
{
if ('r' == ch )
{
int i = rand()%MAX_NAMES;
strcpy(m_name, testNames[i]);
m_weight = testWeights[i];
}
else
{
strcpy(m_name, "");
m_weight = -99;
}
}
//++++++++++++++++++++++++++++++++++++++++++++++++++
// testConstructorCh
//++++++++++++++++++++++++++++++++++++++++++++++++++
void testConstructorCh(void)
{
cout << "Test ConstructorCh\n";
CItem item1('r');
CItem item2('m');
item1.display();
item2.display();
}
////////////////////////////////////////////////////
//CItem (name, weight) constructor
// constructs an object with given name and weight
////////////////////////////////////////////////////
CItem::CItem(char name[], int weight)
{
strcpy(m_name, name);
m_weight = weight;
}
//++++++++++++++++++++++++++++++++++++++++++++++++++
// testConstructor
//++++++++++++++++++++++++++++++++++++++++++++++++++
void testConstructor(void)
{
cout << "Test Constructor(name, weight)\n";
CItem item("Chair", 150);
item.display();
}
////////////////////////////////////////////////////
// display
////////////////////////////////////////////////////
void CItem::display(void) const
{
cout << '[' << m_name << ", ";
cout << m_weight << ']' << endl;
}
////////////////////////////////////////////////////
//constructor default
////////////////////////////////////////////////////
CItem::CItem(void)
{
//cout << "Default Constructor called\n";
strcpy(m_name, "");
m_weight = -99;
}
////////////////////////////////////////////////////
//input function
// This is a member function for CItem class
// it gets the name and weight from the keyboard
// and sets the values of name and weight data members
////////////////////////////////////////////////////
void CItem::input(void)
{
cout << "item m_name: ";
cin >> m_name;
cout << "item m_weight: ";
cin >> m_weight;
}
//++++++++++++++++++++++++++++++++++++++++++++++++++
// testInput
//++++++++++++++++++++++++++++++++++++++++++++++++++
void testInput(void)
{
cout << "test input function\n";
CItem item1;
item1.display();
item1.input();
item1.display();
}
////////////////////////////////////////////////////
// set(name, weight)
////////////////////////////////////////////////////
void CItem::set(char name[], int weight)
{
strcpy(m_name, name);
m_weight = weight;
}
//++++++++++++++++++++++++++++++++++++++++++++++++++
// testSet
//++++++++++++++++++++++++++++++++++++++++++++++++++
void testSet(void)
{
cout << "Test set(name, weight)\n";
for (int i=1; i<=5; i++)
{
CItem item('r');
item.display();
int i = rand()%MAX_NAMES;
char tName[MAX_LEN+1];
strcpy(tName, testNames[i]);
cout << "tName = \"" << tName << "\"" << endl;
int tWeight = rand()%100;
cout << "tWeight = " << tWeight << endl;
item.set(tName, tWeight);
cout << "After item.set(tName, tWeight);\n";
item.display();
cout << "------------------------------------\n";
}
}
|