Advertisement
zaccooner

Main 4 Rocket Project

Nov 21st, 2014
203
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 7.84 KB | None | 0 0
  1. // Zac Cooner
  2.  
  3. #include <iostream>
  4. #include <fstream>
  5. #include <string>
  6. #include <sstream>
  7. #include <vector>
  8. #include "Box.h"
  9. #include "Rocket.h"
  10. #include "Queue.h"
  11.  
  12. using namespace std;
  13.  
  14.  
  15. double checkWeight(Queue * destPtr)
  16. {
  17. Box * travel;
  18. int i = 0;
  19. double total = 0;
  20. travel = (*destPtr).head;
  21. for (double weightCount = 0; i < 5; i++)
  22. {
  23. weightCount += travel->weight;
  24. total = weightCount;
  25. }
  26. return total;
  27. }
  28.  
  29. int checkBays(Queue * destPtr)
  30. {
  31. int boxCount = 0;
  32. Box* travel;
  33. travel = (*destPtr).head;
  34. for (int checker = 0; travel != NULL; travel = travel->next)
  35. {
  36. checker++;
  37. boxCount = checker;
  38. }// End for loop that increases checker per box in the list.
  39.  
  40. return boxCount; // Will return the number of boxes. If 5 or more, the rocket can be sent.
  41.  
  42. }// End checkBays function
  43.  
  44. void validate(int i, char c, string& s, double d, Queue* &list, int dayCount, Queue* marsPtr, Queue* issPtr, Queue* moonPtr)
  45. {
  46. if (i < 1) // If I is negative it is invalid
  47. {
  48. return;
  49. }
  50.  
  51. c = toupper(c); // Make the contents of 'c' uppercase
  52.  
  53. if (c != 'S' || c != 'L' || c != 'M') // If the char does not represent one of the destinations, it is invalid
  54. {
  55. return;
  56. }
  57.  
  58. if (d < 0) // If weight is negative, it is invalid
  59. {
  60. return;
  61. }
  62.  
  63. const char* new_s = s.c_str(); // s is now a c_string
  64. const char * cPtr = new_s; // Set the pointer to the c_string
  65.  
  66. ////////////////////////////////////////////////////////////////////
  67. //
  68. //This is where I need to check bays and send if necessary.
  69. ////////////////////////////////////////////////////////////////////
  70.  
  71. if (i != dayCount)
  72. {
  73. if (checkBays(marsPtr) >= 5) // Print list, delete list (x5)
  74. {
  75. double extra = 0;
  76. double total = 0;
  77. if (checkWeight(marsPtr) > 1000) // Check the weight
  78. {
  79. extra = checkWeight(marsPtr) - 1000;
  80. total = list[2].head->weight + list[2].head->next->weight + list[2].head->next->next->weight + list[2].head->next->next->next->weight
  81. + list[2].head->next->next->next->next->weight; // Add the weights of all the nodes
  82. total -= extra; // Subtract the extra. weight should = 1000.
  83.  
  84. Box * n = new Box(*(list[2].head->next->next->next->next), extra); // Create a new box with the same info as node 5, except weight equals the excess
  85. list[2].Enqueue(*n); // Put the new box in the end of the queue.
  86.  
  87.  
  88. cout << list[2].head << endl; //1
  89. list[2].Dequeue();
  90. cout << list[2].head << endl; //2
  91. list[2].Dequeue();
  92. cout << list[2].head << endl; // 3
  93. list[2].Dequeue();
  94. cout << list[2].head << endl; // 4
  95. list[2].Dequeue();
  96.  
  97.  
  98. list[2].head->weight -= extra; // Subtract the excess weight from the 5th node. At this point it is actually the head of the list.
  99. cout << list[2].head << endl; // 5
  100. list[2].Dequeue(); // Delete the first five nodes in the list
  101. } // End if where mars weight exceeds 1000
  102. else
  103. {
  104. cout << list[2].head << endl;
  105. list[2].Dequeue();
  106. cout << list[2].head << endl;
  107. list[2].Dequeue();
  108. cout << list[2].head << endl;
  109. list[2].Dequeue();
  110. cout << list[2].head << endl;
  111. list[2].Dequeue();
  112. cout << list[2].head << endl;
  113. list[2].Dequeue(); // Delete the first five nodes in the list
  114. }
  115. } // End check mars
  116.  
  117. else if (checkBays(moonPtr) >= 5) // Print list, delete list (x5)
  118. {
  119. double extra = 0;
  120. if (checkWeight(moonPtr) > 1000) // Check the weight
  121. {
  122. extra = checkWeight(moonPtr) - 1000;
  123.  
  124. Box * n = new Box(*(list[1].head->next->next->next->next), extra); // Create a new box with the same info as node 5, except weight equals the excess
  125. list[1].Enqueue(*n); // Put the new box in the end of the queue.
  126.  
  127.  
  128. cout << list[1].head << endl; //1
  129. list[1].Dequeue();
  130. cout << list[1].head << endl; //2
  131. list[1].Dequeue();
  132. cout << list[1].head << endl; // 3
  133. list[1].Dequeue();
  134. cout << list[1].head << endl; // 4
  135. list[1].Dequeue();
  136.  
  137.  
  138. list[1].head->weight -= extra; // Subtract the excess weight from the 5th node. At this point it is actually the head of the list.
  139. cout << list[1].head << endl; // 5
  140. list[1].Dequeue(); // Delete the first five nodes in the list
  141. } // end if lunar weight exceeds 1000
  142.  
  143. else
  144. {
  145. cout << list[1].head << endl;
  146. list[1].Dequeue();
  147. cout << list[1].head << endl;
  148. list[1].Dequeue();
  149. cout << list[1].head << endl;
  150. list[1].Dequeue();
  151. cout << list[1].head << endl;
  152. list[1].Dequeue();
  153. cout << list[1].head << endl;
  154. list[1].Dequeue(); // Delete the first five nodes in the list
  155. }// End else where weight does not exceed 100
  156.  
  157. } // End check Moon
  158.  
  159. else if (checkBays(issPtr) >= 5) // Print list, delete list (x5)
  160. {
  161. double extra = 0;
  162. if (checkWeight(issPtr) > 1000) // Check the weight
  163. { extra = checkWeight(issPtr) - 1000;
  164.  
  165. Box * n = new Box(*(list[0].head->next->next->next->next), extra); // Create a new box with the same info as node 5, except weight equals the excess
  166. list[0].Enqueue(*n); // Put the new box in the end of the queue.
  167.  
  168.  
  169. cout << list[0].head << endl; //1
  170. list[0].Dequeue();
  171. cout << list[0].head << endl; //2
  172. list[0].Dequeue();
  173. cout << list[0].head << endl; // 3
  174. list[0].Dequeue();
  175. cout << list[0].head << endl; // 4
  176. list[0].Dequeue();
  177.  
  178.  
  179. list[0].head->weight -= extra; // Subtract the excess weight from the 5th node. At this point it is actually the head of the list.
  180. cout << list[0].head << endl; // 5
  181. list[0].Dequeue(); // Delete the first five nodes in the list
  182. }
  183.  
  184. else
  185. {
  186. cout << list[0].head << endl;
  187. list[0].Dequeue();
  188. cout << list[0].head << endl;
  189. list[0].Dequeue();
  190. cout << list[0].head << endl;
  191. list[0].Dequeue();
  192. cout << list[0].head << endl;
  193. list[0].Dequeue();
  194. cout << list[0].head << endl;
  195. list[0].Dequeue(); // Delete the first five nodes in the list
  196. }
  197. } // End check ISS
  198.  
  199. }// End if where i != dayCount
  200.  
  201. Box *p = new Box(i, c, *cPtr, d);
  202.  
  203. if (c == 'S') // If destination is ISS, stack the ISS queue.
  204. {
  205. list[0].Enqueue(*p);
  206. }
  207. else if (c == 'L') //If destination is Moon, stack the Moon queue.
  208. {
  209. list[1].Enqueue(*p);
  210. }
  211. else if (c == 'M') //If destination is Mars, stack the Mars queue.
  212. {
  213. list[2].Enqueue(*p);
  214. }
  215. }
  216.  
  217.  
  218.  
  219. int main()
  220. {
  221. ifstream myfile("cargo.txt");
  222. string line;
  223. //Queue mars = *new Queue(&b);
  224. Queue *Destinations[3]; // 0 = ISS. 1 = Lunar. 2 = Mars.
  225. Queue *marsPtr = *(Destinations + 2); // Set the mars pointer to the third element
  226. Queue *moonPtr = *(Destinations + 1); // Set the moon pointer to the second element
  227. Queue *issPtr = *(Destinations); // Set the iss pointer to the first element
  228. bool countFound = false;
  229. int dayCount; // To keep track of the day.
  230.  
  231. // ifstream out("launch.txt", ios::out); // Declare the output file stream object.
  232.  
  233. if (myfile.is_open())
  234. {
  235. while (getline(myfile, line))
  236. {
  237. //validate each line as it comes
  238.  
  239. istringstream iss(line);
  240. do
  241. {
  242. int i; // To represent the day
  243. char c; // To represent the destination character
  244. string s; // To represent the cargo name
  245. double d; // To represent the weight of the cargo
  246. if ((iss >> i) && (iss >> c) && (iss >> s) && (iss >> d))
  247. {
  248. if (countFound == false)
  249. {
  250. dayCount = i; // Set count to the integer in the first succesful line process
  251. countFound = true; // countFound is true now.
  252. } // End if countFound is false.
  253.  
  254. validate(i, c, s, d, *Destinations, dayCount, marsPtr, issPtr, moonPtr);
  255. }// End if all stream data was sent to the proper variables
  256.  
  257. //else
  258. //{break;}
  259.  
  260. } while (iss);
  261. //send each cargo to its appropiate destination
  262. }// While the file can still get the line.
  263. myfile.close();
  264. }// end if file is open
  265.  
  266. }// End main
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement