//"car.h"
#ifndef _CAR_H
#define _CAR_H
#include <iostream>
#include <fstream>
#include <string>
using namespace std;
class car;
istream &operator>>(istream &stream, car &e);
class car {
private:
string license;
string action;
int number;
public:
car ();
car (string license, string action,int number);
string getlicense() ;
string getaction() ;
int getnumber();
void plusnumber();
friend istream &operator>>(istream &stream, car &c);
};
#endif
//"car.cpp"
#include<iostream>
#include"car.h"
using namespace std;
car::car(string license,string action,int number){
this->license = license;
this->action = action;
this->number =number;
}
//the default construct
car::car(){
number = 0;
}
string car::getlicense (){
return license;
}
string car::getaction (){
return action;
}
int car::getnumber (){
return number;
}
void car::plusnumber (){
number ++;
}
//the function to count the times of the car was moved
istream &operator>>(istream &stream, car &c) {
stream >> c.license;
stream >> c.action;
return stream;
}
//overload the operator>>
//"main.cpp"
#include <fstream>
#include <stack>
#include <sstream>
#include"car.h"
int main ()
{
stack <car*>file;
stack <car*> s;
//initialize two stack
ifstream in;
in.open("data.txt");
if (!in) {
cerr << "Couldn't open data file, data file empty" << endl;
return 0;
}
// error message;
string line;
car a[100];
int index=-1;
while(getline(in,line)){
index++;
stringstream strcin(line);
strcin>>a[index];
//read the message of the cars
if(a[index].getaction() == "arrives" &&file.size () < 5){
file.push(&a[index]);
}
//new car arrives
else if (file.size () == 5&&a[index].getaction ()=="arrives"){
cout <<"Sorry " <<a[index].getlicense () <<", the lot is full" <<endl;
}
//full message
else if (a[index].getaction() =="departs"){
int m=0;
while (!file.empty() && file.top()->getlicense()!=a[index].getlicense()){
file.top()->plusnumber();
s.push(file.top());
file.pop();
m++;
}
if(m!= 0){
cout <<file.top()->getlicense() <<" was moved " <<file.top()->getnumber() <<" times while it is here" <<endl;
file.pop();
while(!s.empty ()){
file.push(s.top());
s.pop();
}
} else{
cout <<file.top()->getlicense() <<" was moved " <<file.top()->getnumber() <<" times while it is here" <<endl;
file.pop();
}
//car departs
}
}
while(file.size()!= 0){
cout <<(file.top())->getlicense() <<" was moved " <<(file.top())->getnumber() <<" times while it is here" <<endl;
file.pop();
}
//remaining cars in the stack
in.close ();
return 0;
}