📄 main.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";
// TODO: Output the license plates of all the
// cars that visited the lot, in alphabetical order
std::sort(cars.begin(), cars.end()); //use STL algorithm sort() to sort the vector of cars
for (vector<Car>::iterator itr_car = cars.begin(); itr_car != cars.end(); itr_car++) { //displays an alphabetized list of all the cars that visited the parking lot during the simulation
cout << (*itr_car).getPlate() << endl;
}
return EXIT_SUCCESS;
}
catch (exception& e) {
cerr << e.what() << endl;
}
catch (...) {
cerr << "Unknown exception caught!" << endl;
}
return EXIT_FAILURE;
}
/*
* This function should iterate through the vector of stacks, looking for
* the first stack that does not contain three cars. If all five aisles
* (stacks) are full, output a message indicating such; otherwise place
* the license plate into the first non-full stack.
*/
void handle_arrival(vector<Car>& cars, vector< stack<string> >& parking_lot, const string& plate) {
for (int i = 0; i < NUMBER_OF_AISLES; i++) { //cycle to find the first empty place
if (parking_lot[i].size() < PARKING_SPOTS_PER_AISLE) {//if it is not full
parking_lot[i].push(plate); //push the lience of the car into this aisle
cars.push_back(Car(plate, i)); //add an entry of type Car to the vector cars
return;
}
if (i == NUMBER_OF_AISLES - 1) { //if all the aisles are full
cout << "Sorry! parking_lot is full" << endl; //output such information
}
}
}
/*
* This function should locate the departing vehicle from the cars vector
* using function find_car. Then this function should remove the departing
* car's license plate from the appropriate aisle.
*/
void handle_departure(vector<Car>& cars, vector< stack<string> >& parking_lot, const string& plate) {
Car & car = find_car(cars, plate); //find the car which wants to leave
int i = car.getAisle(); //get the car's aisle
stack<string> temStack; //creat a temporary stack to hold cars in front of it
while (parking_lot[i].top() != plate) { //if the top lience is not the specific car's
string str_moved = parking_lot[i].top(); //get it's lience to push into temporary stack latter
Car & car_moved = find_car(cars, str_moved); //get the car to be moved
car_moved.setTimesMoved(car_moved.getTimesMoved() + 1);//increase this car's moved times
temStack.push(str_moved); //move the car into temporary stack
parking_lot[i].pop(); //delete it in the aisle
}
cout << plate << " was moved " << find_car(cars, plate).getTimesMoved() << " times while it was here" << endl;//display the car's moving information before it leaves
parking_lot[i].pop(); //delete it in the aisle
while (!temStack.empty()) { //if the temporary is not empty
parking_lot[i].push(temStack.top()); //move it back to aisle
temStack.pop(); //delete it in the temporary stack
}
}
/*
* This function returns a reference to the Car object stored in the vector
* cars whose license plate equals the parameter plate.
*/
Car& find_car(vector<Car>& cars, string plate) {
// TODO: Return a reference to the
// the Car object whose license plate equals
// the parameter 'plate'
vector<Car>::iterator itr = std::find(cars.begin(), cars.end(), Car(plate)); //get the specific car through invoking STL find()
return *itr; //return it
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -