3.Parking Lot Simulation

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'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
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值