Advertisement
zaccooner

Project 5

Nov 11th, 2014
155
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 4.21 KB | None | 0 0
  1. // Zac Cooner
  2.  
  3. #include <iostream>
  4. #include <fstream>
  5. #include <string>
  6. #include <sstream>
  7. #include <vector>
  8.  
  9.  
  10.  
  11. using namespace std;
  12.  
  13. class Box // Box represents the package to be sent off
  14. {
  15. public:
  16. int arrivalDay;
  17. char * cargo;
  18. char destination;
  19. double weight;
  20.  
  21. Box( int i, char c, const char dest, double d)//Overloaded constructor
  22. {
  23. arrivalDay =i;
  24. cargo = &c;
  25. destination = dest;
  26. weight = d;
  27. }
  28.  
  29. double operator+(Box b) // Binary plus operator
  30. {
  31. return this->weight + b.weight;//Return the added weights.
  32. }
  33.  
  34. ostream& operator<<(Box* &b)//The stuff starting from 'operator' are function parameters.
  35. {
  36. double w = b->weight;
  37.  
  38. stringstream os;
  39. char c=*cargo;
  40.  
  41. double gravity;
  42. if(c=='S') // If destination is ISS, gravity is 1.
  43. {
  44. gravity=1;
  45. }else if (c=='L') // If destination is Moon, gravity is .1655
  46. {
  47. gravity=.1655;//The gravity constants.
  48. }else // // If destination is Mars, gravity is .3895
  49. {
  50. gravity=.3895;
  51. }
  52. w=w*gravity; // Weight equals the weight times the gravity factor at destination
  53. os << cargo << " " << w <<endl; // Needs formatting. Will return the cargo name and the destination weight.
  54. return os;
  55. }
  56.  
  57. Box *next; // Next pointer
  58. };
  59.  
  60. class Rocket
  61. {
  62. Box bay[5]; // Each rocket has 5 bays that need to be filled before launch.
  63. int dest; // Will number the destinations using the notation within the list array. Ex, 0 = ISS, 1 = Moon, 2 = Mars.
  64.  
  65. void setDest(int i)
  66. {
  67. dest=i;
  68. }
  69.  
  70. double getWeight()
  71. {
  72. double d;
  73. for(int i=0;i<=4;i++)
  74. {
  75. d+=bay[i].weight; //Add weight to the overall weight of the Rocket.
  76. }
  77. return d;
  78. }// End getWeight()
  79.  
  80. ostream& operator<<(Rocket* &b)//Same idea as above, except using the << operator now. Send a pointer to a rocket by reference.
  81. {
  82. stringstream os;
  83. for(int i=0;i<=4;i++)
  84. {
  85. os<<&bay[i];
  86. }
  87. return os;
  88. }
  89.  
  90. };
  91.  
  92.  
  93. class Queue
  94. {
  95. public:
  96. bool setup=true;
  97. Box *head; // Head pointer
  98. Box *tails; // Tail pointer
  99.  
  100. void Enqueue(Box &b)
  101. {
  102. if(setup) // On the first go, set the head to the first box.
  103. {
  104. head=&b;
  105. head->next=tails;
  106. setup=false; // Turn off the bool so it doesn't set head again.
  107. }else{
  108. //add to last on the list
  109. tails->next = &b;
  110. tails = &b;
  111. }
  112. }
  113.  
  114. void Dequeue() //Delete first one on the list.
  115. {
  116. head = head->next;
  117. }
  118.  
  119. };
  120.  
  121. void validate(int i, char c, string& s, double d, Queue* &list)
  122. {
  123. if (i < 1)
  124. {return;}
  125.  
  126. c = toupper(c); // Make the contents of 'c' uppercase
  127.  
  128. if (c != 'S' || c != 'L' || c != 'M')
  129. {return;}
  130.  
  131. if (d < 0)
  132. {return;}
  133.  
  134. const char* new_s = s.c_str(); // s is now a c_string
  135. const char * cPtr = new_s; // Set the pointer to the c_string
  136. Box *p = new Box(i,c,*cPtr,d);
  137.  
  138. if(c=='S') // If destination is ISS, stack the ISS queue.
  139. {
  140. list[0].Enqueue(*p);
  141. }else if(c=='L') //If destination is Moon, stack the Moon queue.
  142. {
  143. list[1].Enqueue(*p);
  144. }else if(c=='M') //If destination is Mars, stack the Mars queue.
  145. {
  146. list[2].Enqueue(*p);
  147. }
  148. }
  149.  
  150. int main()
  151. {
  152. ifstream myfile ("cargo.txt");
  153. string line;
  154. //Queue mars = *new Queue(&b);
  155. Queue *Destinations[3]; // 0 = ISS. 1 = Lunar. 2 = Mars.
  156.  
  157. if (myfile.is_open())
  158. {
  159. while (getline(myfile,line))
  160. {
  161. //validate each line as it comes
  162.  
  163. istringstream iss(line);
  164. do
  165. {
  166. int i;
  167. char c;
  168. string s;
  169. double d;
  170. if((iss>>i)&&(iss>>c)&&(iss>>s)&&(iss>>d))
  171. {
  172. validate(i,c,s,d,*Destinations);
  173. }else{
  174. break;
  175. }
  176. }while(iss);
  177. //send each cargo to its appropiate destination
  178. }// While the file can still get the line.
  179. myfile.close();
  180. }// end if file is open
  181.  
  182. }// End main
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement