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 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328
|
#include "lab5header.h"
#include <iostream>
#include <fstream>
#include <iomanip>
/************************************************/
//
//
//
//Lab4(No leaks)
//
//
//
//
/***********************************************/
//initialize pointer
starFleet::starFleet()
{
//for (int i = 0; i < FLEET_SIZE; i++)
//{
// Ships[i] = NULL; // RM, changed from ships to Ships, as per your class.
//}
shipCount = 0;
head = nullptr;
}
starFleet::~starFleet()
{
shipData * temp;
// RM, delete the linked list.
while(head != nullptr)
{
delete[] head->name;
delete[] head->type;
delete[] head->position;
delete[] head->condition;
delete[] head->captain;
delete head;
temp = head->next;
head = temp;
}
}
void starFleet::loadData()
{
shipData * tempShip, *cur, *next;
shipCount = 0; //set the count = 0
ifstream inputFile;
cout << "Loading Data file......." << endl;
inputFile.open("starships.txt");
char tempString[S_SIZE]; // RM, temporary holder for input info.
while (!inputFile.eof())
{
tempShip = new shipData; // RM, Set aside data for a new ship.
inputFile.getline(tempString, S_SIZE, ';');
tempShip->name = new char[strlen(tempString) + 1]; // RM, Set up the data for name, just the right size.
strcpy(tempShip->name, tempString); // RM, Copy the name info into the name array.
inputFile >> tempShip->regNum;
inputFile.ignore();
inputFile.getline(tempString, S_SIZE, ';');
tempShip->type = new char[strlen(tempString) + 1]; // RM, Set up the data for name, just the right size.
strcpy(tempShip->type, tempString);
inputFile.getline(tempString, S_SIZE, ';');
tempShip->position = new char[strlen(tempString) + 1]; // RM, Set up the data for name, just the right size.
strcpy(tempShip->position, tempString);
inputFile.getline(tempString, S_SIZE, ';');
tempShip->condition = new char[strlen(tempString) + 1]; // RM, Set up the data for name, just the right size.
strcpy(tempShip->condition, tempString);
inputFile.getline(tempString, S_SIZE, '\n');
tempShip->captain = new char[strlen(tempString) + 1]; // RM, Set up the data for name, just the right size.
strcpy(tempShip->captain, tempString);
shipCount++;
// here is where you add a new ship to the linked list, just like addstarship.
// I'll get you started with the head pointer.
if (head == nullptr)
{
head = tempShip;
}
else
{
if (head->name > tempShip->name)
{
next = head;
tempShip->next = next;
}
else
{
cur = tempShip;
next = head->next;
while (next != nullptr)
{
if ((cur->name <= tempShip->name))
{
cur->next = head;
tempShip->next = next;
break;
}
else
{
cur = next;
next = next->next;
}
}
if (next == nullptr)
{
cur->next = tempShip;
}
}
tempShip->next = head;
head = tempShip;
}
}
// RM, moved these lines down.
inputFile.close();
cout << endl << "Successfully loaded " << shipCount << " Ships." << endl << endl; // return the total count
return;
}
void starFleet::addstarship()
{
//Linked List Data.
shipData *tempShip, *cur, *next;
tempShip = new shipData;
tempShip->next = nullptr;
fstream file("starships.txt", fstream::app);
char tempName[S_SIZE];
cout << " What is the name of the ship ? : ";
cin.get(tempName, S_SIZE);
tempShip->name = new char[strlen(tempName) + 1]; // + 1 for null.
strcpy(tempShip->name, tempName);
cout << " What is the registry number of the ship ? : ";
cin >> tempShip->regNum;
cin.ignore();
cout << " What is the type of the ship ? : ";
cin.get(tempName, S_SIZE);
tempShip->type = new char[strlen(tempName) + 1]; // + 1 for null.
strcpy(tempShip->type, tempName);
cin.ignore();
cout << " What is the position of the ship ? : ";
cin.get(tempName, S_SIZE);
tempShip->position = new char[strlen(tempName) + 1]; // + 1 for null.
strcpy(tempShip->position, tempName);
cin.ignore();
cout << " What is the condition of the ship ? : ";
cin.get(tempName, S_SIZE);
tempShip->condition = new char[strlen(tempName) + 1]; // + 1 for null.
strcpy(tempShip->condition, tempName);
cin.ignore();
cout << " What is the captain of the ship ? : ";
cin.get(tempName, S_SIZE);
tempShip->captain = new char[strlen(tempName) + 1]; // + 1 for null.
strcpy(tempShip->captain, tempName);
cin.ignore();
// can't use dots with pointers. You have to dereference with (->).
/*************************************************************************/
//Linked List part of the program. (Lab5)
//
//
//
//
/**************************************************************************/
if (head == nullptr)
{
head = tempShip;
}
else
{
if (head->name > tempShip->name)
{
next = head;
tempShip->next = next;
}
else
{
cur = tempShip;
next = head->next;
while (next != nullptr)
{
if ((cur->name <= tempShip->name))
{
cur->next = head;
tempShip->next = next;
break;
}
else
{
cur = next;
next = next->next;
}
}
if (next == nullptr)
{
cur->next = tempShip;
}
}
tempShip->next = head;
head = tempShip;
}
file << '\n' << tempShip->name << ';' << tempShip->regNum << ';' <<
tempShip->type << ';' << tempShip->position << ';' <<
tempShip->condition << ';' << tempShip->captain;
shipCount++;
return;
}
void starFleet::displayFleet()
{
shipData * cur = head;
{
cout << "List all of the fleet!" << endl;
while(cur != nullptr)
{
cout << "-----------------------------------------" << endl;
cout << "Name of the ship: " << cur->name << endl;
cout << "Registry number: " << cur->regNum << endl;
cout << "Type of ship" << cur->type << endl;
cout << "Position of ship: " << cur->position << endl;
cout << "Condition of the ship: " << cur->condition << endl;
cout << "Captain of the ship: " << cur->captain << endl;
cout << "-----------------------------------------" << endl;
cur = cur->next;
}
}
}
bool starFleet::searchFleet()
{
int regNum1;
cout << "Please enter the registry number of the fleet you would like to search for: ";
cin >> regNum1;
cin.ignore();
shipData * cur = head;
while (cur != nullptr)
{
if (cur->regNum == regNum1)
{
cout << "Name of the ship: " << cur->name << endl;
cout << "Registry number: " << cur->regNum << endl;
cout << "Type of ship" << cur->type << endl;
cout << "Position of ship: " << cur->position << endl;
cout << "Condition of the ship: " << cur->condition << endl;
cout << "Captain of the ship: " << cur->captain << endl;
cur = cur->next;
return true;
}
else
{
cur = cur->next;
}
}
if (cur->regNum != regNum1)
{
cout << "this ship does not exsist" << endl;
return false;
}
}
void starFleet::DeleteShip()
{
shipData * currentShip, *previousShip, *nextShip;
char shipName[S_SIZE];
cout << "Delete a Ship." << endl;
cout << "What is the ship that you would like to Delete?: ";
cin >> shipName;
bool success = false;
if (head != nullptr) // Make sure there are numbers in the list.
{
if (head->name == shipName)
{
currentShip = head;
head = head->next;
delete currentShip;
}
else
{
previousShip = currentShip = head;
nextShip = head->next;
while (currentShip != nullptr)
{
if (currentShip->name == shipName)
{
previousShip->next = nextShip;
delete currentShip;
success = true;
break;
}
else
{
previousShip = currentShip;
currentShip = currentShip->next;
if (currentShip != nullptr)
nextShip = currentShip->next;
}
}
if (!success)
{
cout << "The ship was not found in the list." << endl;
}
}
}
else
{
cout << "The list is empty. :(" << endl;
}
}
// RM, getCount implementation.
int starFleet::getCount()
{
return shipCount;
}
|