Metal Guitarist Forums banner
1 - 6 of 6 Posts

·
I don't like it.
Joined
·
11,071 Posts
Discussion Starter · #1 ·
Lots of code coming up... Here's the assignment:

ASSIGNMENT:

Your employer has determined a market for a simple airline ticketing system for a very simple airline (Tree Top Airways - TTA). The airline only has two aircraft, named ALFA and BRAVO. Initially, the ticket system will determine the number of seats on each plane and in the lounge. The ticketing system is to be installed at the airport ticket booth for the airline and will operate under the following rules:

1) If a party arrives and there is enough room on the plane requested, they are allowed to board and a message to that effect is printed.

2) If a party arrives and there is not enough room on the plane requested due to people already being on the plane, the party is directed to wait in the lounge and a message to that effect is printed.

3) If a party arrives and there is not enough room on the plane requested because the plane is just too small, the party is turned away and a message to that effect is printed.

4) If a party is directed to the lounge and there is not enough room in the lounge, the party is turned away.

5) Parties are never split.

6) A plane will fly whenever it becomes full and a list of all parties on board will be printed.

7) A plane may fly upon a command from the ticket clerk. Again a list of all parties on board will be printed.

8) The ticket clerk will enter when a plane flys and upon the return of the plane the system will move people from the lounge to the plane in the order in which the parties arrived, but will skip parties that cannot fit and search for those which can fit. A list of those parties boarding the plane will be printed.

9) When the airline shuts down for the night, the airplanes will continue to fly and return (without the ticket clerk performing any action) until all parties in the lounge have flown with appropriate messages being printed.

10) Party names do not have a maximum size.

The commands the system will accept are the following:

ALFA <size>
BRAVO <size>
LOUNGE <size>
FLY <airplane>
ARRIVE <airplane> <party> <size>
SHUTDOWN

where: <airplane> is either ALFA or BRAVO.
<party> is a name of unlimited length.
<size> is an integer number

Fields in the input will be separated by some amount of whitespace.

The ticket clerk is a terrible typist.
Here's my main.cpp:

Code:
#include <iostream>

using namespace std;

#include "Party.h"
#include "Plane.h"
#include "ReadFunctions.h"
#include "ReadString.h"

enum CommandList {ALFA, BRAVO, LOUNGE, ARRIVE, FLY, SHUTDOWN, INVALID};

void		Move		(Plane &, Plane &);
CommandList	ReadCommand	();

void main ()
	{
	long		i;
	long		j;
	Plane		Alfa;
	Plane		Bravo;
	Plane		Lounge;
	Party		Parties;

	bool		Done;
	bool		AlfaInitialized;
	bool		BravoInitialized;
	bool		LoungeInitialized;
	CommandList	Command;

	AlfaInitialized		= false;
	BravoInitialized	= false;
	LoungeInitialized	= false;

	do	{
		if (!AlfaInitialized)
							{
							Alfa.PlaneAbbrev	= 'A';
							cout << "Enter number of ALFA seats: ";
							Alfa.NumSeats		= ReadInteger ();
							Alfa.NumEmptySeats	= Alfa.NumSeats;
							Alfa.NumParties		= 0;
							Alfa.pParty			= new Party [Alfa.NumSeats];
							AlfaInitialized		= true;
							cout << "ALFA initialized." << endl;
							}
						else
							cout << "ALFA is already initialized" << endl;
		if (!BravoInitialized)
							{
							Bravo.PlaneAbbrev	= 'B';
							cout << "Enter number of BRAVO seats: ";
							Bravo.NumSeats		= ReadInteger ();
							Bravo.NumEmptySeats	= Bravo.NumSeats;
							Bravo.NumParties		= 0;
							Bravo.pParty			= new Party [Bravo.NumSeats];
							BravoInitialized		= true;
							cout << "BRAVO initialized." << endl;
							}
						else
							cout << "BRAVO is already initialized" << endl;
		
		
		if (!LoungeInitialized)
							{
							Lounge.PlaneAbbrev	= 'L';
							cout << "Enter number of Lounge seats: ";
							Lounge.NumSeats		= ReadInteger ();
							Lounge.NumEmptySeats	= Lounge.NumSeats;
							Lounge.NumParties		= 0;
							Lounge.pParty			= new Party [Lounge.NumSeats];
							LoungeInitialized		= true;
							cout << "Lounge initialized." << endl;
							}
						else
							cout << "Lounge is already initialized" << endl;					
			
		} while (!AlfaInitialized || !BravoInitialized || !LoungeInitialized);
	Done = false;
	do	{
		cout << "Enter a command (ARRIVE, FLY, SHUTDOWN): ";
		Command = ReadCommand ();
		switch (Command)
			{
			case ARRIVE:
					cout << "Enter number of members in party: ";
					Parties.Size = ReadInteger ();	
					cout << "Party size entered is " << Parties.Size << endl;
					cout << "Enter which plane the party will be on: ";
					Parties.Plane = ReadCommand ();
					switch (Parties.Plane)
						{
						case ALFA:
							Parties.pName = new char [Parties.Size];
							Parties.Plane = 'A';
							for (i = 0; i < Parties.Size; i++)
								{
								cout << "Enter name [" << i + 1 << "] or press Enter to continue: ";
								Parties.pName [i] = *ReadString ();
								}
							break;
						case BRAVO:
							Parties.pName = new char [Parties.Size];
							Parties.Plane = 'B';
							for (i = 0; i < Parties.Size; i++)
								{
								cout << "Enter name [" << i + 1 << "] or press Enter to continue: ";
								Parties.pName [i] = *ReadString ();
								}				
							break;
						default:
							cout << "Invalid Command." << endl;
						}
			
			
			case FLY:
				// if plane is Alfa
						// print the names of all parties on the plane
						// take everybody off the plane
						Move (Alfa, Lounge);
					//else	// plane is Bravo
						// print the names of all parties on the plane
						// take everybody off the plane
						Move (Bravo, Lounge);
				break;
			case SHUTDOWN:
				Done = true;
				break;
			default:
				cout << "Invalid Command" << endl;
			}
		} while (!Done);
	}

CommandList ReadCommand ()
	{
	char *		CommandString;
	CommandList	Command;

	CommandString = ReadString ();
	if (_strcmpi (CommandString, "ALFA") == 0)
			Command = ALFA;
		else
			if (_strcmpi (CommandString, "BRAVO") == 0)
					Command = BRAVO;
				else
					if (_strcmpi (CommandString, "LOUNGE") == 0)
							Command = LOUNGE;
						else
							if (_strcmpi (CommandString, "ARRIVE") == 0)
									Command = ARRIVE;
								else
									if (_strcmpi (CommandString, "FLY") == 0)
											Command = FLY;
										else
											if (_strcmpi (CommandString, "SHUTDOWN") == 0)
													Command = SHUTDOWN;
												else
													Command = INVALID;
	delete [] CommandString;
	return Command;
	}

void Move (Plane & To, Plane & From)
	{
	long	i;
	long	j;

	for (i = 0; i < From.NumParties; i++)
		if ((From.pParty [i].Plane == To.PlaneAbbrev) && (From.pParty [i].Size <= To.NumEmptySeats))
				{
				To.pParty [To.NumParties] = From.pParty [i];
				To.NumParties++;
				To.NumEmptySeats			-= From.pParty [i].Size;
				From.NumEmptySeats			+= From.pParty [i].Size;
				for (j = i + 1; j < From.NumParties; j++)
					From.pParty [j - 1] = From.pParty [j];
				From.NumParties--;
				i--;
				}
			else;
	}
Party.h:

Code:
#ifndef PARTY_H
#define PARTY_H

struct Party
	{
	char *	pName;
	long	Size;
	char 	Plane;
	};

#endif
Plane.h:
Code:
#ifndef PLANE_H
#define PLANE_H

#include "Party.h"

struct Plane
	{
	long	NumSeats;
	long	NumEmptySeats;
	long	NumParties;
	Party *	pParty;
	char	PlaneAbbrev;
	};

#endif
ReadString.cpp
Code:
#include <iostream>

using namespace std;

#include <ctype.h>
#include <memory.h>

#include "ReadString.h"

char * ReadString ()
	{
	const	long	StartingSize (50);
			long	CurrentNumChars;
			long	CurrentSize;
			char *	pStr;
			char	c;

	CurrentSize		= StartingSize;
	pStr			= new char [CurrentSize + 1];
	CurrentNumChars	= 0;

	while (!isspace (c = cin.get ()))
		{
		pStr [CurrentNumChars++] = c;
		if (CurrentNumChars >= CurrentSize)
				{
				char * pChar;
				CurrentSize += StartingSize;
				pChar		=  new char [CurrentSize + 1];
				memcpy (pChar, pStr, CurrentNumChars);
				delete [] pStr;
				pStr = pChar;
				}
			else;
		}
	pStr [CurrentNumChars] = '\0';
	return pStr;
	}
ReadFunctions.cpp:
Code:
#include <iostream>

using namespace std;

#include <conio.h>
#include <ctype.h>	// could use <cctype>
#include <math.h>	// could use <cmath>

#include "ReadFunctions.h"

static void BackSpace ()	// static means this function can only be used by other functions in this file
	{
	_putch ('\b');
	_putch (' ');
	}

double ReadDouble ()
	{
	char	c;
	bool	Digits		(false);
	int		Fraction	(0);
	double	Num			(0.0);
	int		Sign		(0);

	while (!isspace (c = static_cast <char> (_getch ())))
		{
		switch (c)
			{
			case '+':
			case '-':
				if ((Sign == 0) && !Digits)
						if (c == '+')
								Sign = 1;
							else  // c == '-'
								Sign = -1;
					else
						c = '\a';
				break;
			case '.':
				if (Fraction == 0)
						Fraction = 1;
					else
						c = '\a';
				break;
			case '\b':
				if (Fraction > 0)
						{
						BackSpace ();
						Fraction /= 10;
						if (Fraction > 0)
								{
								Num = static_cast <int> (Num * Fraction);
								Num /= Fraction;
								}
							else;
						}
					else
						if (Digits)
								{
								BackSpace ();
								Num = static_cast <int> (Num) / 10;
								if (Num == 0.0)
										Digits = false;
									else;
								}
							else
								if (Sign != 0)
										{
										BackSpace ();
										Sign = 0;
										}
									else
										c = '\a';
				break;
			case '0':
				if (!Digits && (Fraction == 0))	// doesn't do leading zeros
						{
						c = '\a';
						break;
						}
					else;
			case '1':
			case '2':
			case '3':
			case '4':
			case '5':
			case '6':
			case '7':
			case '8':
			case '9':
				Digits = true;
				if (Fraction > 0)
						{
						Fraction *= 10;
						Num = Num + ((c - '0') / static_cast <double> (Fraction));
						}
					else
						Num = (Num * 10.0) + (c - '0');
				break;
			default:
				c = '\a';
			}
		_putch (c);
		}
	if (c == '\r')
			c = '\n';
		else;
	_putch (c);
	if (Sign < 0)
			Num = -Num;
		else;
	return Num;
	}


// this has been changed to work with cin

int ReadInteger ()
	{
	char	c;
	bool	Digits	(false);
	int		Num		(0);
	int		Sign	(0);

//	while (!isspace (c = static_cast <char> (_getch ())))
	while (!isspace (c = static_cast <char> (cin.get ())))
		{
		switch (c)
			{
			case '+':
			case '-':
				if ((Sign == 0) && !Digits)
						if (c == '+')
								Sign = 1;
							else  // c == '-'
								Sign = -1;
					else
						c = '\a';
				break;
			case '\b':
				if (Digits)
						{
						BackSpace ();
						Num /= 10;
						if (Num == 0)
								Digits = false;
							else;
						}
					else
						if (Sign != 0)
								{
								BackSpace ();
								Sign = 0;
								}
							else
								c = '\a';
				break;
			case '0':
				if (!Digits)
						{
						c = '\a';
						break;
						}
					else;
			case '1':
			case '2':
			case '3':
			case '4':
			case '5':
			case '6':
			case '7':
			case '8':
			case '9':
				Digits = true;
				Num = (Num * 10) + (c - '0');
				break;
			default:
				c = '\a';
			}
//		_putch (c);
		}
	if (c == '\r')
			c = '\n';
		else;
//	_putch (c);
	if (Sign < 0)
			Num = -Num;
		else;
	return Num;
	}

UINT ReadHex ()
	{
	char	c;
	bool	Digits	(false);
	UINT	Num		(0);

	while (!isspace (c = static_cast <char> (_getch ())))
		{
		switch (c)
			{
			case '\b':
				if (Digits)
						{
						BackSpace ();
						Num /= 16;
						if (Num == 0)
								Digits = false;
							else;
						}
					else
						c = '\a';
				break;
			case '0':
				if (!Digits)
						{
						c = '\a';
						break;
						}
					else;
			case '1':
			case '2':
			case '3':
			case '4':
			case '5':
			case '6':
			case '7':
			case '8':
			case '9':
				Digits = true;
				Num = (Num * 16) + (c - '0');
				break;
			case 'a':
			case 'b':
			case 'c':
			case 'd':
			case 'e':
			case 'f':
				Digits = true;
				Num = (Num * 16) + (c - 'a') + 10;
				break;
			case 'A':
			case 'B':
			case 'C':
			case 'D':
			case 'E':
			case 'F':
				Digits = true;
				Num = (Num * 16) + (c - 'A') + 10;
				break;
			default:
				c = '\a';
			}
		_putch (c);
		}
	if (c == '\r')
			c = '\n';
		else;
	_putch (c);
	return Num;
	}
That's everything for now... The professor got most of the stuff here started for us, only stuff I've added is in main.cpp, giving it the basic functionality... I can't figure out where the hell to go from here though...
 

·
I don't like it.
Joined
·
11,071 Posts
Discussion Starter · #2 ·
If I can't figure this out tonight I can go in tomorrow and talk with the programming tutor, see if I can't figure it out by then...

Talked with my dad, he gave me some good ideas.
 

·
Registered
Joined
·
1 Posts
Hey, I know you posted this like two years ago, but I was wondering if you had the completed version of this project you could post. I have this same assignment due.

Thanks
 

·
Read Only
Joined
·
10 Posts
Haha, this is random as hell, but hey - if we metalheads can't help someone with their homework, then the whole rebellious metal attitude that we loved as kids goes out the window!

Stick it to the man, Adam - hook him up!
 
1 - 6 of 6 Posts
Top