SSD5_Recommended Exercise 4 分析
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.
- 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
- 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.
- 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.
- 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.
- Next, declare a stack object to represent the single-aisle parking lot. This object must be of type stack<Car*>
- 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.
- 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 <iostream> #include <string> /* *对答案注释 *作者:白强 *日期: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 <iostream> #include <fstream> #include <cstdlib> #include <string> #include <stack> #include <stdexcept> #include "car.h" /* * 作者:白强 * 日期:2013/4/17\ * All Right Reserved */ /*总体思想是根据第二个参数判断是出是入 *入比较简单,直接压入即可,出的时候考虑一下是否是最顶的车 *是的话直接出栈,不是的话先把它顶上的移到临时栈,去除要的车,然后再把临时栈的车全部压回停车场栈即可 *总的来说就是出的时候比较有些绕而已 */ using namespace std; //定义停车场最大的停车数 const int NUMBER_OF_PARKING_SPOTS = 5; //声明两个函数车来时如何处理,车走时如何处理 //注意stack的模板使用方法stack<Car*>&,掌握STL中的Stack使用 void handle_arrival(stack<Car*>&, const string&); void handle_departure(stack<Car*>&, const string&); int main(int argc, char* argv[]) { //如果没有data.txt参数,程序退出 if (argc != 2) { cerr << "Usage:\n" << argv[0] << " data-file"; return EXIT_FAILURE; } //data.txt不能打开。程序异常退出 ifstream inf(argv[1]); if (! inf) { cerr << "Could not open " << argv[1]; return EXIT_FAILURE; } //读入data.txt try { //关键的定义栈,停车场栈 stack<Car*> parking_lot; //直到文件末尾 while (! inf.eof()) { //注意是按词读取 string action, plate; inf >> plate >> action; //根据第二个词采用不同函数处理 if (action == "arrives") { handle_arrival(parking_lot, plate); } else if (action == "departs") { handle_departure(parking_lot, plate); } else { cerr << "Unknown action: " << action << endl; } } inf.close(); // Handle any cars that remain in the lot. while (! parking_lot.empty()) {//当栈不为空 //得到栈顶的车 string plate = parking_lot.top()->get_license_plate(); //处理剩下的车
handle_departure(parking_lot, plate); } return EXIT_SUCCESS; } catch (exception& e) { cerr << e.what() << endl; if (inf.is_open()) inf.close(); } catch (...) { cerr << "Unknown exception caught!" << endl; if (inf.is_open()) inf.close(); } return EXIT_FAILURE; } void handle_arrival(stack<Car*>& parking_lot, const string& plate) { //判断是否栈满 if ((int) parking_lot.size() == NUMBER_OF_PARKING_SPOTS) { //满则输出警告 cout << "Sorry " << plate << ", the lot is full\n"; } else { //不满则创建Car对象再用Stack的push方法进栈 Car* arriving_car = new Car(plate); parking_lot.push(arriving_car); } } void handle_departure(stack<Car*>& parking_lot, const string& plate) { stack<Car*> the_street; //当栈顶的型号和要退出的不一致时,让顶部的先出去再压入另外的那个临时栈 while (parking_lot.top()->get_license_plate() != plate) { //车移动次数加一,直到和参数的plate相同,然后把栈顶的那个车拿出来 parking_lot.top()->times_moved()++; //把停车场栈顶的车压入临时栈 the_street.push(parking_lot.top()); //停车场栈顶出栈 parking_lot.pop(); } //栈顶的车并输入它的型号和移动次数 Car* departing_car = parking_lot.top(); cout << departing_car->get_license_plate() << " was moved " << departing_car->times_moved() << " times while it was here\n"; //栈顶的车出栈 parking_lot.pop(); //把已经出栈的车给删除了 delete departing_car; //然后再把临时栈的车全部按那个顺序再放回停车场栈即可 while (! the_street.empty()) { //临时栈顶放入停车场栈 parking_lot.push(the_street.top()); //临时栈顶出车 the_street.pop(); } }
这样这个题就算完成了,相信你也对STL的栈有了些认识,所以继续努力吧
最后附上data.txt的内容
//data.txt
COOLONE arrives
COOLONE departs
TKG-123 arrives
QWE-839 arrives
UTU-K90 arrives
QWE-839 departs
RRR-877 arrives
GHL-GII arrives
PROGRAM arrives
TKG-123 departs
HEAD-DR arrives
UTU-K90 departs
RRR-877 departs
DMS-RJS arrives
DMS-RJS departs
TUE-87B arrives
GHL-GII departs
WEW-GH1 arrives
THE-MAN arrives
PORSCHE arrives
HEAD-DR departs
ERU-883 arrives
TUE-87B departs
WEW-GH1 departs
APPLE-1 arrives
BKE-284 arrives
BKE-284 departs
APPLE-1 departs
THE-MAN departs