Computer Science 261

Exercise set #

Due:  Tuesday, Oct. 26

 

Consider the following code (note that this includes a class template for a queue class, a helper routine GetCh, and the creator procedure for the queue ADT):

#include <iostream.h>

const int QueueSize=11;	// 10 items max in stack
void GetChar(char &ch);
class CharQueue{
public:
	CharQueue();	//constructor

	char get();	//accessors
	void put(char ch);
	bool IsFull();
	bool IsEmpty();

private:
	char theQueue[QueueSize];
	int front, back;	//pointers.  front points to
	//	the item at the front of the queue, back points
	//	to the next available spot to put something.
	//	When front == back the queue is empty.
};

void GetChar(char &ch)
{
	cin.get(ch);
	while ((ch == '\n') || (ch == ' '))
		cin.get(ch);	// skip over noise
}

int main()
{
	// type characters in, load (or attempt to load) them
	//	in to a queue.  ';' causes the contents of the queue
	//  to be printed, 'q' stops the program

	CharQueue MyQueue;
	char ch;

	cout << "\nType in characters.  ';' to print characters typed\n";
	cout << "There are a total of " << QueueSize-1 << endl;
	cout << "characters possible in the queue: typing more than this";
	cout << "should cause an error message to be printed " << endl << endl;

	GetChar(ch);
while (ch != 'q')
	{
		while (ch != ';')
		{ 
			MyQueue.put(ch);
			GetChar(ch);
		}
		cout << "\nThe String is: ";
		while (!MyQueue.IsEmpty())
		{
			ch = MyQueue.get();
			cout << ch;
		}
		cout << endl;
		GetChar(ch);
	}
	return 0;
}

CharQueue::CharQueue()
{
	front = 0;
	back = 0;

	return;
}

 

Include all this in a program (called queue.cpp). Please note that you need only to add the get, put, IsFull and IsEmpty accessor routines.