📄 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
sort(cars.begin() , cars.end()) ;
vector<Car>::iterator it = cars.begin() ;
for(; it != cars.end() ; it++)
cout << it->getPlate() << endl ;
//delete memory where car stand on.
for(int i = 0 ; i < cars.size() ; i++){
Car& car = cars[i] ;
delete &car ;
}
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) {
// TODO: Handle car arrivals
for(int i = 0 ; i < parking_lot.size() ; i++){
if(parking_lot[i].size() < 3){
parking_lot[i].push(plate);
Car* c = new Car(plate , i) ;
cars.push_back(*c) ;
break ;
}
}
//if all ailses are full , indicate following message.
if(i >= parking_lot.size())
cout << "Sorry " << plate << ", the parking lot is full " << endl ;
}
void handle_departure(vector<Car>& cars, vector< stack<string> >& parking_lot, const string& plate) {
// TODO: Handle car departures
Car c = find_car(cars , plate) ;
stack<string> tempStack ;
int ailse = c.getAisle() ;
//push plate of car that is not equal parameter plate to temporarily stack.
//and add 1 to moved times.
while(parking_lot[ailse].top() != c.getPlate()){
string p = parking_lot[ailse].top() ;
tempStack.push(p) ;
parking_lot[ailse].pop() ;
Car& car = find_car(cars, p) ;
int times = car.getTimesMoved() ;
car.setTimesMoved(++times);
}
//remove the plate of car that departs from stack.
parking_lot[ailse].pop() ;
cout << c.getPlate() << " was moved " << c.getTimesMoved()
<< " times while it was here " << endl ;
//push plates of car that was moved to temporarily stack to former stack.
while(!tempStack.empty()){
string s = tempStack.top() ;
parking_lot[ailse].push(s) ;
tempStack.pop() ;
}
}
Car& find_car(vector<Car>& cars, string plate) {
// TODO: Return a reference to the
// the Car object whose license plate equals
// the parameter 'plate'
Car car(plate) ;
vector<Car>::iterator it ;
it = find(cars.begin() , cars.end() , car) ;
return *it ;
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -