Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- Main.cpp
- //
- // CS Project
- // COSC-1436.S02
- // Will
- // 4/25/2017
- //
- #include <iostream>
- #include <stdlib.h>
- #include "Commands.h"
- #include "Plane.h"
- #include "ReadInt.h"
- #include "ReadWord.h"
- using namespace std;
- Plane Alfa;
- Plane Bravo;
- Plane Lounge;
- void main()
- {
- Party NewParty;
- Alfa.NumSeats = 0;
- Bravo.NumSeats = 0;
- Lounge.NumSeats = 0;
- //set seats to zero before user enters anything
- bool SettleDown = false; //planes not flying
- PlaneName planeName;
- while (true) {
- cout << "Enter command: >";
- switch (GetCommand())
- {
- case InvalidCmd:
- cout << "Error - InvalidCmd try again" << endl << endl;
- break;
- //Cmd(Command)
- case CmdAlfa:
- Alfa.Name = PlaneAlfa;
- InitialPlane(Alfa);
- break;
- case CmdBravo:
- Bravo.Name = PlaneBravo;
- InitialPlane(Bravo);
- break;
- case CmdLounge:
- Lounge.Name = PlaneLounge;
- InitialPlane(Lounge);
- break;
- case CmdArrive:
- if (SettleDown)
- {
- planeName = GetPlaneName();
- if (planeName != -1) {
- NewParty.WhichPlane = planeName;
- }
- else {
- cout << "Wrong Plane Name!" << endl << endl;
- break;
- }
- NewParty.pName = ReadWord();
- NewParty.pAmt = ReadInt();
- AddPartyToPlaneOrLounge(NewParty);
- cout << endl;
- }
- else {
- cout << "Error! Please set all the size of Alfa, Bravo, and Lounge first. " << endl << endl;
- }
- break;
- case CmdFly:
- if (SettleDown)
- {
- switch (GetPlaneName())
- {
- case PlaneAlfa:
- FlyUntilNotFull(Alfa); // Fly the plane
- break;
- case PlaneBravo:
- FlyUntilNotFull(Bravo);
- break;
- default:
- break;
- }
- cout << endl;
- }
- else {
- cout << "Error! Please set all the size of Alfa, Bravo, and Lounge first. " << endl << endl;
- }
- break;
- case CmdShutdown:
- ShutDown();
- exit(0);
- break;
- default:
- cout << "Error." << endl;
- }
- if (Alfa.NumSeats && Bravo.NumSeats && Lounge.NumSeats)
- SettleDown = true;
- }
- }
- Plane.cpp
- #include <iostream>
- #include "commands.h"
- #include "Plane.h"
- #include "ReadInt.h"
- #include "ReadWord.h"
- using namespace std;
- extern Plane Alfa;
- extern Plane Bravo;
- extern Plane Lounge;
- char * PlaneNames[] = {
- "Alfa",
- "Bravo",
- "Lounge",
- };
- PlaneName GetPlaneName()
- {
- char * WhichPlane;
- int i;
- WhichPlane = ReadWord();
- for (i = 0; i < NumPlanes; i++) {
- if (_strcmpi(WhichPlane, PlaneNames[i]) == 0)
- {
- delete[] WhichPlane;
- return (PlaneName)i;
- }
- }
- delete[] WhichPlane;
- return InvalidPlane;
- }
- void InitialPlane(Plane & Plane) {
- int NumSeats;
- if (Plane.NumSeats > 0) {
- cout << "You only can set the size of " << PlaneNames[Plane.Name] << " once." << endl << endl;
- }
- else
- {
- if ((NumSeats = ReadInt())) {
- Plane.NumSeats = NumSeats;
- Plane.NumEmptySeats = Plane.NumSeats;
- Plane.NumParties = 0;
- Plane.Parties = new Party[Plane.NumSeats];
- cout << PlaneNames[Plane.Name] << " has " << Plane.NumSeats << " seats, and all of them are empty " << endl;
- }
- else {
- cout << "You did not set " << PlaneNames[Plane.Name] << " properly, set it again." << endl << endl;
- }
- }
- }
- void DisplayPlane(Plane Plane) {
- cout << PlaneNames[Plane.Name] << " has " << Plane.NumSeats << " seat, ";
- cout << "and has " << Plane.NumSeats - Plane.NumEmptySeats << " passages in it. " << endl;
- for (int i = 0; i < Plane.NumParties; i++) {
- cout << "Party Name: " << Plane.Parties[i].pName << ", Party Members: " << Plane.Parties[i].pAmt << endl;
- }
- }
- Plane & GetPlane(Party NewParty)
- {
- switch (NewParty.WhichPlane)
- {
- case InvalidPlane:
- cout << "Invalid Name. Try again. " << endl;
- break;
- case PlaneAlfa:
- return Alfa;
- break;
- case PlaneBravo:
- return Bravo;
- break;
- default:
- cout << "Error" << endl;
- }
- }
- void FlyPlaneOnce(Plane &Plane) {
- cout << "\n==========( " << PlaneNames[Plane.Name] << " is Flying )==========" << endl;
- DisplayPlane(Plane);
- //Display all parties on the plane
- for (int i = 0; i < Plane.NumParties; i++)
- {
- delete[] Plane.Parties[i].pName;
- }
- Plane.NumEmptySeats = Plane.NumSeats;
- Plane.NumParties = 0;
- cout << "========== Flight Ended ==========" << endl << endl;
- }
- void DeletePartyFromLounge(int i) {
- Lounge.NumEmptySeats += Lounge.Parties[i].pAmt;
- cout << "Lounge has " << Lounge.NumEmptySeats << " seats,";
- for (int j = i; j < Lounge.NumParties - 1; j++)
- Lounge.Parties[j] = Lounge.Parties[j + 1];
- Lounge.NumParties--;
- cout << " and " << Lounge.NumParties << " Parties left. " << endl << endl;
- }
- void AddPartyToPlane(Party NewParty) {
- Plane & Plane = GetPlane(NewParty);
- Plane.NumEmptySeats -= NewParty.pAmt;
- Plane.Parties[Plane.NumParties] = NewParty;
- Plane.NumParties++;
- cout << PlaneNames[NewParty.WhichPlane] << " have " << Plane.NumEmptySeats << " seats left." << endl;
- }
- void AddPartyToLounge(Party NewParty)
- {
- if (Lounge.NumEmptySeats >= NewParty.pAmt)
- {
- Lounge.NumEmptySeats -= NewParty.pAmt;
- Lounge.Parties[Lounge.NumParties] = NewParty;
- Lounge.NumParties++;
- cout << "Lounge have " << Lounge.NumEmptySeats << " seats left." << endl;
- }
- else {
- cout << "But Lounge only have " << Lounge.NumEmptySeats << " seats left. So, send the party away" << endl;
- delete[] NewParty.pName;
- }
- }
- void AddPartyToPlaneOrLounge(Party NewParty) {
- Plane & Plane = GetPlane(NewParty);
- if ((Plane).NumEmptySeats >= NewParty.pAmt)//enough seats?
- {
- AddPartyToPlane(NewParty);
- if (Plane.NumEmptySeats == 0)
- {
- FlyUntilNotFull(Plane);
- }
- }
- else //not enough empty seats, so...
- {
- if ((Plane.NumSeats == Alfa.NumSeats) || (Plane.NumSeats == Bravo.NumSeats) && Plane.NumSeats < NewParty.pAmt) //if the number of empty seats on the plane is lower than the size of the party
- {
- cout << "The Party is too big for " << PlaneNames[NewParty.WhichPlane] << ", so this party will be sent to the lounge." << endl;
- AddPartyToLounge(NewParty);
- if (Plane.NumSeats == Lounge.NumSeats && Plane.NumSeats < NewParty.pAmt)
- {
- cout << "The Party is too big for " << PlaneNames[NewParty.WhichPlane] << ", so this party will be sent away." << endl;
- delete[] NewParty.pName;
- }
- }
- else { // if the plane was empty, there would be enough seats
- cout << PlaneNames[NewParty.WhichPlane] << " only has " << Plane.NumEmptySeats << " seats left, send party to Lounge. " << endl;
- AddPartyToLounge(NewParty);
- }
- }
- }
- void ShutDown()
- {
- while (Alfa.NumParties >= 1) // keep flying the plane until there are no more people
- FlyUntilNotFull(Alfa); // fly the plane even if not full
- cout << endl;
- while (Bravo.NumParties >= 1)
- FlyUntilNotFull(Bravo);
- //while (Lounge.Parties != 0)
- //AddLoungeToPlane(Plane);
- //I had an idea to create a function for this particular circumstance based on my FlyUntilNotFull function, but I kept getting really strange errors
- delete[] Alfa.Parties;
- delete[] Bravo.Parties;
- delete[] Lounge.Parties;
- cout << "The airline was shut down." << endl << endl;
- }
- void FlyUntilNotFull(Plane & Plane) {
- FlyPlaneOnce(Plane);
- for (int i = 0; i < Lounge.NumParties; i++) //people in lounge on plane
- {
- if (Lounge.Parties[i].WhichPlane == Plane.Name)
- {
- if (Plane.NumEmptySeats >= Lounge.Parties[i].pAmt) {
- cout << "Moving party '" << Lounge.Parties[i].pName << "' from Lounge to " << PlaneNames[Plane.Name];
- cout << "£¬ which has " << Lounge.Parties[i].pAmt << " people." << endl;
- AddPartyToPlane(Lounge.Parties[i]);
- DeletePartyFromLounge(i); // delete Parties
- i--;
- if (Plane.NumEmptySeats == 0) { // if plane is full
- FlyPlaneOnce(Plane);
- i = -1;
- }
- }
- else {
- cout << "Party '" << Lounge.Parties[i].pName << "', which has " << Lounge.Parties[i].pAmt;
- cout << " party has too many to fit in the empty seats of " << PlaneNames[Plane.Name] << ", they stay at the lounge." << endl << endl;
- }
- }
- }
- cout << endl;
- }
- Plane.h
- #ifndef PLANE_H
- #define PLANE_H
- enum PlaneName {
- InvalidPlane = -1,
- PlaneAlfa,
- PlaneBravo,
- PlaneLounge,
- NumPlanes
- };
- struct Party
- {
- char * pName; //party name
- int pAmt; //how many in party
- PlaneName WhichPlane;
- };
- struct Plane
- {
- int NumEmptySeats; //Number of empty seats
- int NumSeats; //Number of seats
- int NumParties;
- Party * Parties;
- PlaneName Name;
- };
- void InitialPlane(Plane &);
- PlaneName GetPlaneName();
- void DisplayPlane(Plane);
- void FlyPlaneOnce(Plane &);
- void FlyUntilNotFull(Plane &);
- Plane & GetPlane(Party);
- void AddPartyToPlaneOrLounge(Party);
- void AddPartyToPlane(Party);
- void AddPartyToLounge(Party);
- void ShutDown();
- #endif
- Commands.cpp
- #include <iostream>
- #include "Commands.h"
- #include "ReadWord.h"
- using namespace std;
- char * CmdWords[] = {
- "Alfa",
- "Bravo",
- "Lounge",
- "Arrive",
- "Fly",
- "Shutdown"
- };
- Commands GetCommand()
- {
- char * Cmd;
- int i;
- Cmd = ReadWord();
- for (i = 0; i < NumCmds; i++)
- if (_strcmpi(Cmd, CmdWords[i]) == 0) // The command and then the additions to it. compare each command word until it equals what the user entered
- {
- delete[] Cmd; //delete what the user enters for the next time
- return (Commands)i;
- }
- else;
- delete[] Cmd;
- return InvalidCmd;
- }
- Commands.h
- #ifndef COMMANDS_H
- #define COMMANDS_H
- enum Commands {
- InvalidCmd = -1,
- CmdAlfa,
- CmdBravo,
- CmdLounge,
- CmdArrive,
- CmdFly,
- CmdShutdown,
- NumCmds
- };
- Commands GetCommand();
- #endif
- ReadWord.cpp
- #include <iostream>
- #include <ctype.h>
- #include <memory.h>
- #include "ReadWord.h"
- using namespace std;
- char * ReadWord()
- {
- const int FirstGuess(20);
- char c;
- char * pLine;
- char * pTemp;
- int NumCharsRead;
- int MaxCharsInLine;
- NumCharsRead = 0;
- MaxCharsInLine = FirstGuess;
- pLine = new char[MaxCharsInLine + 1];
- SkipSpaces();
- while (!isspace(c = cin.get()))
- {
- pLine[NumCharsRead++] = c;
- if (NumCharsRead >= MaxCharsInLine)//if the line is full
- {
- MaxCharsInLine += FirstGuess;
- pTemp = new char[MaxCharsInLine + 1];
- memcpy(pTemp, pLine, NumCharsRead);
- delete[] pLine;
- pLine = pTemp;
- }
- else;
- }
- pLine[NumCharsRead] = '\0';
- return pLine;
- }
- void SkipSpaces()
- {
- char c;
- while (isspace(c = cin.get()));
- cin.putback(c);
- }
- ReadWord.h
- #ifndef READ_LINE_H
- #define READ_LINE_H
- char * ReadWord();
- void SkipSpaces();
- #endif
- #pragma once
- ReadInt.cpp
- #include <iostream>
- #include "ReadInt.h"
- #include "ReadWord.h"
- using namespace std;
- int ReadInt()
- {
- char c;
- bool Negative;
- int Num;
- int NumCharsEntered;
- Negative = false;
- Num = 0;
- NumCharsEntered = 0;
- SkipSpaces();
- while (!isspace(c = cin.get()))
- {
- switch (c)
- {
- case '0':
- case '1':
- case '2':
- case '3':
- case '4':
- case '5':
- case '6':
- case '7':
- case '8':
- case '9':
- Num = Num * 10 + (c - '0');
- NumCharsEntered++;
- break;
- default:
- cout << '\a';
- }
- }
- return Num;
- }
- ReadInt.h
- #ifndef READ_INT_H
- #define READ_INT_H
- int ReadInt();
- #endif
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement