博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
SSD5_Recommended Exercise 4 分析
阅读量:5140 次
发布时间:2019-06-13

本文共 6401 字,大约阅读时间需要 21 分钟。

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<Car*>
  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;//声明两个函数车来时如何处理,车走时如何处理//注意stack的模板使用方法stack
&,掌握STL中的Stack使用void handle_arrival(stack
&, const string&);void handle_departure(stack
&, 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
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
& 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
& parking_lot, const string& plate) { stack
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

转载于:https://www.cnblogs.com/bq12345/archive/2013/04/17/3027384.html

你可能感兴趣的文章
查询数据库锁
查看>>
我对于脚本程序的理解——百度轻应用有感
查看>>
面试时被问到的问题
查看>>
spring 事务管理
查看>>
VS2008 去掉msvcr90的依赖
查看>>
当前记录已被另一个用户锁定
查看>>
Node.js 连接 MySQL
查看>>
那些年,那些书
查看>>
注解小结
查看>>
java代码编译与C/C++代码编译的区别
查看>>
Bitmap 算法
查看>>
转载 C#文件中GetCommandLineArgs()
查看>>
list control控件的一些操作
查看>>
LVM快照(snapshot)备份
查看>>
绝望的第四周作业
查看>>
一月流水账
查看>>
npm 常用指令
查看>>
判断字符串在字符串中
查看>>
Linux环境下Redis安装和常见问题的解决
查看>>
HashPump用法
查看>>