Advertisement
Guest User

Untitled

a guest
Mar 30th, 2015
230
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 3.08 KB | None | 0 0
  1. #pragma warning(disable:4786)
  2.  
  3. #include <iostream>
  4. #include <fstream>
  5. #include <cstdlib>
  6. #include <string>
  7. #include <stack>
  8. #include <stdexcept>
  9. #include <vector>
  10. #include <algorithm>
  11.  
  12. #include "car.h"
  13.  
  14. using namespace std;
  15.  
  16. const int PARKING_SPOTS_PER_AISLE = 3;
  17. const int NUMBER_OF_AISLES = 5;
  18.  
  19. void handle_arrival(vector<Car>&, vector<stack<string> >&, const string&);
  20. void handle_departure(vector<Car>&, vector<stack<string> >&, const string&);
  21. Car& find_car(vector<Car>&, string);
  22.  
  23. int main(int argc, char* argv[]) {
  24.  
  25.     try {
  26.         /*
  27.         if (argc != 2) {
  28.             cerr << "Usage:\n" << argv[0] << " data-file";
  29.             return EXIT_FAILURE;
  30.         }*/    
  31.  
  32.         ifstream inf("data.txt");
  33.         if (! inf) {
  34.             cerr << "Could not open " << argv[1];
  35.             return EXIT_FAILURE;
  36.         }
  37.  
  38.         vector<Car> cars;
  39.         vector< stack<string> > parking_lot(NUMBER_OF_AISLES);
  40.  
  41.         while (! inf.eof()) {
  42.        
  43.             string action, plate;
  44.             inf >> plate >> action;
  45.        
  46.             if (action == "arrives") {
  47.                 handle_arrival(cars, parking_lot, plate);
  48.             }
  49.             else if (action == "departs") {
  50.                 handle_departure(cars, parking_lot, plate);            
  51.             } else {
  52.                 cerr << "Unknown action: " << action << endl;
  53.             }
  54.        
  55.         }
  56.         inf.close();    
  57.  
  58.         cout << "\nHere are all the cars that visited the lot today:\n";
  59.  
  60.         // TODO: Output the license plates of all the
  61.         // cars that visited the lot, in alphabetical order    
  62.  
  63.  
  64.         return EXIT_SUCCESS;
  65.        
  66.     }
  67.     catch (exception& e) {
  68.         cerr << e.what() << endl;
  69.     }
  70.     catch (...) {
  71.         cerr << "Unknown exception caught!" << endl;
  72.     }
  73.    
  74.     return EXIT_FAILURE;
  75. }
  76.  
  77. void handle_arrival(vector<Car>& cars, vector< stack<string> >& parking_lot, const string& plate) {
  78.  
  79.     for(int i = 0; i < parking_lot.size();i++)
  80.     {
  81.         if(parking_lot[i].size()<PARKING_SPOTS_PER_AISLE)
  82.         {
  83.             stack<string>& lot = parking_lot[i];
  84.             lot.push(plate);
  85.             cars.push_back(Car(plate,i));
  86.             return;
  87.         }
  88.  
  89.     }
  90.     cout<<"Sorry"<< plate <<", the lot is full" <<endl;
  91. }
  92.  
  93. void handle_departure(vector<Car>& cars, vector< stack<string> >& parking_lot, const string& plate) {
  94.  
  95.     Car& car_to_depart= find_car(cars,plate);
  96.     stack<Car> sub;
  97.     int number_of_lot = car_to_depart.getAisle();
  98.     stack<string>& lot = parking_lot[number_of_lot];
  99.     do{
  100.         if(Car(lot.top()) == car_to_depart)
  101.         {
  102.             lot.pop();
  103.             cout<< plate<< "was moved "<< (find_car(cars,lot.top())).getTimesMoved() << " times while it was here" << endl;
  104.             while(sub.size() != 0)
  105.             {
  106.                 Car& moved_car = sub.top();
  107.                 moved_car.setTimesMoved(moved_car.getTimesMoved()+1);
  108.                 sub.pop();
  109.                 lot.push(moved_car.getPlate());
  110.             }
  111.             break;
  112.         }
  113.         else
  114.         {
  115.             Car& prev_car = find_car(cars,lot.top());
  116.             sub.push(prev_car);
  117.             lot.pop();
  118.         }
  119.     }while(true);
  120. }
  121.  
  122. Car& find_car(vector<Car>& cars, string plate) {
  123.     return (*std::find(cars.begin(),cars.end(),Car(plate)));
  124. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement