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
|
#include <cstdlib>
#include <time.h>
#include <fstream>
#include <cstring>
#include "vector_jar.h"
#include "llist_jar.h"
void writeJarToFile(Jar& J1, string fileName)
{
ofstream ofs(fileName.c_str());
ofs << J1;
ofs.close();
}
void readJarFromFile(Jar& J2, string fileName)
{
ifstream ifs(fileName.c_str());
while(ifs)
{
Marble m1;
ifs >> m1;
J2.add(m1);
}
}
int main()
{
srand(time(0)); // for random marbles
int jarflag;
// if the user enter 1, then you should test vector jars;
// otherwise, enter 2 to test list jars.
cout << "Which kind of jar do you want to test? "
"(1 - vector jar; 2 - list jar)" << endl;
cin >> jarflag;
jarflag = 1;
//do testing as instructed;
// show a menu with choices to let the user to choose ways
//to generate vector jar objects: VectorJar
jarflag = 2;
//do testing as instructed;
// show a menu with choices to let the user to choose
if(jarflag==1)
{
VectorJar jar1(10), jar2(10), jar3, jar4, jar5, J1, J2, J3;
}
else if(jarflag==2)
{
ListJar jar1(10), jar2(10), jar3, jar4, jar5, J1, J2, J3;
}
else
{
cout << "Wrong number entered. Testing list jar." << endl;
LListJar jar1(10), jar2(10), jar3, jar4, jar5, J1, J2, J3;
}
//Testing Sequence
cout <<"Jar 1 was created with 10 randomly generated marbles"<<endl;
cout <<"Jar 2 was created with 10 randomly generated marbles"<<endl;
cout << "Contents of Jar 1:\n" << jar1;
cout << "Contents of Jar 2:\n" << jar2;
jar3.merge(jar1,jar2);
cout <<"Contents of the merge Jar 1 & 2:\n" << jar3;
jar4.intersection(jar1,jar2);
cout <<"Contents of the intersection of Jar 1 & 2:\n" << jar4;
jar5.difference(jar1,jar2);
cout <<"Contents of the difference of Jar 1 & 2:\n" << jar5;
//Begin Menu
char selection;
char removeToken;
string filename = "jar.out";
int generatedJarCount = 0;
bool hasWrittenFiles = false;
do {
cout << "Please choose from the following choices: " << endl
<< "(a) Enter the number of marbles to create a jar." << endl
<< "(b) Print the jar to a file." << endl
<< "(c) Read a jar from a file." << endl
<< "(d) Remove a specific marble with color." << endl
<< "(e) Remove a specific marble with size" << endl
<< "(f) Test merge with 2 jars read from files, print the resulting jar to the file named \"merge.out\""<< endl
<< "(g) Test complement (difference) with 2 jars read from files, print the resulting jar to the file named \"completion.out\"" << endl
<< "(h) Test intersection with 2 jars read from files, print the resulting jar to the file named \"intersection.out\"" << endl
<< "(i) Exit the program" << endl;
cin >> selection;
switch (selection)
{
case 'a':
case 'A':
{
cout << "Please enter the number of marbles to generate a jar:";
int n;
cin >> n;
if(generatedJarCount%2 == 0) {
//J1(n); To FIX LATER
cout << endl << "Jar J1 (re)generated with " << J1.marble_number() << " marbles." << endl;
cout << J1;
}
else {
//J2(n); To FIX LATER
cout << endl << "Jar J2 (re)generated with " << J1.marble_number() << " marbles." << endl;
cout << J2;
}
generatedJarCount++;
}
break;
case 'b':
case 'B':
{
if(generatedJarCount < 2) {
cout << "You need to create two jars before they can be printed. (Try menu item 'a')" << endl;
break;
}
writeJarToFile(J1, "jar1.in");
writeJarToFile(J2, "jar2.in");
hasWrittenFiles = true;
}
break;
case 'c':
case 'C':
{
cout << "Please enter the filename you would like to read from:";
cin >> filename;
if(generatedJarCount%2 == 0) {
readJarFromFile(J1, filename);
cout << endl << "Jar J1 (re)generated with " << J1->marble_number() << " marbles." << endl;
}
else {
readJarFromFile(J2, filename);
cout << endl << "Jar J2 (re)generated with " << J2->marble_number() << " marbles." << endl;
}
generatedJarCount++;
}
break;
case 'd':
case 'D':
{
cout << "Please input the color you would like to remove from j1, (r,y,b,g):";
cin >> removeToken;
Marble m1;
switch(removeToken) {
case 'r':
m1.set_color(Marble::Color(0));
break;
case 'y':
m1.set_color(Marble::Color(2));
break;
case 'b':
m1.set_color(Marble::Color(1));
break;
case 'g':
m1.set_color(Marble::Color(3));
break;
default:
cout << "Not a valid input" << endl;
break;
}
J1.remove(m1);
}
break;
case 'e':
case 'E':
{
cout << "Please input the size you would like to remove from j1, (s/m/l):";
cin >> removeToken;
Marble m1;
switch(removeToken) {
case 's':
m1.set_size(Marble::Size(0));
break;
case 'm':
m1.set_size(Marble::Size(1));
break;
case 'l':
m1.set_size(Marble::Size(2));
break;
default:
cout << "Not a valid input" << endl;
break;
}
J1.remove(m1);
}
break;
case 'f':
case 'F':
{
if(hasWrittenFiles) {
readJarFromFile(J1, "jar1.in");
readJarFromFile(J2, "jar2.in");
J3.merge(J1,J2);
writeJarToFile(J3, "merge.out");
}
else
cout << "You have not written any files yet, and this operation requires two written files. (try menu item b)" << endl;
}
break;
case 'g':
case 'G':
{
if(hasWrittenFiles) {
readJarFromFile(J1, "jar1.in");
readJarFromFile(J2, "jar2.in");
J3.intersection(J1,J2);
writeJarToFile(J3, "intersect.out");
}
else
cout << "You have not written any files yet, and this operation requires two written files. (try menu item b)" << endl;
}
break;
case 'h':
case 'H':
{
if(hasWrittenFiles) {
readJarFromFile(J1, "jar1.in");
readJarFromFile(J2, "jar2.in");
J3.difference(J1,J2);
writeJarToFile(J3, "difference.out");
}
else
cout << "You have not written any files yet, and this operation requires two written files. (try menu item b)" << endl;
}
break;
}
} while(selection != 'i' || selection !='I');
}
|