Question
C++ Copy the solution from problem 5.2. You will change the implementation of the StringOfCars class, but keep the public interface. This implementation will use
C++
Copy the solution from problem 5.2.
You will change the implementation of the StringOfCars class, but keep the public interface. This implementation will use a linked list, rather than an array or vector to hold the cars. Keep all the public function prototypes in the StringOfCars. Discard all the private data and the implementation of the functions; you will rebuild all of these.
Do not change anything outside the StringOfCars class.
Build a new class called Node:
You will build the linked list using Node objects, linked together to make a list.
In the private data of the Node class put two pointers: One with type Node * and name next, which will point to the next node in the linked list, The second with type Car* and name data, which will point to the Car data associated with this node.
Also in the private area create a default constructor that sets the next and data pointers to zero. Because the constructor is private, only friends can use this class.
In the public area of the Node class, make StringOfCars a friend class.
The order of the following three things is important:
Declare the StringOfCars class with: class StringOfCars;
The Node class
The StringOfCars class
This is needed because the Node class uses the StringOfCars and the StringOfCars class uses the Node class.
In the StringOfCars class implementation:
Replace the private data with two pointers of type Node *, and nothing else. Name these two pointers head and tail.
Change the StringOfCars default constructor to set the head and tail pointers to zero.
Rebuild the push function, with the same function heading. Declare a local pointer variable of type Car * named currentCarPtr. Declare a local pointer variable of type Node * named currentNodePtr. Use new to get space in the heap for a Node and save the address of the space in the heap in currentNodePtr Use new get space in the heap for a new Car that is a copy of the car parameter of the push function and save the address of the space in the heap in currentCarPtr
Set the data pointer in this new Node object to point to the new Car object.
If the head pointer is zero set the head and the tail pointer to the value of currentNodePtr else set the next pointer of the Node object pointed to by the tail pointer to the value of currentNodePtr set the next pointer to the value of the currentNodePtr
Do not write a pop function.
Rebuild the output function, with the same function heading. Declare a local pointer variable of type Node * named currentNodePtr - it will point to the Node you are currently working on.
if the head pointer is zero print: NO cars else set the currentNodePointer to the value of the head pointer while the currentNodePointer is not zero print the Car pointed to by the currentNodePointer set the currentNodePtr to the next pointer in the Node pointed to by the currentNodePtr, which now makes the next Node the current Node
Rebuild the copy construtor. Declare a local pointer variable of type Node * named currentNodePtr - it will point to the Node in the oldStringOfCars that you are currently working on. Set the head and tail pointers in the StringOfCars being constructed to zero. If the oldStringOfCars head pointer is not zero: loop while the currentNodePointer is not zero, push the Car pointed to by the data pointer in the current Node, which is pointed to by the currentNodePointer. set the currentNodePtr to the next pointer in the currentNodePtr so we now make the next Node the current Node
Use the data in cardata61.txt to test your program. The data is shown below:
cardata61.txt
Car car1 SP 819487 maintenance false NONE
Car car2 NP 46877 business true Portland
Car car3 NS 157 tender true Saint Louis
FreightCar car4 PVT 123457 tank false Fort Worth
FreightCar car5 MP 98767 box true Saint Louis
FreightCar car6 SP 567897 flat true New York
FreightCar car7 NSF 7877 hopper true Texarkana
PassengerCar car8 KCS 7897 chair true Kansas City
PassengerCar car9 ATSF 147 sleeper true Tucson
PassengerCar car10 B&O 747 combine false Winslow
Submit your code (.cpp file) and your data (.txt file)
This is 5.2
#include
enum Kind { business, maintenance, other, box, tank, flat, otherFreight, chair, sleeper, otherPassenger }; const string KIND_ARRAY[] = { "business", "maintenance", "other", "box", "tank", "flat", "otherFreight", "chair", "sleeper", "otherPassenger" };
class Car { protected: string reportingMark; int carNumber; Kind kind; bool loaded; public: string destination;
void setUp(string rMark, int carNum, string carKind, bool isLoaded, string dest); virtual void setKind(const string & tempKind); void output(); Car& operator=(const Car & carB); friend bool operator==(const Car &carA, const Car &carB);
Car() { setUp("", 0, "other", false, "NONE"); } Car(const Car &oldCar) { setUp(oldCar.reportingMark, oldCar.carNumber, KIND_ARRAY[oldCar.kind], oldCar.loaded, oldCar.destination); kind = oldCar.kind; } Car(const string &reportingMark, const int &carNumber, const string &kind, const bool &loaded, const string &destination) { setUp(reportingMark, carNumber, kind, loaded, destination); } int getCarNum(){ return carNumber; } string getRepMark(){ return reportingMark; }
virtual ~Car() {};
};
class FreightCar : public Car { public: FreightCar() { setUp("", 0, "other", false, "NONE"); } FreightCar(const FreightCar &oldFreight) { setUp(oldFreight.reportingMark, oldFreight.carNumber, KIND_ARRAY[oldFreight.kind], oldFreight.loaded, oldFreight.destination); } FreightCar(const string &reportingMark, const int &carNumber, const string &kind, const bool &loaded, const string &destination) { setUp(reportingMark, carNumber, kind, loaded, destination); } void setKind(const string & stringKind); };
class PassengerCar : public Car { public: PassengerCar() { setUp("", 0, "other", false, "NONE"); } PassengerCar(const PassengerCar &oldPassenger) { setUp(oldPassenger.reportingMark, oldPassenger.carNumber, KIND_ARRAY[oldPassenger.kind], oldPassenger.loaded, oldPassenger.destination); } PassengerCar(const string &reportingMark, const int &carNumber, const string &kind, const bool &loaded, const string &destination) { setUp(reportingMark, carNumber, kind, loaded, destination); } void setKind(const string & stringKind);
};
//StringOfCars declaration for the Node class class StringOfCars;
class Node { private: Node * next; Car * data; Node() { next = nullptr; data = nullptr; } public: friend class StringOfCars;
};
class StringOfCars { private: Node * head; Node * tail;
public: StringOfCars() { head = nullptr; tail = nullptr; }
StringOfCars(const StringOfCars & oldStringOfCars) { Node * currentNodePtr = oldStringOfCars.head; head = nullptr; tail = nullptr;
if (oldStringOfCars.head != nullptr) { while (currentNodePtr != nullptr) { push(*currentNodePtr->data); currentNodePtr = (*currentNodePtr).next; } } }
~StringOfCars() {}
void push(const Car & tempCar); Car* search(int carNum); void addCar(); void output(); };
//Function Prototypes void input(StringOfCars & carArray); void buildCar(string rMark, int carNum, string carKind, bool isLoaded, string dest, StringOfCars & carArray); void buildFreightCar(string rMark, int carNum, string carKind, bool isLoaded, string dest, StringOfCars & carArray); void buildPassengerCar(string rMark, int carNum, string carKind, bool isLoaded, string dest, StringOfCars & carArray);
int main() { StringOfCars stringOfCars1; input(stringOfCars1);
StringOfCars stringOfCars2(stringOfCars1); stringOfCars2.output();
cout << "Enter the car number you'd like to search: "; int carNumChoice; cin >> carNumChoice; cout << " Search results: ";
if(stringOfCars2.search(carNumChoice) != nullptr){ (stringOfCars2.search(carNumChoice))->output(); }else{ cout << "No car found. "; }
int closeWindow; cout << " Enter an integer to close the output window: "; cin >> closeWindow; }
void Car::output() { cout << "Reporting Mark: " << reportingMark << endl; cout << "Car Number : " << carNumber << endl; cout << "Kind : " << KIND_ARRAY[kind] << endl;
//Converts the loaded boolean into a string for output string temp; if (loaded == true) temp = "true"; else temp = "false";
cout << "Loaded : " << temp << endl; cout << "Destination : " << destination << endl; cout << endl;
}
void Car::setUp(string rMark, int carNum, string carKind, bool isLoaded, string dest) { reportingMark = rMark; carNumber = carNum; setKind(carKind); loaded = isLoaded; destination = dest; }
void Car::setKind(const string & stringKind) { if (stringKind == "business") kind = business; else if (stringKind == "maintenance") kind = maintenance; else kind = other; }
Car & Car::operator=(const Car & carB) { setUp(carB.reportingMark, carB.carNumber, KIND_ARRAY[carB.kind], carB.loaded, carB.destination);
return *this; }
bool operator==(const Car &carA, const Car &carB) { bool temp; if (carA.reportingMark == carB.reportingMark && carA.carNumber == carB.carNumber) temp = true; else temp = false; return temp; }
void FreightCar::setKind(const string & stringKind) { if (stringKind == "box") kind = box; else if (stringKind == "tank") kind = tank; else if (stringKind == "flat") kind = flat; else kind = otherFreight; }
void PassengerCar::setKind(const string & stringKind) { if (stringKind == "chair") kind = chair; else if (stringKind == "sleeper") kind = sleeper; else kind = otherPassenger; }
void StringOfCars::addCar(){ string type, order, rMark; cout << "Would you like to add a: 1)Car 2)PassengerCar 3)FrieghtCar 4)Exit "; int choice; cin >> choice; while(choice > 4){ cout << "Sorry, please enter a valid input. "; cin >> choice; } if(choice == 1){ type = "Car"; } else if(choice == 2){ type = "PassengerCar"; } else { type = "FreightCar"; } order = "car11";
cout << "Please enter the rMark: "; cin >> rMark;
cout << "Please enter the car number: "; int carNum; cin >> carNum;
cout << "In one word, what kind of car is it?: "; string carKind; cin >> carKind;
bool isLoaded; isLoaded = true;
cout << "In one word, where is it headed?: "; string dest; cin >> dest;
Car temp(rMark, carNum, carKind, isLoaded, dest); push(temp);
}
Car* StringOfCars::search(int carNum){ Node * pCurr; if (head == nullptr){ cout << "No cars "; }else{ pCurr = head; while(pCurr != nullptr){ if(carNum == (*pCurr->data).getCarNum()){ return &(*pCurr->data); } pCurr = (*pCurr).next; } } Car* temp = nullptr; return temp;
}
void StringOfCars::output() { Node * currentNodePtr; if (head == nullptr) { cout << "No cars "; } else { currentNodePtr = head; int currentCarNumber = 0; while (currentNodePtr != nullptr) { cout << "Car Number " << ++currentCarNumber << endl; (*currentNodePtr->data).output(); currentNodePtr = (*currentNodePtr).next; } } }
void StringOfCars::push(const Car & tempCar) { Car* currentCarPtr = new Car(tempCar); Node* currentNodePtr = new Node;
(*currentNodePtr).data = currentCarPtr;
if (head == nullptr) { head = currentNodePtr; tail = currentNodePtr; } else { (*tail).next = currentNodePtr; tail = currentNodePtr; }
}
void input(StringOfCars & carArray) { string type; string order; string rMark; int carNum; string carKind; bool isLoaded; string dest; ifstream inputFile;
inputFile.open("C:\\Users\\Swapnil\\CLionProjects\\Car\\data.txt");
if (!inputFile) { cerr << "Error while opening the file. Exitting with code 1" << endl; exit(1);
}
while (inputFile.peek() != EOF) { inputFile >> type; inputFile >> order; inputFile >> rMark; inputFile >> carNum; inputFile >> carKind;
//Converts the user inputted true/false string to a boolean value string temp; inputFile >> temp; if (temp == "true") isLoaded = true; else if (temp == "false") isLoaded = false;
//Skips the white space while (inputFile.peek() == ' ') inputFile.get();
getline(inputFile, dest);
if (type == "Car") buildCar(rMark, carNum, carKind, isLoaded, dest, carArray); else if (type == "FreightCar") buildFreightCar(rMark, carNum, carKind, isLoaded, dest, carArray); else if (type == "PassengerCar") buildPassengerCar(rMark, carNum, carKind, isLoaded, dest, carArray); }
inputFile.close(); }
void buildCar(string rMark, int carNum, string carKind, bool isLoaded, string dest, StringOfCars & carArray) { Car obj = Car(rMark, carNum, carKind, isLoaded, dest); carArray.push(obj); }
void buildFreightCar(string rMark, int carNum, string carKind, bool isLoaded, string dest, StringOfCars & carArray) { FreightCar obj = FreightCar(rMark, carNum, carKind, isLoaded, dest); carArray.push(obj); }
void buildPassengerCar(string rMark, int carNum, string carKind, bool isLoaded, string dest, StringOfCars & carArray) { PassengerCar obj = PassengerCar(rMark, carNum, carKind, isLoaded, dest); carArray.push(obj); }
Step by Step Solution
There are 3 Steps involved in it
Step: 1
Get Instant Access to Expert-Tailored Solutions
See step-by-step solutions with expert insights and AI powered tools for academic success
Step: 2
Step: 3
Ace Your Homework with AI
Get the answers you need in no time with our AI-driven, step-by-step assistance
Get Started