Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

This is Problem F1 #include <iostream> #include <string> #include <iomanip> #inc

ID: 3677576 • Letter: T

Question

This is Problem F1

#include <iostream>
#include <string>
#include <iomanip>
#include <fstream>
#include <cstdlib>
#include<bits/stdc++.h>
#define nullptr NULL

using namespace std;
enum Kind { business, maintenance, other, box, tank, flat, otherFreight, chair, sleeper, otherPassenger };
const string KIND_ARRAY[10] = { "business", "maintenance", "other", "box", "tank", "flat", "otherFreight",
"chair", "sleeper", "otherPassenger" };

class Car
{
public:
string reportingMark;
int carNumber;
Kind kind;
bool loaded;
string destination;
public:
Car()
{
  setUp("", 0, "other", false, "NONE");
}
Car(Car &obj)
{
  setUp(obj.reportingMark, obj.carNumber, KIND_ARRAY[obj.kind], obj.loaded, obj.destination);
}
Car(const string &rmark, const int &cNum, const string &k, const bool &l, const string &dest1)
{
  setUp(rmark, cNum, k, l, dest1);
}
virtual ~Car(){}
void setUp(string reportingMark1, int cNum, string k, bool l, string dest);
void output();
friend bool operator==(const Car &car1, const Car &car2);
Car & operator=(const Car & carB);
virtual void setKind(const string &x);
};
class Node
{
Node* next;
Car *data;
Node()
{

}
public:
friend class StringOfCars;
};

class StringOfCars
{
private:
Node *head;
Node *tail;
  Car **ptr;
  static const int ARRAY_MAX_SIZE = 10;
int size;
public:
StringOfCars()
{
   head=NULL;
   tail=NULL;
   ptr = new Car*[ARRAY_MAX_SIZE];
  size = 0;
}
StringOfCars(const StringOfCars &oldObj)
{
   Node *currentNodeptr=new Node;
   currentNodeptr->next=oldObj.head;
  head=NULL;
  tail=NULL;
  if(oldObj.head!=NULL)
    {
      while(currentNodeptr!=NULL)
      {
        Car *temp=currentNodeptr->data;
        push(*temp);
        currentNodeptr=currentNodeptr->next;
      }
    }
}
~StringOfCars()
{
  for (int x = 0; x < size;x++)
  {
   delete ptr[x];
   ptr[x] = nullptr;
  }
  delete[]ptr;
  ptr = nullptr;
}
void push(Car &x);
void pop(Car &x);
void output();
};
class FreightCar :public Car
{
public:
FreightCar(){
  setUp("", 0, "other", false, "NONE");
}
FreightCar(const FreightCar &obj) {
  setUp(obj.reportingMark, obj.carNumber, KIND_ARRAY[obj.kind], obj.loaded, obj.destination);
  setKind(KIND_ARRAY[obj.kind]);
}
FreightCar(const string &rM, const int &cN, const string &kind, const bool &load, const string &dest) {
  setUp(rM, cN, kind, load, dest);
}
void setKind(const string &x);
};
class PassengerCar :public Car
{
public:
PassengerCar(){
  setUp("", 0, "other", false, "NONE");
}
PassengerCar(const PassengerCar &obj){
  setUp(obj.reportingMark, obj.carNumber, KIND_ARRAY[obj.kind], obj.loaded, obj.destination);
}
PassengerCar(const string &rM, const int &cN, const string &kind, const bool &load, const string &dest){
  setUp(rM, cN, kind, load, dest);
}
void setKind(const string &x);
};
void input(StringOfCars &obj);
bool operator==(const Car &car1, const Car &car2);
void buildCar(StringOfCars &obj,string rMark, int carNumber, string kind, bool loaded, string destination);
void buildFreightCar(StringOfCars &obj, string rMark, int carNumber, string kind, bool loaded, string destination);
void buildPassengerCar(StringOfCars &obj, string rMark, int carNumber, string kind, bool loaded, string destination);


int main()
{
StringOfCars stringOfCars1;
input(stringOfCars1);
stringOfCars1.output();
return 0;
}
/**********************
*      setup car     *
**********************/


void Car::setUp(string reportingMark1, int cNum, string k, bool l, string dest)
{
reportingMark = reportingMark1;
carNumber = cNum;
setKind(k);
loaded = l;
destination = dest;
}
/**********************
*      output car    *
**********************/
void Car::output()
{
cout << "reportingMark: " << reportingMark << endl;
//cout<<" yoman "<<endl;
cout << "carNumber:     " << carNumber << endl;
cout << "kind:          " << KIND_ARRAY[kind] << endl;
if (loaded == true)
  cout << "loaded:        " << "true" << endl;
else
  cout << "loaded:        " << "false" << endl;
cout << "destination:   " << destination << endl;
}
/*************************
*      operator Car     *
*************************/

Car & Car::operator=(const Car & carB)
{
setUp(carB.reportingMark, carB.carNumber, KIND_ARRAY[carB.kind], carB.loaded, carB.destination);
return *this;
}
/*********************
*      setKind      *
*********************/

void Car::setKind(const string &k)
{
if (k != "business" && k != "maintenance")
  kind = other;
else if (k == "business")
  kind = business;
else if (k == "maintenance")
  kind = maintenance;
}
/*********************
*      operator     *
*********************/

bool operator==(const Car &car1, const Car &car2)
{
bool x;
if ((car1.reportingMark == car2.reportingMark) && (car1.carNumber == car2.carNumber))
  x = true;
else
  x = false;
return x;
}
/*************************
*      StringOfCars     *
*************************/


void StringOfCars::output()
{
Node * currentNodePtr=head;
if(head==NULL)
{
    cout << "NO cars";
}
else
{

    currentNodePtr=head;
    while(currentNodePtr!=NULL)
    {

      currentNodePtr->data->output();
      currentNodePtr=currentNodePtr->next;

    }
}
}
/****************************
*      push function       *
****************************/

void StringOfCars::push(Car &x)
{
Node *currentNodePtr=new Node;
Car *temp2(&x);
currentNodePtr->data=temp2;
currentNodePtr->next=NULL;
if(head==NULL)
{

    head=currentNodePtr;
    tail=currentNodePtr;

}
   else
   {
     tail->next=currentNodePtr;
   }
   tail=currentNodePtr;
   tail->next=NULL;
}
/*************************
*      pop function     *
*************************/
void StringOfCars::pop(Car &x)
{
if (size > 0)
{
  size--;
  x = (*ptr[size]);
  delete ptr[size];
  ptr[size] = nullptr;
}
}
/******************************
*      FreightCar setKind    *
******************************/
void FreightCar::setKind(const string &k)
{
if (k != "box" && k != "tank" && k != "flat")
  kind = otherFreight;
else if (k == "box")
  kind = box;
else if (k == "tank")
  kind = tank;
else if (k == "flat")
  kind = flat;
}
/*********************************
*      passengercar setkind     *
*********************************/
void PassengerCar::setKind(const string &k)
{
if (k != "chair" && k != "sleeper")
  kind = otherPassenger;
else if (k == "chair")
  kind = chair;
else if (k == "sleeper")
  kind = sleeper;
}
/*********************
*      input        *
*********************/

void input(StringOfCars &obj)
{
ifstream inputFile;
inputFile.open("Cars.txt");
if (!inputFile)
{
  cerr << "Error: File did not open. Program will terminate";
  exit(1);
}
string reportingMark; int carNumber; string kind; bool loaded; string destination;
string type;
string order;
int counter = 0;
while (inputFile.peek() != EOF)
{
  string trueFalse;
  inputFile >> type;
  inputFile >> order;
  inputFile >> reportingMark;
  inputFile >> carNumber;
  inputFile >> kind;
  inputFile >> trueFalse;
  if (trueFalse == "true")
   loaded = true;
  else
   loaded = false;
  while (inputFile.peek() == ' ')
   inputFile.get();
  getline(inputFile, destination);
  counter++;
  if (type == "Car")
   buildCar(obj, reportingMark, carNumber, kind, loaded, destination);
  else if (type == "FreightCar")
   buildFreightCar(obj, reportingMark, carNumber, kind, loaded, destination);
  else if (type == "PassengerCar")
   buildPassengerCar(obj, reportingMark, carNumber, kind, loaded, destination);
}
}
/*********************
*      buildCar    *
*********************/
void buildCar(StringOfCars &obj, string rMark, int carNumber, string kind, bool loaded, string destination)
{
Car *x = new Car(rMark, carNumber, kind, loaded, destination);
obj.push(*x);
}
/****************************
*      buildFreightCar     *
****************************/

void buildFreightCar(StringOfCars &obj, string rMark, int carNumber, string kind, bool loaded, string destination)
{
Car *x = new FreightCar(rMark, carNumber, kind, loaded, destination);
obj.push(*x);
}
/****************************
*     buildPassengerCar    *
****************************/

void buildPassengerCar(StringOfCars &obj, string rMark, int carNumber, string kind, bool loaded, string destination)
{
Car *x = new PassengerCar(rMark, carNumber, kind, loaded, destination);
obj.push(*x);
}

/* Execution Results :

reportingMark: CN
carNumber:     819481
kind:          maintenance
loaded:        false
destination:   NONE

reportingMark: SLSF
carNumber:     46871
kind:          business
loaded:        true
destination:   Memphis

reportingMark: AOK
carNumber:     156
kind:          other
loaded:        true
destination:   McAlester

reportingMark: MKT
carNumber:     123456
kind:          tank
loaded:        false
destination:   Fort Worth

reportingMark: MP
carNumber:     98765
kind:          box
loaded:        true
destination:   Saint Louis

reportingMark: SP
carNumber:     567890
kind:          flat
loaded:        true
destination:   Chicago

reportingMark: GMO
carNumber:     7878
kind:          otherFreight
loaded:        true
destination:   Mobile

reportingMark: KCS
carNumber:     7893
kind:          chair
loaded:        true
destination:   Kansas City

reportingMark: PAPX
carNumber:     145
kind:          sleeper
loaded:        true
destination:   Tucson

reportingMark: GN
carNumber:     744
kind:          otherPassenger
loaded:        false
destination:   NONE

*/

Copy the solution from problem F1 and make the name OEC2.
Change the Node class to be a template class.
In the Node class change the Car * pointer to a T * pointer where T is the template generated type.

Everywhere that Node is used in the StringOfCars data and functions, change it to Node<Car>

Use the same tests as in problem F1.

Explanation / Answer

#include <iostream>
#include <string>
#include <iomanip>
#include <fstream>
#include <cstdlib>
#include<bits/stdc++.h>
#define nullptr NULL
using namespace std;
enum Kind { business, maintenance, other, box, tank, flat, otherFreight, chair, sleeper, otherPassenger };
const string KIND_ARRAY[10] = { "business", "maintenance", "other", "box", "tank", "flat", "otherFreight",
"chair", "sleeper", "otherPassenger" };
class Car
{
public:
string reportingMark;
int carNumber;
Kind kind;
bool loaded;
string destination;
public:
Car()
{
setUp("", 0, "other", false, "NONE");
}
Car(Car &obj)
{
setUp(obj.reportingMark, obj.carNumber, KIND_ARRAY[obj.kind], obj.loaded, obj.destination);
}
Car(const string &rmark, const int &cNum, const string &k, const bool &l, const string &dest1)
{
setUp(rmark, cNum, k, l, dest1);
}
virtual ~Car(){}
void setUp(string reportingMark1, int cNum, string k, bool l, string dest);
void output();
friend bool operator==(const Car &car1, const Car &car2);
Car & operator=(const Car & carB);
virtual void setKind(const string &x);
};
template <class T>class Node
{
Node* next;
T*data;
Node()
{
}
public:
friend class StringOfCars;
};
class StringOfCars
{
private:
Node<Car> *head;
Node<Car> *tail;
Car **ptr;
static const int ARRAY_MAX_SIZE = 10;
int size;
public:
StringOfCars()
{
head=NULL;
tail=NULL;
ptr = new Car*[ARRAY_MAX_SIZE];
size = 0;
}
StringOfCars(const StringOfCars &oldObj)
{
Node<Car> *currentNodeptr=new Node<Car>;
currentNodeptr->next=oldObj.head;
head=NULL;
tail=NULL;
if(oldObj.head!=NULL)
{
while(currentNodeptr!=NULL)
{
Car *temp=currentNodeptr->data;
push(*temp);
currentNodeptr=currentNodeptr->next;
}
}
}
~StringOfCars()
{
for (int x = 0; x < size;x++)
{
delete ptr[x];
ptr[x] = nullptr;
}
delete[]ptr;
ptr = nullptr;
}
void push(Car &x);
void pop(Car &x);
void output();
};
class FreightCar :public Car
{
public:
FreightCar(){
setUp("", 0, "other", false, "NONE");
}
FreightCar(const FreightCar &obj) {
setUp(obj.reportingMark, obj.carNumber, KIND_ARRAY[obj.kind], obj.loaded, obj.destination);
setKind(KIND_ARRAY[obj.kind]);
}
FreightCar(const string &rM, const int &cN, const string &kind, const bool &load, const string &dest) {
setUp(rM, cN, kind, load, dest);
}
void setKind(const string &x);
};
class PassengerCar :public Car
{
public:
PassengerCar(){
setUp("", 0, "other", false, "NONE");
}
PassengerCar(const PassengerCar &obj){
setUp(obj.reportingMark, obj.carNumber, KIND_ARRAY[obj.kind], obj.loaded, obj.destination);
}
PassengerCar(const string &rM, const int &cN, const string &kind, const bool &load, const string &dest){
setUp(rM, cN, kind, load, dest);
}
void setKind(const string &x);
};
void input(StringOfCars &obj);
bool operator==(const Car &car1, const Car &car2);
void buildCar(StringOfCars &obj,string rMark, int carNumber, string kind, bool loaded, string destination);
void buildFreightCar(StringOfCars &obj, string rMark, int carNumber, string kind, bool loaded, string destination);
void buildPassengerCar(StringOfCars &obj, string rMark, int carNumber, string kind, bool loaded, string destination);

int main()
{
StringOfCars stringOfCars1;
input(stringOfCars1);
stringOfCars1.output();
return 0;
}
/**********************
* setup car *
**********************/

void Car::setUp(string reportingMark1, int cNum, string k, bool l, string dest)
{
reportingMark = reportingMark1;
carNumber = cNum;
setKind(k);
loaded = l;
destination = dest;
}
/**********************
* output car *
**********************/
void Car::output()
{
cout << "reportingMark: " << reportingMark << endl;
//cout<<" yoman "<<endl;
cout << "carNumber: " << carNumber << endl;
cout << "kind: " << KIND_ARRAY[kind] << endl;
if (loaded == true)
cout << "loaded: " << "true" << endl;
else
cout << "loaded: " << "false" << endl;
cout << "destination: " << destination << endl;
}
/*************************
* operator Car *
*************************/
Car & Car::operator=(const Car & carB)
{
setUp(carB.reportingMark, carB.carNumber, KIND_ARRAY[carB.kind], carB.loaded, carB.destination);
return *this;
}
/*********************
* setKind *
*********************/
void Car::setKind(const string &k)
{
if (k != "business" && k != "maintenance")
kind = other;
else if (k == "business")
kind = business;
else if (k == "maintenance")
kind = maintenance;
}
/*********************
* operator *
*********************/
bool operator==(const Car &car1, const Car &car2)
{
bool x;
if ((car1.reportingMark == car2.reportingMark) && (car1.carNumber == car2.carNumber))
x = true;
else
x = false;
return x;
}
/*************************
* StringOfCars *
*************************/

void StringOfCars::output()
{
Node<Car> * currentNodePtr=head;
if(head==NULL)
{
cout << "NO cars";
}
else
{
currentNodePtr=head;
while(currentNodePtr!=NULL)
{
currentNodePtr->data->output();
currentNodePtr=currentNodePtr->next;
}
}
}
/****************************
* push function *
****************************/
void StringOfCars::push(Car &x)
{
Node<Car> *currentNodePtr=new Node<Car>;
Car *temp2(&x);
currentNodePtr->data=temp2;
currentNodePtr->next=NULL;
if(head==NULL)
{
head=currentNodePtr;
tail=currentNodePtr;
}
else
{
tail->next=currentNodePtr;
}
tail=currentNodePtr;
tail->next=NULL;
}
/*************************
* pop function *
*************************/
void StringOfCars::pop(Car &x)
{
if (size > 0)
{
size--;
x = (*ptr[size]);
delete ptr[size];
ptr[size] = nullptr;
}
}
/******************************
* FreightCar setKind *
******************************/
void FreightCar::setKind(const string &k)
{
if (k != "box" && k != "tank" && k != "flat")
kind = otherFreight;
else if (k == "box")
kind = box;
else if (k == "tank")
kind = tank;
else if (k == "flat")
kind = flat;
}
/*********************************
* passengercar setkind *
*********************************/
void PassengerCar::setKind(const string &k)
{
if (k != "chair" && k != "sleeper")
kind = otherPassenger;
else if (k == "chair")
kind = chair;
else if (k == "sleeper")
kind = sleeper;
}
/*********************
* input *
*********************/
void input(StringOfCars &obj)
{
ifstream inputFile;
inputFile.open("D:Cars.txt");
if (!inputFile)
{
cerr << "Error: File did not open. Program will terminate";
exit(1);
}
string reportingMark; int carNumber; string kind; bool loaded; string destination;
string type;
string order;
int counter = 0;
while (inputFile.peek() != EOF)
{
string trueFalse;
inputFile >> type;
inputFile >> order;
inputFile >> reportingMark;
inputFile >> carNumber;
inputFile >> kind;
inputFile >> trueFalse;
if (trueFalse == "true")
loaded = true;
else
loaded = false;
while (inputFile.peek() == ' ')
inputFile.get();
getline(inputFile, destination);
counter++;
if (type == "Car")
buildCar(obj, reportingMark, carNumber, kind, loaded, destination);
else if (type == "FreightCar")
buildFreightCar(obj, reportingMark, carNumber, kind, loaded, destination);
else if (type == "PassengerCar")
buildPassengerCar(obj, reportingMark, carNumber, kind, loaded, destination);
}
}
/*********************
* buildCar *
*********************/
void buildCar(StringOfCars &obj, string rMark, int carNumber, string kind, bool loaded, string destination)
{
Car *x = new Car(rMark, carNumber, kind, loaded, destination);
obj.push(*x);
}
/****************************
* buildFreightCar *
****************************/
void buildFreightCar(StringOfCars &obj, string rMark, int carNumber, string kind, bool loaded, string destination)
{
Car *x = new FreightCar(rMark, carNumber, kind, loaded, destination);
obj.push(*x);
}
/****************************
* buildPassengerCar *
****************************/
void buildPassengerCar(StringOfCars &obj, string rMark, int carNumber, string kind, bool loaded, string destination)
{
Car *x = new PassengerCar(rMark, carNumber, kind, loaded, destination);
obj.push(*x);
}

output:

reportingMark: CN
carNumber:     819481
kind:          maintenance
loaded:        false
destination:   NONE

reportingMark: SLSF
carNumber:     46871
kind:          business
loaded:        true
destination:   Memphis

reportingMark: AOK
carNumber:     156
kind:          other
loaded:        true
destination:   McAlester

reportingMark: MKT
carNumber:     123456
kind:          tank
loaded:        false
destination:   Fort Worth

reportingMark: MP
carNumber:     98765
kind:          box
loaded:        true
destination:   Saint Louis

reportingMark: SP
carNumber:     567890
kind:          flat
loaded:        true
destination:   Chicago

reportingMark: GMO
carNumber:     7878
kind:          otherFreight
loaded:        true
destination:   Mobile

reportingMark: KCS
carNumber:     7893
kind:          chair
loaded:        true
destination:   Kansas City

reportingMark: PAPX
carNumber:     145
kind:          sleeper
loaded:        true
destination:   Tucson

reportingMark: GN
carNumber:     744
kind:          otherPassenger
loaded:        false
destination:   NONE

Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
Chat Now And Get Quote