3.Parking Lot Simulation

Prerequisites, Goals, and Outcomes

Prerequisites: Students should have mastered the following prerequisite skills.

· Pointers - Using pointers to indirectly reference and modify objects

· File I/O - Opening and reading a text data file

· Dynamic Memory Management - Use of new and delete

· Stacks - Understanding of stack operations and use of STL stack adapter

· Class Specification - Design and implementation of a simple class

Goals: This assignment is designed to improve the student's knowledge of stacks and their use of the STL stack adapter.

Outcomes: Students successfully completing this assignment would master the following outcomes.

· Understand use of the STL stack adapter

· Create an entire C++ program

· Use file I/O

· Use dynamic memory management

· Use pointers

Background

Parking lot attendants often park cars bumper-to-bumper, several cars deep. This maximizes the number of cars they can fit into a parking lot at the expense of complicating the process of retrieving someone's car when they want to leave. Consider the case of a person wanting to leave the parking lot but their car is parked in the back of a row of cars. In this case, all the cars parked in front of this person's car must be temporarily moved to allow this person to leave.

Description

This assessment tests your ability to use the STL stack adapter to solve a problem. You are asked to create a program that simulates a single-aisle parking lot. When cars are parked bumper-to-bumper, this parking-lot aisle can hold five cars.

It is your task to create a simulation that processes the vehicle arrivals and departures. The goal of the simulation is to keep track of and report how many times individual cars are moved while handling the departure of other cars.

Files

Following is a list of files needed to complete this assessment.

· handout-files.zip contains all of the following necessary files:

o data.txt - This file contains arrival and departure data.

o output.txt - This file contains output from a sample solution to this assessment (that used data.txt).

Tasks

To complete this assessment, you will need to design and implement class Car and implement the parking-lot simulator program.

To begin, verify the files needed for this assessment.

1. Extract the archive to retrieve the files needed to complete this assessment.

Following is an ordered list of steps that serves as a guide to completing this assessment. Work and test incrementally. Save often

1. First, declare and implement class Car. Instances of class Car need to store the license plate of the car and the number of times the car has been moved while it has been parked in the lot.

2. Next, begin the implementation of the parking-lot simulator program. Create a filed named main.cpp. Among other libraries, you will need to add the necessary include directives to access the C++ input/output library, the file input/output library, the STL stack adapter, and your class Car. Create function main.

3. Then, write the code that opens the simulation data file. The user of the simulation should specify the data file to use via the command-line. Take appropriate actions if a command-line is not present or if the specified file cannot be opened.

4. Next, declare a stack object to represent the single-aisle parking lot. This object must be of type stack

5. Then, read the contents of the data file. Each line in the data file is in the form: license-plate action, where action is either "arrives" or "departs". For each arrival, instantiate an object of type Car in the free store. Simulate parking the car by pushing a pointer to the object into the parking-lot stack. Output a meaningful message if the parking lot is full. The lot is full when the stack contains five elements. For each departure, remove the corresponding Car pointer from the stack, and output the number of times this car was moved while it was parked in the lot. To do this and preserve the order of the other cars, you may need to use a second, temporary stack of type pointer to Car. Be sure to keep track of the number of times a car is moved while accommodating the departure of another car. Do not leak memory.

6. Finally, after processing the contents of the data file, output the number of times each car that remains in the lot (if there are any) was moved.

#include
#include
#include
#include
using namespace std;
const int N=5;
class Car
{
private:
	string licence;
	int moveTimes;
public:
	Car(string lic)
	{
		this->licence=lic;
		this->moveTimes=0;
	}
	~Car();
	string getLicence()
	{
		return licence;
	}
	int getMoveTimes()
	{
		return moveTimes;
	}
	void moveCar()
	{
		moveTimes++;
	}
};
int main()
{
	string snum;
	string sdata;
	stack stack1;//存储已停车辆指针
	stack stack2;//暂存车辆指针
	int count = 0;//计录停车场车数
	char * filein ="E:\\data.txt";
	char * fileout ="E:\\output.txt";

	ifstream infile(filein,ios::in);
	ofstream outfile(fileout,ios::out);

	if( !infile )
	{
		cout << "Cannot open the file.";
		exit(0);
	}
	if( !outfile )
	{
		cout << "Cannot open the file.";
		exit(0);
	}

	while( infile >> snum >> sdata )
	{
		//车辆停入
		//若超过规定停车量,则不能停入,继续下一个循环
		//否则,new一个Car*的指针,压入栈中
		if( sdata == "arrives" )
		{
			count ++;
			if( count>N )
			{
				outfile<<"Sorry PORSCHE, the lot is full"<getLicence() != snum )
			{
				stack1.top()->moveCar();
				stack2.push(stack1.top());
				stack1.pop();
			}		
            if( stack1.top()->getLicence() == snum )
			{
				outfile<< stack1.top()->getLicence() << " was moved " 
				       << stack1.top()->getMoveTimes() << " times while it was here" <getLicence() << " was moved " 
			   << stack1.top()->getMoveTimes() << " times while it was here" <


你可能感兴趣的:(数据结构,c++)