1.先来看一下题目要求
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.
所以我们首先声明Car类car.h
#include
#include
/*
*对答案注释
*作者:白强
*日期:2013/4/17
*
*/
using namespace std;
/*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.(来自doc要求)
要求统计每一辆车的移动次数顾定义moved变量
车的牌号定义变量license_plate
相应的 times_moved(void)返回moved
get_license_plate(void)返回license_plate
构造函数就不介绍了
*/
class Car {
private:
string license_plate;
int moved;
public:
Car(string);
int& times_moved(void);
string get_license_plate(void);
};再就是对Car的定义文件car.cpp
#include "car.h"
//对car.h实现见car.h说明
Car::Car(string plate) :license_plate(plate),
moved(0) {}
int& Car::times_moved(void) {
return moved;
}
string Car::get_license_plate(void) {
return license_plate;
}这个时候再只需一个main.cpp了
总体思想是根据第二个参数判断是出是入
入比较简单,直接压入即可,出的时候考虑一下是否是最顶的车
是的话直接出栈,不是的话先把它顶上的移到临时栈,使要的车的出栈,然后再把临时栈的车全部压回停车场栈即可
#include
#include
#include
#include
#include
#include
#include "car.h"
/*
* 作者:白强
* 日期:2013/4/17\
* All Right Reserved
*/
/*总体思想是根据第二个参数判断是出是入
*入比较简单,直接压入即可,出的时候考虑一下是否是最顶的车
*是的话直接出栈,不是的话先把它顶上的移到临时栈,去除要的车,然后再把临时栈的车全部压回停车场栈即可
*总的来说就是出的时候比较有些绕而已
*/
using namespace std;
//定义停车场最大的停车数
const int NUMBER_OF_PARKING_SPOTS = 5;
//声明两个函数车来时如何处理,车走时如何处理
/