worm吧 关注:11贴子:282
  • 2回复贴,共1

不要看~~

收藏回复

  • 210.30.99.*
#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);
bool compare(Car c_1, Car c_2); //declare the function
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;
             }



1楼2009-12-26 10:18回复
    • 210.30.99.*

             }
             inf.close();    
             cout << "\nHere are all the cars that visited the lot today:\n";
             //Range an alphabetized list of all the cars
             sort(cars.begin(),cars.end(), compare);
             //display the ranged list
             for(vector<Car>::iterator iter = cars.begin(); iter != cars.end();iter ++){
                 cout << (*iter).getPlate() << " in the " << (*iter).getAisle() <<\
                     " aisle moved " << (*iter).getTimesMoved() << " times " << endl;
             }
             // 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;
    }
    //compare two car'plate
    bool compare(Car car_1, Car car_2){
         return car_1 < car_2;
    }
    void handle_arrival(vector<Car>& cars, vector< stack<string> >& parking_lot, const string& plate) {
         // TODO: Handle car departures
         int num = 0;
         //storage the car when the Parking Lot is not full. Judge information when it is full
         for(; num < parking_lot.size(); num++) {
             if((parking_lot[num]).size() < PARKING_SPOTS_PER_AISLE) {
                 (parking_lot[num]).push(plate);
                 cars.push_back(Car(plate,num));
                 return;
             }
         }
         cout << "All five aisles are full!" << endl;
    }
    void handle_departure(vector<Car>& cars, vector< stack<string> >& parking_lot,
    


    2楼2009-12-26 10:18
    回复
      • 210.30.99.*
                             const string& plate)
      {
           //define a depart car and another stack to storage car of top of parking_lot
           Car car_find = find_car(cars,plate);
           stack<string> cars_move;
           while(true)
           {
               //transfer the top car in the parking_lot
               Car &car_top = find_car(cars,parking_lot[car_find.getAisle()].top());
               if(car_top == car_find)
               { //find the top car is the departed car ,display and delete it.
                   cout << plate << " in the " << car_top.getAisle() << " aisle moved " << \
                       car_top.getTimesMoved() << " times " << endl;
                   parking_lot[car_top.getAisle()].pop();
                   while(!cars_move.empty())
                   {     //move the non-depart car back to former vector
                       parking_lot[car_find.getAisle()].push(cars_move.top());
                       cars_move.pop();
                   }
                   break;
               }
               else
               {    
                   //find the top car isn't the departed car, move it to another stack,delete it.
                   car_top.setTimesMoved(car_top.getTimesMoved() + 1);
                   cars_move.push(parking_lot[car_find.getAisle()].top());
                   parking_lot[car_find.getAisle()].pop();
               }
           }
      }
      Car& find_car(vector<Car>& cars, string plate) {
           //search the car with name plate in the cars vector
           vector<Car>::iterator iter = std::find(cars.begin (), cars.end (), Car(plate));
           if(iter == cars.end ()){//no found
               cerr<<"There is not the car."<<endl;
               exit(0);
           }else{//found
               return *iter;    
           }
      }
      


      3楼2009-12-26 10:18
      回复