3.Parking Lot Simulation
2013-07-02 20:15
162 查看
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<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.
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<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.
#include<stack> #include<string> #include<iostream> #include<fstream> 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<Car *> stack1;//存储已停车辆指针 stack<Car *> 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"<<endl; count--; continue; } Car *car = new Car(snum); stack1.push(car); } //车辆开出 //返回栈首元素,与文件读出车牌号比较 //若相同,则信息输入output2文件中,stack1弹出该车指针,再把stack2中的车辆压入stack1中 //若不同,则将车辆移动,弹出stack1,压入stack2 if( sdata == "departs" ) { while( stack1.top()->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" <<endl; stack1.pop(); count--; while( !stack2.empty() ) { stack1.push( stack2.top() ); stack2.pop(); } } } } //若停车场还有剩余车辆,则输入output2文件中 while( !stack1.empty() ) { outfile<< stack1.top()->getLicence() << " was moved " << stack1.top()->getMoveTimes() << " times while it was here" <<endl; stack1.pop(); } outfile.close(); infile.close(); cout<<"OK"<<endl; return 0; } /* 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 output.txt COOLONE was moved 0 times while it was here QWE-839 was moved 0 times while it was here TKG-123 was moved 0 times while it was here UTU-K90 was moved 2 times while it was here RRR-877 was moved 2 times while it was here DMS-RJS was moved 0 times while it was here GHL-GII was moved 3 times while it was here Sorry PORSCHE, the lot is full HEAD-DR was moved 3 times while it was here TUE-87B was moved 2 times while it was here WEW-GH1 was moved 2 times while it was here BKE-284 was moved 0 times while it was here APPLE-1 was moved 0 times while it was here THE-MAN was moved 3 times while it was here ERU-883 was moved 3 times while it was here PROGRAM was moved 4 times while it was here */
相关文章推荐
- CF 135 E.Parking Lot(线段树)
- Codeforces Round #135 (Div. 2) E. Parking Lot 线段数区间合并
- Codeforces#135(div.2) E.Parking lot【线段树】
- CF 480 E. Parking Lot
- 在Scrum中忽视了Parking Lot List
- Codeforces 480.E Parking Lot
- #OOD#Parking Lot停车场设计
- Amazon Interview Question: Design an OO parking lot
- Parking-lot
- 有趣的定律3. 木桶定律
- 解决窗口无响应的 WindowsFormsParkingWindow 问题。
- Microsoft .NET Pet Shop 3.x: .NET Pet Shop 的设计模式与体系结构
- About Human Motion Simulation
- 利用CVSNT在Eclipse中使用CVS--3.创建一个CVS实例
- Are You Living In a Computer Simulation?
- Researches on FAST site selection and 3D simulation
- Unix文件系统研究: 3. 链接
- PMP-PMBOK-培训(6)Project Management Essentials Simulation
- 浪潮之巅 第一章 帝国的余晖 3. 利令智昏
- Siebel Config: How to Enabling Simulation