⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 main.cpp

📁 ssd5 exam2需要的自己下载ssd5 exam2需要的自己下载
💻 CPP
字号:
#include <iostream>
#include <fstream>
#include <cstdlib>
#include <string>
#include <stack>
#include <stdexcept>
#include <vector>
#include <algorithm>

#include "car.h"

using namespace std;

const unsigned int PARKING_SPOTS_PER_AISLE = 3;
const unsigned 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";

		sort (cars.begin(), cars.end());
		for (vector<Car>::iterator it = cars.begin(); it != cars.end(); it++)
		{
			cout << it->getPlate() << endl;
		}
		
        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 Aisle_num = 0; Aisle_num < NUMBER_OF_AISLES; Aisle_num++)
	{
		if (parking_lot[Aisle_num].size() < PARKING_SPOTS_PER_AISLE)
		{
			parking_lot[Aisle_num].push (plate);
			Car temp_car (plate,Aisle_num);
			cars.push_back (temp_car);
			cout << temp_car.getPlate() << " arrived!" << endl;
			break;
		}
		
		if (Aisle_num == NUMBER_OF_AISLES-1)
			cout << plate << ": Sorry! The parking lot is full!"<<endl;
	}
}

void handle_departure (vector<Car>& cars, vector< stack<string> >& parking_lot, const string& plate) 
{
	Car Car_depart = find_car (cars,plate);
	int car_Aisle = Car_depart.getAisle(); 
	stack<string>* Aisle_temp = new stack<string>(); 
	
    for (int i = 0; i < PARKING_SPOTS_PER_AISLE; i++)
	{
		if (parking_lot[car_Aisle].top() != Car_depart.getPlate())
		{
			Car& temp_car = find_car (cars, parking_lot[car_Aisle].top()); 
			
			temp_car.setTimesMoved (temp_car.getTimesMoved() + 1);  
			Aisle_temp->push (parking_lot[car_Aisle].top());
			
			parking_lot[car_Aisle].pop();
		}
		else
			break;
	}
	
	cout << Car_depart.getPlate() << " was moved " << Car_depart.getTimesMoved()
		<< " times when it was parked in the lot" << endl;
	
	parking_lot[car_Aisle].pop();
	
	while (!Aisle_temp->empty())
	{
		parking_lot[car_Aisle].push (Aisle_temp->top());
		Aisle_temp->pop();
	}
	
	delete Aisle_temp;
}

Car& find_car (vector<Car>& cars, string plate) 
{
	vector<Car>::iterator it = find (cars.begin (),cars.end (),Car(plate));
	return *it;
}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -