Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #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("data.txt");
- 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
- 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) {
- for(int i = 0; i < parking_lot.size();i++)
- {
- if(parking_lot[i].size()<PARKING_SPOTS_PER_AISLE)
- {
- stack<string>& lot = parking_lot[i];
- lot.push(plate);
- cars.push_back(Car(plate,i));
- return;
- }
- }
- cout<<"Sorry"<< plate <<", the lot is full" <<endl;
- }
- void handle_departure(vector<Car>& cars, vector< stack<string> >& parking_lot, const string& plate) {
- Car& car_to_depart= find_car(cars,plate);
- stack<Car> sub;
- int number_of_lot = car_to_depart.getAisle();
- stack<string>& lot = parking_lot[number_of_lot];
- do{
- if(Car(lot.top()) == car_to_depart)
- {
- lot.pop();
- cout<< plate<< "was moved "<< (find_car(cars,lot.top())).getTimesMoved() << " times while it was here" << endl;
- while(sub.size() != 0)
- {
- Car& moved_car = sub.top();
- moved_car.setTimesMoved(moved_car.getTimesMoved()+1);
- sub.pop();
- lot.push(moved_car.getPlate());
- }
- break;
- }
- else
- {
- Car& prev_car = find_car(cars,lot.top());
- sub.push(prev_car);
- lot.pop();
- }
- }while(true);
- }
- Car& find_car(vector<Car>& cars, string plate) {
- return (*std::find(cars.begin(),cars.end(),Car(plate)));
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement