Advertisement
Guest User

Untitled

a guest
Apr 28th, 2017
94
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 11.12 KB | None | 0 0
  1. Main.cpp
  2.  
  3. //
  4. // CS Project
  5. // COSC-1436.S02
  6. // Will
  7. // 4/25/2017
  8. //
  9.  
  10. #include <iostream>
  11. #include <stdlib.h>
  12.  
  13. #include "Commands.h"
  14. #include "Plane.h"
  15. #include "ReadInt.h"
  16. #include "ReadWord.h"
  17.  
  18. using namespace std;
  19.  
  20. Plane Alfa;
  21. Plane Bravo;
  22. Plane Lounge;
  23.  
  24. void main()
  25. {
  26.  
  27. Party NewParty;
  28.  
  29. Alfa.NumSeats = 0;
  30. Bravo.NumSeats = 0;
  31. Lounge.NumSeats = 0;
  32. //set seats to zero before user enters anything
  33. bool SettleDown = false; //planes not flying
  34.  
  35. PlaneName planeName;
  36.  
  37. while (true) {
  38.  
  39. cout << "Enter command: >";
  40.  
  41. switch (GetCommand())
  42. {
  43. case InvalidCmd:
  44. cout << "Error - InvalidCmd try again" << endl << endl;
  45. break;
  46.  
  47. //Cmd(Command)
  48. case CmdAlfa:
  49. Alfa.Name = PlaneAlfa;
  50. InitialPlane(Alfa);
  51. break;
  52.  
  53. case CmdBravo:
  54. Bravo.Name = PlaneBravo;
  55. InitialPlane(Bravo);
  56. break;
  57.  
  58. case CmdLounge:
  59. Lounge.Name = PlaneLounge;
  60. InitialPlane(Lounge);
  61. break;
  62.  
  63. case CmdArrive:
  64. if (SettleDown)
  65. {
  66. planeName = GetPlaneName();
  67. if (planeName != -1) {
  68. NewParty.WhichPlane = planeName;
  69. }
  70. else {
  71. cout << "Wrong Plane Name!" << endl << endl;
  72. break;
  73. }
  74.  
  75. NewParty.pName = ReadWord();
  76. NewParty.pAmt = ReadInt();
  77. AddPartyToPlaneOrLounge(NewParty);
  78. cout << endl;
  79. }
  80. else {
  81. cout << "Error! Please set all the size of Alfa, Bravo, and Lounge first. " << endl << endl;
  82. }
  83. break;
  84.  
  85. case CmdFly:
  86. if (SettleDown)
  87. {
  88. switch (GetPlaneName())
  89. {
  90. case PlaneAlfa:
  91. FlyUntilNotFull(Alfa); // Fly the plane
  92. break;
  93. case PlaneBravo:
  94. FlyUntilNotFull(Bravo);
  95. break;
  96. default:
  97. break;
  98. }
  99. cout << endl;
  100. }
  101. else {
  102. cout << "Error! Please set all the size of Alfa, Bravo, and Lounge first. " << endl << endl;
  103. }
  104. break;
  105.  
  106. case CmdShutdown:
  107. ShutDown();
  108. exit(0);
  109. break;
  110.  
  111. default:
  112. cout << "Error." << endl;
  113. }
  114.  
  115. if (Alfa.NumSeats && Bravo.NumSeats && Lounge.NumSeats)
  116. SettleDown = true;
  117. }
  118.  
  119. }
  120.  
  121. Plane.cpp
  122.  
  123. #include <iostream>
  124.  
  125. #include "commands.h"
  126. #include "Plane.h"
  127. #include "ReadInt.h"
  128. #include "ReadWord.h"
  129.  
  130. using namespace std;
  131.  
  132.  
  133. extern Plane Alfa;
  134. extern Plane Bravo;
  135. extern Plane Lounge;
  136.  
  137. char * PlaneNames[] = {
  138. "Alfa",
  139. "Bravo",
  140. "Lounge",
  141. };
  142.  
  143. PlaneName GetPlaneName()
  144. {
  145. char * WhichPlane;
  146. int i;
  147.  
  148. WhichPlane = ReadWord();
  149. for (i = 0; i < NumPlanes; i++) {
  150. if (_strcmpi(WhichPlane, PlaneNames[i]) == 0)
  151. {
  152. delete[] WhichPlane;
  153. return (PlaneName)i;
  154. }
  155.  
  156. }
  157. delete[] WhichPlane;
  158. return InvalidPlane;
  159. }
  160.  
  161.  
  162. void InitialPlane(Plane & Plane) {
  163.  
  164. int NumSeats;
  165.  
  166. if (Plane.NumSeats > 0) {
  167. cout << "You only can set the size of " << PlaneNames[Plane.Name] << " once." << endl << endl;
  168. }
  169. else
  170. {
  171. if ((NumSeats = ReadInt())) {
  172. Plane.NumSeats = NumSeats;
  173. Plane.NumEmptySeats = Plane.NumSeats;
  174. Plane.NumParties = 0;
  175. Plane.Parties = new Party[Plane.NumSeats];
  176. cout << PlaneNames[Plane.Name] << " has " << Plane.NumSeats << " seats, and all of them are empty " << endl;
  177. }
  178. else {
  179. cout << "You did not set " << PlaneNames[Plane.Name] << " properly, set it again." << endl << endl;
  180. }
  181. }
  182. }
  183.  
  184. void DisplayPlane(Plane Plane) {
  185.  
  186. cout << PlaneNames[Plane.Name] << " has " << Plane.NumSeats << " seat, ";
  187. cout << "and has " << Plane.NumSeats - Plane.NumEmptySeats << " passages in it. " << endl;
  188. for (int i = 0; i < Plane.NumParties; i++) {
  189. cout << "Party Name: " << Plane.Parties[i].pName << ", Party Members: " << Plane.Parties[i].pAmt << endl;
  190. }
  191. }
  192.  
  193. Plane & GetPlane(Party NewParty)
  194. {
  195. switch (NewParty.WhichPlane)
  196. {
  197. case InvalidPlane:
  198. cout << "Invalid Name. Try again. " << endl;
  199. break;
  200. case PlaneAlfa:
  201. return Alfa;
  202. break;
  203. case PlaneBravo:
  204. return Bravo;
  205. break;
  206. default:
  207. cout << "Error" << endl;
  208.  
  209. }
  210.  
  211. }
  212.  
  213. void FlyPlaneOnce(Plane &Plane) {
  214.  
  215. cout << "\n==========( " << PlaneNames[Plane.Name] << " is Flying )==========" << endl;
  216.  
  217. DisplayPlane(Plane);
  218. //Display all parties on the plane
  219.  
  220. for (int i = 0; i < Plane.NumParties; i++)
  221. {
  222. delete[] Plane.Parties[i].pName;
  223. }
  224. Plane.NumEmptySeats = Plane.NumSeats;
  225. Plane.NumParties = 0;
  226. cout << "========== Flight Ended ==========" << endl << endl;
  227.  
  228. }
  229.  
  230.  
  231. void DeletePartyFromLounge(int i) {
  232. Lounge.NumEmptySeats += Lounge.Parties[i].pAmt;
  233. cout << "Lounge has " << Lounge.NumEmptySeats << " seats,";
  234.  
  235. for (int j = i; j < Lounge.NumParties - 1; j++)
  236. Lounge.Parties[j] = Lounge.Parties[j + 1];
  237.  
  238. Lounge.NumParties--;
  239. cout << " and " << Lounge.NumParties << " Parties left. " << endl << endl;
  240. }
  241.  
  242. void AddPartyToPlane(Party NewParty) {
  243.  
  244. Plane & Plane = GetPlane(NewParty);
  245.  
  246.  
  247. Plane.NumEmptySeats -= NewParty.pAmt;
  248.  
  249. Plane.Parties[Plane.NumParties] = NewParty;
  250.  
  251.  
  252. Plane.NumParties++;
  253. cout << PlaneNames[NewParty.WhichPlane] << " have " << Plane.NumEmptySeats << " seats left." << endl;
  254.  
  255. }
  256.  
  257. void AddPartyToLounge(Party NewParty)
  258. {
  259. if (Lounge.NumEmptySeats >= NewParty.pAmt)
  260. {
  261. Lounge.NumEmptySeats -= NewParty.pAmt;
  262.  
  263. Lounge.Parties[Lounge.NumParties] = NewParty;
  264.  
  265. Lounge.NumParties++;
  266.  
  267. cout << "Lounge have " << Lounge.NumEmptySeats << " seats left." << endl;
  268. }
  269. else {
  270. cout << "But Lounge only have " << Lounge.NumEmptySeats << " seats left. So, send the party away" << endl;
  271. delete[] NewParty.pName;
  272. }
  273. }
  274.  
  275. void AddPartyToPlaneOrLounge(Party NewParty) {
  276.  
  277. Plane & Plane = GetPlane(NewParty);
  278.  
  279. if ((Plane).NumEmptySeats >= NewParty.pAmt)//enough seats?
  280. {
  281.  
  282. AddPartyToPlane(NewParty);
  283.  
  284. if (Plane.NumEmptySeats == 0)
  285. {
  286. FlyUntilNotFull(Plane);
  287. }
  288.  
  289. }
  290. else //not enough empty seats, so...
  291. {
  292. 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
  293. {
  294. cout << "The Party is too big for " << PlaneNames[NewParty.WhichPlane] << ", so this party will be sent to the lounge." << endl;
  295. AddPartyToLounge(NewParty);
  296.  
  297. if (Plane.NumSeats == Lounge.NumSeats && Plane.NumSeats < NewParty.pAmt)
  298. {
  299. cout << "The Party is too big for " << PlaneNames[NewParty.WhichPlane] << ", so this party will be sent away." << endl;
  300. delete[] NewParty.pName;
  301. }
  302. }
  303. else { // if the plane was empty, there would be enough seats
  304. cout << PlaneNames[NewParty.WhichPlane] << " only has " << Plane.NumEmptySeats << " seats left, send party to Lounge. " << endl;
  305. AddPartyToLounge(NewParty);
  306. }
  307.  
  308. }
  309. }
  310.  
  311. void ShutDown()
  312. {
  313. while (Alfa.NumParties >= 1) // keep flying the plane until there are no more people
  314. FlyUntilNotFull(Alfa); // fly the plane even if not full
  315.  
  316. cout << endl;
  317.  
  318. while (Bravo.NumParties >= 1)
  319. FlyUntilNotFull(Bravo);
  320. //while (Lounge.Parties != 0)
  321. //AddLoungeToPlane(Plane);
  322. //I had an idea to create a function for this particular circumstance based on my FlyUntilNotFull function, but I kept getting really strange errors
  323.  
  324. delete[] Alfa.Parties;
  325. delete[] Bravo.Parties;
  326. delete[] Lounge.Parties;
  327.  
  328. cout << "The airline was shut down." << endl << endl;
  329.  
  330. }
  331.  
  332. void FlyUntilNotFull(Plane & Plane) {
  333.  
  334. FlyPlaneOnce(Plane);
  335.  
  336. for (int i = 0; i < Lounge.NumParties; i++) //people in lounge on plane
  337. {
  338. if (Lounge.Parties[i].WhichPlane == Plane.Name)
  339. {
  340. if (Plane.NumEmptySeats >= Lounge.Parties[i].pAmt) {
  341.  
  342. cout << "Moving party '" << Lounge.Parties[i].pName << "' from Lounge to " << PlaneNames[Plane.Name];
  343. cout << "£¬ which has " << Lounge.Parties[i].pAmt << " people." << endl;
  344.  
  345. AddPartyToPlane(Lounge.Parties[i]);
  346.  
  347. DeletePartyFromLounge(i); // delete Parties
  348.  
  349. i--;
  350.  
  351. if (Plane.NumEmptySeats == 0) { // if plane is full
  352. FlyPlaneOnce(Plane);
  353. i = -1;
  354. }
  355. }
  356. else {
  357. cout << "Party '" << Lounge.Parties[i].pName << "', which has " << Lounge.Parties[i].pAmt;
  358. cout << " party has too many to fit in the empty seats of " << PlaneNames[Plane.Name] << ", they stay at the lounge." << endl << endl;
  359. }
  360.  
  361. }
  362.  
  363. }
  364. cout << endl;
  365. }
  366.  
  367. Plane.h
  368.  
  369. #ifndef PLANE_H
  370. #define PLANE_H
  371.  
  372.  
  373. enum PlaneName {
  374. InvalidPlane = -1,
  375. PlaneAlfa,
  376. PlaneBravo,
  377. PlaneLounge,
  378.  
  379. NumPlanes
  380. };
  381.  
  382. struct Party
  383. {
  384. char * pName; //party name
  385. int pAmt; //how many in party
  386. PlaneName WhichPlane;
  387. };
  388.  
  389. struct Plane
  390. {
  391. int NumEmptySeats; //Number of empty seats
  392. int NumSeats; //Number of seats
  393. int NumParties;
  394. Party * Parties;
  395. PlaneName Name;
  396. };
  397.  
  398.  
  399. void InitialPlane(Plane &);
  400.  
  401. PlaneName GetPlaneName();
  402. void DisplayPlane(Plane);
  403. void FlyPlaneOnce(Plane &);
  404. void FlyUntilNotFull(Plane &);
  405.  
  406.  
  407. Plane & GetPlane(Party);
  408. void AddPartyToPlaneOrLounge(Party);
  409. void AddPartyToPlane(Party);
  410. void AddPartyToLounge(Party);
  411.  
  412. void ShutDown();
  413.  
  414. #endif
  415.  
  416. Commands.cpp
  417.  
  418. #include <iostream>
  419.  
  420. #include "Commands.h"
  421. #include "ReadWord.h"
  422.  
  423. using namespace std;
  424.  
  425. char * CmdWords[] = {
  426. "Alfa",
  427. "Bravo",
  428. "Lounge",
  429. "Arrive",
  430. "Fly",
  431. "Shutdown"
  432. };
  433.  
  434. Commands GetCommand()
  435. {
  436. char * Cmd;
  437. int i;
  438.  
  439. Cmd = ReadWord();
  440. for (i = 0; i < NumCmds; i++)
  441. 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
  442. {
  443. delete[] Cmd; //delete what the user enters for the next time
  444. return (Commands)i;
  445. }
  446. else;
  447. delete[] Cmd;
  448. return InvalidCmd;
  449. }
  450.  
  451. Commands.h
  452.  
  453. #ifndef COMMANDS_H
  454. #define COMMANDS_H
  455.  
  456. enum Commands {
  457. InvalidCmd = -1,
  458. CmdAlfa,
  459. CmdBravo,
  460. CmdLounge,
  461. CmdArrive,
  462. CmdFly,
  463. CmdShutdown,
  464.  
  465. NumCmds
  466. };
  467.  
  468. Commands GetCommand();
  469.  
  470. #endif
  471.  
  472. ReadWord.cpp
  473.  
  474. #include <iostream>
  475. #include <ctype.h>
  476. #include <memory.h>
  477.  
  478. #include "ReadWord.h"
  479.  
  480. using namespace std;
  481.  
  482. char * ReadWord()
  483. {
  484. const int FirstGuess(20);
  485. char c;
  486. char * pLine;
  487. char * pTemp;
  488. int NumCharsRead;
  489. int MaxCharsInLine;
  490.  
  491. NumCharsRead = 0;
  492. MaxCharsInLine = FirstGuess;
  493. pLine = new char[MaxCharsInLine + 1];
  494. SkipSpaces();
  495. while (!isspace(c = cin.get()))
  496. {
  497. pLine[NumCharsRead++] = c;
  498. if (NumCharsRead >= MaxCharsInLine)//if the line is full
  499. {
  500. MaxCharsInLine += FirstGuess;
  501. pTemp = new char[MaxCharsInLine + 1];
  502. memcpy(pTemp, pLine, NumCharsRead);
  503. delete[] pLine;
  504. pLine = pTemp;
  505. }
  506. else;
  507. }
  508. pLine[NumCharsRead] = '\0';
  509. return pLine;
  510. }
  511.  
  512. void SkipSpaces()
  513. {
  514. char c;
  515.  
  516. while (isspace(c = cin.get()));
  517. cin.putback(c);
  518. }
  519.  
  520. ReadWord.h
  521.  
  522. #ifndef READ_LINE_H
  523. #define READ_LINE_H
  524.  
  525. char * ReadWord();
  526. void SkipSpaces();
  527.  
  528. #endif
  529.  
  530. #pragma once
  531.  
  532. ReadInt.cpp
  533.  
  534. #include <iostream>
  535.  
  536. #include "ReadInt.h"
  537. #include "ReadWord.h"
  538.  
  539. using namespace std;
  540.  
  541. int ReadInt()
  542. {
  543. char c;
  544. bool Negative;
  545. int Num;
  546. int NumCharsEntered;
  547.  
  548. Negative = false;
  549. Num = 0;
  550. NumCharsEntered = 0;
  551. SkipSpaces();
  552. while (!isspace(c = cin.get()))
  553. {
  554. switch (c)
  555. {
  556. case '0':
  557. case '1':
  558. case '2':
  559. case '3':
  560. case '4':
  561. case '5':
  562. case '6':
  563. case '7':
  564. case '8':
  565. case '9':
  566. Num = Num * 10 + (c - '0');
  567. NumCharsEntered++;
  568. break;
  569. default:
  570. cout << '\a';
  571. }
  572. }
  573. return Num;
  574. }
  575.  
  576. ReadInt.h
  577.  
  578. #ifndef READ_INT_H
  579. #define READ_INT_H
  580.  
  581. int ReadInt();
  582.  
  583. #endif
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement