3.Parking Lot Simulation

本篇博客介绍了一个使用C++ STL堆栈适配器模拟单通道停车场的程序设计案例。学生需掌握指针使用、文件I/O、动态内存管理等技能。案例通过模拟车辆进出,记录移车次数,加深了对堆栈操作的理解。

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.

#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
*/


数据结构第16次作业,hash表 Spellchecking Prerequisites, Goals, and Outcomes Prerequisites: Students should have mastered the following prerequisite skills. • Hash Tables - Understanding of the concept of a recursive function • Inheritance - Enhancing an existing data structure through specialization Goals: This assignment is designed to reinforce the student&#39;s understanding of the use of hash tables as searchable containers. Outcomes: Students successfully completing this assignment would master the following outcomes. • Familiarize how to use hash tables, specifically hash sets Background Any word processing application will typically contain a spell check feature. Not only does this feature point out potentially misspelled words; it also suggests possible corrections. Description The program to be completed for this assessment is a spell checker. Below is a screen shot of the program in execution? The program begins by opening a word list text file, specified by a command line parameter. The program outputs an error message and terminates if it cannot open the specified word list text file. A sample word list text file (wordlist.txt) is given in the supplied wordlist.zip archive. After successfully opening the specified word list text file, the program then stores each word into a hash table. The program then opens a file to spell check. This user specifies this file through the command line. After opening this file, the program then compares each word in the file against the words stored in the hash table. The program considers a word to be misspelled if the word does not exist in the hash table. When this occurs, the program displays the line number the word appeared in, the word, and a list of possible corrections. The list of possible corrections for a misspelled word is generated using a simple algorithm. Any variation of a misspelled word that is itself a word (i.e. it is found in the word list file) is a possible correction. Your solution to this asses
timing is 63983 ns, write address [0x88015604] [0x05581]= 0x00005555 ... simulation is on, at time 64000 ns... timing is 64103 ns, read address [0x88015604] [0x05581] = 0x00005555 ... simulation is on, at time 64200 ns... timing is 64243 ns, write address [0x88015608] [0x05582]= 0x0000aaaa ... timing is 64363 ns, read address [0x88015608] [0x05582] = 0x0000aaaa ... simulation is on, at time 64400 ns... timing is 64503 ns, write address [0x8801560c] [0x05583]= 0x00005555 ... simulation is on, at time 64600 ns... timing is 64623 ns, read address [0x8801560c] [0x05583] = 0x00005555 ... timing is 64763 ns, write address [0x88015610] [0x05584]= 0x0000aaaa ... simulation is on, at time 64800 ns... timing is 64883 ns, read address [0x88015610] [0x05584] = 0x0000aaaa ... simulation is on, at time 65000 ns... timing is 65023 ns, write address [0x88015614] [0x05585]= 0x00005555 ... timing is 65143 ns, read address [0x88015614] [0x05585] = 0x00005555 ... simulation is on, at time 65200 ns... timing is 65283 ns, write address [0x88015618] [0x05586]= 0x0000aaaa ... simulation is on, at time 65400 ns... timing is 65403 ns, read address [0x88015618] [0x05586] = 0x0000aaaa ... timing is 65543 ns, write address [0x8801561c] [0x05587]= 0x00005555 ... simulation is on, at time 65600 ns... timing is 65663 ns, read address [0x8801561c] [0x05587] = 0x00005555 ... simulation is on, at time 65800 ns... timing is 65803 ns, write address [0x88015620] [0x05588]= 0x0000aaaa ... timing is 65923 ns, read address [0x88015620] [0x05588] = 0x0000aaaa ... simulation is on, at time 66000 ns... timing is 66063 ns, write address [0x88015624] [0x05589]= 0x00005555 ... timing is 66183 ns, read address [0x88015624] [0x05589] = 0x00005555 ... simulation is on, at time 66200 ns... timing is 66323 ns, write address [0x88015628] [0x0558a]= 0x0000aaaa ... simulation is on, at time 66400 ns... timing is 66443 ns, read address [0x88015628] [0x0558a] = 0x0000aaaa ... timing is 66583 ns, write address [0x8801562c] [0x0558b]= 0x00005555 ... simulation is on, at time 66600 ns... timing is 66703 ns, read address [0x8801562c] [0x0558b] = 0x00005555 ... simulation is on, at time 66800 ns... timing is 66843 ns, write address [0x88015630] [0x0558c]= 0x0000aaaa ... timing is 66963 ns, read address [0x88015630] [0x0558c] = 0x0000aaaa ... simulation is on, at time 67000 ns... timing is 67103 ns, write address [0x88015634] [0x0558d]= 0x00005555 ... simulation is on, at time 67200 ns... timing is 67223 ns, read address [0x88015634] [0x0558d] = 0x00005555 ... timing is 67363 ns, write address [0x88015638] [0x0558e]= 0x0000aaaa ... simulation is on, at time 67400 ns... timing is 67483 ns, read address [0x88015638] [0x0558e] = 0x0000aaaa ... simulation is on, at time 67600 ns... timing is 67623 ns, write address [0x8801563c] [0x0558f]= 0x00005555 ... timing is 67743 ns, read address [0x8801563c] [0x0558f] = 0x00005555 ... simulation is on, at time 67800 ns... timing is 67883 ns, write address [0x88015640] [0x05590]= 0x0000aaaa ... simulation is on, at time 68000 ns... timing is 68003 ns, read address [0x88015640] [0x05590] = 0x0000aaaa ... timing is 68143 ns, write address [0x88015800] [0x05600]= 0x00aaaaaa ... simulation is on, at time 68200 ns... timing is 68263 ns, read address [0x88015800] [0x05600] = 0x00aaaaaa ... simulation is on, at time 68400 ns... timing is 68403 ns, write address [0x88015804] [0x05601]= 0x55555555 ... timing is 68523 ns, read address [0x88015804] [0x05601] = 0x55555555 ... simulation is on, at time 68600 ns... timing is 68663 ns, write address [0x88015808] [0x05602]= 0xaaaaaaaa ... timing is 68783 ns, read address [0x88015808] [0x05602] = 0xaaaaaaaa ... simulation is on, at time 68800 ns... timing is 68923 ns, write address [0x8801580c] [0x05603]= 0x55555555 ... simulation is on, at time 69000 ns... timing is 69043 ns, read address [0x8801580c] [0x05603] = 0x55555555 ... timing is 69183 ns, write address [0x88015810] [0x05604]= 0xaaaaaaaa ... simulation is on, at time 69200 ns... timing is 69303 ns, read address [0x88015810] [0x05604] = 0xaaaaaaaa ... simulation is on, at time 69400 ns... timing is 69443 ns, write address [0x88015814] [0x05605]= 0x55555555 ... timing is 69563 ns, read address [0x88015814] [0x05605] = 0x55555555 ... simulation is on, at time 69600 ns... timing is 69703 ns, write address [0x88015818] [0x05606]= 0xaaaaaaaa ... simulation is on, at time 69800 ns... timing is 69823 ns, read address [0x88015818] [0x05606] = 0xaaaaaaaa ... timing is 69963 ns, write address [0x8801581c] [0x05607]= 0x55555555 ... simulation is on, at time 70000 ns... timing is 70083 ns, read address [0x8801581c] [0x05607] = 0x55555555 ... simulation is on, at time 70200 ns... timing is 70223 ns, write address [0x88015820] [0x05608]= 0xaaaaaaaa ... timing is 70343 ns, read address [0x88015820] [0x05608] = 0xaaaaaaaa ... simulation is on, at time 70400 ns... timing is 70483 ns, write address [0x88015824] [0x05609]= 0x55555555 ... simulation is on, at time 70600 ns... timing is 70603 ns, read address [0x88015824] [0x05609] = 0x55555555 ... timing is 70743 ns, write address [0x88015828] [0x0560a]= 0xaaaaaaaa ... simulation is on, at time 70800 ns... timing is 70863 ns, read address [0x88015828] [0x0560a] = 0xaaaaaaaa ... simulation is on, at time 71000 ns... timing is 71003 ns, write address [0x8801582c] [0x0560b]= 0x55555555 ... timing is 71123 ns, read address [0x8801582c] [0x0560b] = 0x55555555 ... simulation is on, at time 71200 ns... timing is 71263 ns, write address [0x88015830] [0x0560c]= 0xaaaaaaaa ... timing is 71383 ns, read address [0x88015830] [0x0560c] = 0xaaaaaaaa ... simulation is on, at time 71400 ns... timing is 71523 ns, write address [0x88015834] [0x0560d]= 0x55555555 ... simulation is on, at time 71600 ns... timing is 71643 ns, read address [0x88015834] [0x0560d] = 0x55555555 ... timing is 71783 ns, write address [0x88015838] [0x0560e]= 0xaaaaaaaa ... simulation is on, at time 71800 ns... timing is 71903 ns, read address [0x88015838] [0x0560e] = 0xaaaaaaaa ... simulation is on, at time 72000 ns... timing is 72043 ns, write address [0x8801583c] [0x0560f]= 0x55555555 ... timing is 72163 ns, read address [0x8801583c] [0x0560f] = 0x55555555 ... simulation is on, at time 72200 ns... timing is 72303 ns, write address [0x88015840] [0x05610]= 0xaaaaaaaa ... simulation is on, at time 72400 ns... timing is 72423 ns, read address [0x88015840] [0x05610] = 0xaaaaaaaa ... timing is 72563 ns, write address [0x88015844] [0x05611]= 0x55555555 ... simulation is on, at time 72600 ns... timing is 72683 ns, read address [0x88015844] [0x05611] = 0x55555555 ... simulation is on, at time 72800 ns... timing is 72823 ns, write address [0x88015848] [0x05612]= 0xaaaaaaaa ... timing is 72943 ns, read address [0x88015848] [0x05612] = 0xaaaaaaaa ... simulation is on, at time 73000 ns... timing is 73083 ns, write address [0x8801584c] [0x05613]= 0x55555555 ... simulation is on, at time 73200 ns... timing is 73203 ns, read address [0x8801584c] [0x05613] = 0x55555555 ... timing is 73343 ns, write address [0x88015850] [0x05614]= 0xaaaaaaaa ... simulation is on, at time 73400 ns... timing is 73463 ns, read address [0x88015850] [0x05614] = 0xaaaaaaaa ... simulation is on, at time 73600 ns... timing is 73603 ns, write address [0x88015854] [0x05615]= 0x55555555 ... timing is 73723 ns, read address [0x88015854] [0x05615] = 0x55555555 ... simulation is on, at time 73800 ns... timing is 73863 ns, write address [0x88015858] [0x05616]= 0xaaaaaaaa ... timing is 73983 ns, read address [0x88015858] [0x05616] = 0xaaaaaaaa ... simulation is on, at time 74000 ns... timing is 74123 ns, write address [0x8801585c] [0x05617]= 0x55555555 ... simulation is on, at time 74200 ns... timing is 74243 ns, read address [0x8801585c] [0x05617] = 0x55555555 ... timing is 74383 ns, write address [0x88015860] [0x05618]= 0xaaaaaaaa ... simulation is on, at time 74400 ns... timing is 74503 ns, read address [0x88015860] [0x05618] = 0xaaaaaaaa ... simulation is on, at time 74600 ns... timing is 74643 ns, write address [0x88015900] [0x05640]= 0x0000aaaa ... timing is 74763 ns, read address [0x88015900] [0x05640] = 0x0000aaaa ... simulation is on, at time 74800 ns... timing is 74903 ns, write address [0x88015904] [0x05641]= 0x00005555 ... simulation is on, at time 75000 ns... timing is 75023 ns, read address [0x88015904] [0x05641] = 0x00005555 ... timing is 75163 ns, write address [0x88015908] [0x05642]= 0x0000aaaa ... simulation is on, at time 75200 ns... timing is 75283 ns, read address [0x88015908] [0x05642] = 0x0000aaaa ... simulation is on, at time 75400 ns... timing is 75423 ns, write address [0x8801590c] [0x05643]= 0x00005555 ... timing is 75543 ns, read address [0x8801590c] [0x05643] = 0x00005555 ... simulation is on, at time 75600 ns... timing is 75683 ns, write address [0x88015910] [0x05644]= 0x0000aaaa ... simulation is on, at time 75800 ns... timing is 75803 ns, read address [0x88015910] [0x05644] = 0x0000aaaa ... 继续检查
最新发布
09-24
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值