c programming here is the problem requiremwnt http voyage 5191935
C++ programming
——————————————————————————————————————
Here is the problem requiremwnt
http://voyager.deanza.edu/~oldham/cis22b-cpp/assignments/assignment_F.html
——————————————————————————————————————
***************************************************
Here is the code for E3
***************************************************
#include // Needed for Input/Output
#include // Needed for File Input/Output
#include // Needed for format output
#include // Needed for exit function
#include // Needed for string variable
using namespace std;
/* ******************** Car ******************** */
class Car {
// Change private to protected in the Car class only.
protected:
string reportingMark;
int carNumber;
string kind;
bool loaded;
string destination;
public:
// A default constructor which calls the setup member function
Car();
// A copy constructor
Car(const Car &obj);
// A constructor that accepts five parameters.
Car(const string reportingMark1, const int carNumber1, const string kind1,
const bool loaded1, const string destination1);
// A destructor that does nothing.
virtual ~Car() {
}
// Takes the five parameters by value
void setUp(string reportingMark1, int carNumber1,
string kind1, bool loaded1, string destination1);
// Prints the member data in a neat format
void output();
// The friend function
friend bool operator==(Car &car1, Car &car2);
Car &operator=(const Car &carB);
virtual void setKind(string kind);
};
/* ******************** FreightCar ******************** */
class FreightCar : public Car {
public:
FreightCar() : Car() { };
FreightCar(const FreightCar &obj) : Car(obj) { };
FreightCar(const string reportingMark1, const int carNumber1, const string kind1,
const bool loaded1, const string destination1) {
setUp(reportingMark1, carNumber1, kind1, loaded1, destination1);
}
~FreightCar() { }
FreightCar &operator=(const FreightCar &carB);
void setKind(string kind);
};
/* ******************** PassengerCar ******************** */
class PassengerCar : public Car {
public:
PassengerCar() : Car() { };
PassengerCar(const PassengerCar &obj) : Car(obj) { };
PassengerCar(const string reportingMark1, const int carNumber1, const string kind1,
const bool loaded1, const string destination1) {
setUp(reportingMark1, carNumber1, kind1, loaded1, destination1);
}
~PassengerCar() { }
PassengerCar &operator=(const PassengerCar &carB);
void setKind(string kind);
};
/* ******************** StringOfCars ******************** */
class StringOfCars {
private:
// Use an array of pointers to Car
Car **ptr;
static const int ARRAY_SIZE = 10;
// an int containing the current number of Cars in the array, named carCount.
int carCount;
public:
// a default constructor which gets space for Car * elements
// set each unused element in the array to zero
StringOfCars() {
ptr = new Car *[ARRAY_SIZE];
for (int i = 0; i
ptr[i] = NULL;
}
carCount = 0;
}
/* A copy constructor which which gets space for Car * elements
It copies the each Car and also copies the carCount.
Use the push function to push each each Car object into the array. */
StringOfCars(const StringOfCars &oldObject) {
ptr = new Car *[ARRAY_SIZE];
carCount = 0;
for (int i = 0; i
ptr[i] = NULL;
push(*(oldObject.ptr)[i]);
}
}
/* The destructor will need to delete each Car pointed to by the array,
and then delete the array.*/
~StringOfCars() {
for (int i = 0; i > ARRAY_SIZE; i++) {
delete ptr[i];
}
delete[]ptr;
}
void output();
void push(const Car &car);
void push(const FreightCar &car);
void push(const PassengerCar &car);
void pop(Car &car);
};
// Function prototype
void input(StringOfCars &string1);
/* ******************** main ******************** */
int main(void) {
StringOfCars string1;
input(string1);
string1.output();
return 0;
}
/* **************************************************
The following is Car member functions
************************************************** */
Car::Car() {
setUp(“”, 0, “other”, false, “NONE”);
}
Car::Car(const Car &obj) {
setUp(obj.reportingMark, obj.carNumber, obj.kind, obj.loaded, obj.destination);
}
Car::Car(const string reportingMark1, const int carNumber1, const string kind1,
const bool loaded1, const string destination1) {
setUp(reportingMark1, carNumber1, kind1, loaded1, destination1);
}
/* *************** Car::setUp *************** */
void Car::setUp(string reportingMark2, int carNumber2,
string kindParm, bool loaded2, string destination2) {
reportingMark = reportingMark2;
carNumber = carNumber2;
(*this).setKind(kindParm);
loaded = loaded2;
destination = destination2;
};
/* *************** Car::output *************** */
void Car::output() {
cout
cout
cout
if (loaded) {
cout
cout
}
else {
cout
cout
}
}
/* ***************** Car::operator= *****************
sets the values in the left hand object from the right hand object
*/
Car &Car::operator=(const Car &carB) {
setUp(carB.reportingMark, carB.carNumber, carB.kind, carB.loaded, carB.destination);
return *this;
}
/* ******************** Car::setKind ********************
set the appropriate kind of car
*/
void Car::setKind(string k) {
if (k == “business” || k == “maintenance”) {
kind = k;
}
else {
kind = “other”;
}
}
/* **************************************************
The following is FreightCar Member Functions
************************************************** */
/* ******************** FreightCar::operator= ********************
sets the values in the left hand object from the right hand object
*/
FreightCar &FreightCar::operator=(const FreightCar &carB) {
setUp(carB.reportingMark, carB.carNumber, carB.kind, carB.loaded, carB.destination);
return *this;
}
/* ******************** FreightCar::setKind ********************
uses only the values: box, tank, flat, otherFreight
*/
void FreightCar::setKind(string k) {
if (k == “box” || k == “tank” || k == “flat”) {
kind = k;
}
else {
kind = “otherFreight”;
}
}
/* **************************************************
The following is PassengerCar Member Functions
************************************************** */
/* ******************** PassengerCar::operator= ********************
sets the values in the left hand object from the right hand object
*/
PassengerCar &PassengerCar::operator=(const PassengerCar &carB) {
setUp(carB.reportingMark, carB.carNumber, carB.kind, carB.loaded, carB.destination);
return *this;
}
/* ******************** PassengerCar::setKind ********************
set the appropriate kind of car
*/
void PassengerCar::setKind(string k) {
if (k == “chair” || k == “sleeper”) {
kind = k;
}
else {
kind = “otherPassenger”;
}
}
/* **************************************************
Global functions
Create a friend function for the function
which tests to see if two objects are equivalent.
************************************************** */
bool operator==(Car &car1, Car &car2) {
if (car1.reportingMark == car2.reportingMark
&& car1.carNumber == car2.carNumber) {
return true;
}
}
/* **************************************************
The following is StringOfCars Member Functions
************************************************** */
/* ************* StringOfCars::output ************ */
void StringOfCars::output() {
if (carCount == 0) {
cout
}
else {
for (int i = 0; i
cout
// Dereference the pointers to get at the Car objects
(*ptr[i]).output();
//ptr[i]->output(); will also work
}
}
}
/* ************* StringOfCars::push *************
allocate space in the heap for one Car object that is a copy of the Car parameter
and then put the pointer to that Car in the array.
Then it will increment the carCount.
*/
void StringOfCars::push(const Car &car) {
if (carCount == ARRAY_SIZE) {
cout
exit(1);
}
else {
Car *ptrToCar;
ptrToCar = new Car;
*ptrToCar = car;
ptr[carCount] = ptrToCar;
carCount++;
}
}
void StringOfCars::push(const FreightCar &car) {
if (carCount == ARRAY_SIZE) {
cout
exit(1);
}
else {
FreightCar *ptrToCar;
ptrToCar = new FreightCar;
*ptrToCar = car;
ptr[carCount] = ptrToCar;
carCount++;
}
}
void StringOfCars::push(const PassengerCar &car) {
if (carCount == ARRAY_SIZE) {
cout
exit(1);
}
else {
PassengerCar *ptrToCar;
ptrToCar = new PassengerCar;
*ptrToCar = car;
ptr[carCount] = ptrToCar;
carCount++;
}
}
/* ************* StringOfCars::pop *************
The pop function will take a Car parameter by reference.
It will copy the Car to the reference parameter.
Then it will delete the Car from the heap.
It will set the entry in the array that is no longer used to 0.
It will decrement the carCount.
*/
void StringOfCars::pop(Car &car) {
if (carCount == 0) {
cout
exit(1);
}
else {
car = *(ptr[carCount – 1]);
delete ptr[carCount – 1];
ptr[carCount – 1] = NULL;
carCount–;
}
}
/* **************************************************
Global functions, Reads data from the file
************************************************** */
void input(StringOfCars &string1) {
string carType;
string carX;
string reportingMark;
int carNumber;
string kind;
bool loaded;
string destination;
// Open the file.
ifstream inputFile(“/Users/Yufeng/Desktop/carE3.txt”);
// If the open fails, send a message to stderr and exit the program.
if (!inputFile) {
cerr
exit(1);
}
else {
// Use a loop to process each line from the file.
while (inputFile.peek() != EOF) {
// Use temporary variable tempForLoaded to hold true or false
string tempForLoaded;
inputFile >> carType >> carX >> reportingMark >> carNumber >> kind >> tempForLoaded;
loaded = tempForLoaded == “false” ? false : true;
// Get rid of the space character
while (inputFile.peek() == ' ') {
inputFile.get();
}
// Read the destination
getline(inputFile, destination);
if (carType == “Car”) {
Car temp(reportingMark, carNumber, kind, loaded, destination);
string1.push(temp);
}
else if (carType == “FreightCar”) {
FreightCar temp(reportingMark, carNumber, kind, loaded, destination);
string1.push(temp);
}
else if (carType == “PassengerCar”) {
PassengerCar temp(reportingMark, carNumber, kind, loaded, destination);
string1.push(temp);
}
else {
cerr
exit(1);
}
} // End of while loop
}
inputFile.close(); // close the file.
}
/*
Car number 1
reportingMark CN
carNumber 819481
kind maintenance
loaded false
destination NONE
Car number 2
reportingMark SLSF
carNumber 46871
kind business
loaded true
destination Memphis
Car number 3
reportingMark AOK
carNumber 156
kind other
loaded true
destination McAlester
Car number 4
reportingMark MKT
carNumber 123456
kind tank
loaded false
destination Fort Worth
Car number 5
reportingMark MP
carNumber 98765
kind box
loaded true
destination Saint Louis
Car number 6
reportingMark SP
carNumber 567890
kind flat
loaded true
destination Chicago
Car number 7
reportingMark GMO
carNumber 7878
kind otherFreight
loaded true
destination Mobile
Car number 8
reportingMark KCS
carNumber 7893
kind chair
loaded true
destination Kansas City
Car number 9
reportingMark PAPX
carNumber 145
kind sleeper
loaded true
destination Tucson
Car number 10
reportingMark GN
carNumber 744
kind otherPassenger
loaded false
destination NONE
*/