1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239
|
/*
Franz Inbavannan
Assignment 1
DRIVER 1
CIS 200
Instructor: Tsui
*/
#include<stack>
#include<iostream>
#include<string>
using namespace std;
struct anything //to test number list
{
int value;
string name;
};
int anynumber; //no. of things in a.
template<class T>
class list
{
private:
#define MAXSIZE 50
T ar[MAXSIZE];
int number; // number of things in a.
public:
list();
void getThing(T &n);
void getint(int &n);
bool isFull();
// returns true if the list is full, false otherwise
bool isEmpty();
// returns true if the list is empty, false otherwise
void add(T x);
/*
purpose: add x to the list
precondition: list not full;
postcondition: list contains one more thing, i.e. x
*/
T remove();
/*
purpose: remove one thing from the list
precondition: list not empty;
postcondition: list contains one less thing and it is returned back
*/
};
template <class T>
list<T>::list()
{
number=0; //Doing so initializes the number of values to zero.
}
/*******************************************************************************************************************************************/
//Function defenition for what happens when the list is full
template <class T>
bool list <T>::isFull()
{ if(number==50)
{return true;}
else
{return false;}
}
/*******************************************************************************************************************************************/
//Function defenition for what happens when the list is empty
template <class T>
bool list <T>::isEmpty()
{ if(number==0)
{return true;}
else
{return false;}
}
/*******************************************************************************************************************************************/
//Function defenition for adding one item, x.
template <class T>
void list<T>::add(T x)
{ if (isFull() == false) //This limits the number of items that can be added to 50, or MAXSIZE
{ar[number]=x; //Assigns new value
number=number+1; } //Changes the number of elements in the array, and gives you updated info
}
/*******************************************************************************************************************************************/
//Function defenition for removing one item, x.
template <class T>
T list<T>::remove()
{
return ar[number-1]; //Returns last number in array which was removed
number--; //Decrease number 1, this way the removed number is no longer able to access.
}
template <class T>
void list<T>::getThing(T &n)
{
//n.name=a[0].name;
}
template <class T>
void list<T>::getint(int &n)
{
n=a[0];
}
#include<iostream>
#include<string>
using namespace std;
int main()
{cout<<"1=true"<<endl;
cout<<"0=false"<<endl<<endl;
/*TEST OF STRUCT*/
list <anything> mylist;
anything mything1;
mything1.value=1;
mything1.name="Hi";
for(int i=1; i<=50; i++)//A loop so that the value1 itself can be addded to the array 50 times.
{
mylist.add(mything1);
mylist.getThing(mything1);
}
cout<<"After 'Hi' has been added to the struct, we call isEmpty and it returns: "<<mylist.isEmpty<<endl<<endl;
//The below is for testing..bool type functions isEmpty, and isFull
/****************************************************************************************************************************************/
list <int> FX;
int value1=7;
cout<<"A list by the name of \"FX\" was created \n and when the function 'isEmpty' is called, it returns : "<<FX.isEmpty()<<endl<<endl<<endl;
//The value that is expected is 1, and that's what we get.
list <int> FM;
for(int i=1; i<=50; i++)//A loop so that the value1 itself can be addded to the array 50 times.
{
FM.add(value1);
}
cout<<"50 of value1 have been added to ar, and when the function 'isFull' is called, it returns: "<<FM.isFull()<<endl<<endl<<endl;
//Expected value: 1
//Returned value: 1; the isFull functions works properly
/*****************************************************************************************************************************************/
FX.add(value1);
cout<<"The int \"7\" was added to list \"FX.\" \n Now isEmpty returns: "<<FX.isEmpty()<<endl<<endl;
//The expeced value is 0, which is what is returned.
cout<<"The 'remove' function is called and the value it returns is "<<FX.remove()<<endl<<"The last element in the array."<<endl<<endl;
//Expected value to be returned is 7, and that's what we get.
cout<<"Now, the function isEmpty is called and the value it returns is "<<FM.isEmpty()<<endl;
cout<<"IT WORKS!"<<endl;
cout<<endl<<endl<<endl<<"TEST PART 2"<<endl<<endl<<endl;
////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
list <int> XX;
int value3=5;
cout<<"A list by the name of \"XX\" was created \n and when the function 'isEmpty' is called, it returns : "<<XX.isEmpty()<<endl<<endl<<endl;
//The value that is expected is 1, and that's what we get.
list <int> MM;
for(int i=1; i<=50; i++)//A loop so that the value1 itself can be addded to the array 50 times.
{
FM.add(value1);
}
cout<<"50 of value3 have been added to ar, and when the function 'isFull' is called, it returns: "<<FM.isFull()<<endl<<endl<<endl;
//Expected value: 1
//Returned value: 1; the isFull functions works properly
/*****************************************************************************************************************************************/
XX.add(value3);
cout<<"The string \"FRANZ\" was added to list \"XX.\" \n Now isEmpty returns: "<<XX.isEmpty()<<endl<<endl;
//The expeced value is 0, which is what is returned.
cout<<"The 'remove' function is called and the value it returns is "<<XX.remove()<<endl<<"The last element in the array."<<endl<<endl;
//Expected value to be returned is Franz, and that's what we get.
cout<<"Now, the function isEmpty is called and the value it returns is "<<MM.isEmpty()<<endl;
cout<<"IT WORKS!"<<endl;
system("pause");
return 0;
}
|