⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 main.cpp

📁 SSD5 Exam2 代码
💻 CPP
字号:
#pragma warning(disable:4786)

#include <iostream>
#include <fstream>
#include <cstdlib>
#include <string>
#include <stack>
#include <stdexcept>
#include <vector>
#include <algorithm>

#include "car.h"

using namespace std;

const int PARKING_SPOTS_PER_AISLE = 3;
const int NUMBER_OF_AISLES = 5;

void handle_arrival(vector<Car>&, vector<stack<string> >&, const string&);
void handle_departure(vector<Car>&, vector<stack<string> >&, const string&);
Car& find_car(vector<Car>&, string);

int main(int argc, char* argv[]) {

    try {

        if (argc != 2) {
            cerr << "Usage:\n" << argv[0] << " data-file";
            return EXIT_FAILURE;
        }    

        ifstream inf(argv[1]);
        if (! inf) {
            cerr << "Could not open " << argv[1];
            return EXIT_FAILURE;
        }

		vector<Car> cars;
		vector< stack<string> > parking_lot(NUMBER_OF_AISLES);

        while (! inf.eof()) {
        
            string action, plate;
            inf >> plate >> action;
        
            if (action == "arrives") {
                handle_arrival(cars, parking_lot, plate);
            } 
            else if (action == "departs") {
                handle_departure(cars, parking_lot, plate);            
            } else {
                cerr << "Unknown action: " << action << endl;
            }
        
        }
        inf.close();    

        cout << "\nHere are all the cars that visited the lot today:\n";

        sort(cars.begin(), cars.end());    //using the template method sort();
  /*
     using iterators,traverse the sorted vector 
	 and display the license plates of the cars. 
  */
		vector<Car>::iterator itr = cars.begin();

		while(itr != cars.end()){

			cout<<itr->getPlate()<<endl;
		    itr++;
		}

        return EXIT_SUCCESS;
        
    }
    catch (exception& e) {
        cerr << e.what() << endl;
    }
    catch (...) {
        cerr << "Unknown exception caught!" << endl;
    }
    
    return EXIT_FAILURE;
}


void handle_arrival(vector<Car>& cars, vector< stack<string> >& parking_lot, const string& plate) {

    /*
	 iterate through the vector of stacks, looking for the first stack that
	 does not contain three cars
	 */

	for(int i = 0; i < NUMBER_OF_AISLES; i++){

		if(parking_lot[i].size() < PARKING_SPOTS_PER_AISLE) {
			
		// place the license plate into the first non-full stack

			parking_lot[i].push(plate);  
			cars.push_back(Car(plate,i));
			break;

		}
	}

	//If all five aisles (stacks) are full, output a message indicating such; 

	if(i == NUMBER_OF_AISLES)
		cout<<"The parking_lot is full."<<endl;

}

void handle_departure(vector<Car>& cars, vector< stack<string> >& parking_lot, const string& plate) {

	
    Car departingCar = find_car(cars,plate);        //find the departing vehicle
	int carAiles = departingCar.getAisle();         //locate the departing car
	stack<string>* tempAiles = new stack<string>(); //create a temporary stack

   //move any car that in the front of the departing car into the temporary stack
	while(parking_lot[carAiles].top() != departingCar.getPlate()){
  
        Car& temp = find_car(cars, parking_lot[carAiles].top()); 

		temp.setTimesMoved(temp.getTimesMoved() + 1);  
		tempAiles->push(parking_lot[carAiles].top());
	
        parking_lot[carAiles].pop();
	}

  // display the times the departing car moved

	cout<<departingCar.getPlate()<<" was moved "<<departingCar.getTimesMoved()
		<<" times when it was parked in the lot"<<endl;

  	parking_lot[carAiles].pop();                //remove the departing car's license plate.

	while(!tempAiles->empty()){

		parking_lot[carAiles].push(tempAiles->top());
		tempAiles->pop();

	}

	delete tempAiles;
}

// find the car in the vector using the template method find()

Car& find_car(vector<Car>& cars, string plate) {
	  
      return *find(cars.begin(), cars.end(), Car(plate));

}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -