SSD5_Exercise 4

//"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;
   
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值