Advertisement
Guest User

Untitled

a guest
Dec 15th, 2019
142
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 6.72 KB | None | 0 0
  1. /*
  2. * Name: YOUR NAME
  3. * Fires Project
  4. * Course: CSI108 (Fall 2019)
  5. * Date: December 8, 2019
  6. * Description: Put out various types of fires in the lab that
  7. * were caused by escaped engineered worms.
  8. */
  9.  
  10. #include <iostream>
  11. #include <string>
  12. #include <fstream>
  13. #include <cstdlib> // for random numbers
  14. #include <ctime> // for time
  15. using namespace std;
  16.  
  17. // Fire class definition.
  18. class Fire
  19. {
  20. public:
  21. // Store an identifier for the fire's "type" and
  22. // give the fire a random starting "level".
  23. Fire(const string &initType);
  24.  
  25. // Return identifier for type of fire.
  26. string getType() const;
  27.  
  28. // Return current level of fire.
  29. int getLevel() const;
  30.  
  31. // Apply some material to the fire.
  32. virtual void douseWith(const string &material) = 0;
  33.  
  34. protected:
  35. // Limit on initial level of fire.
  36. static const int MAX_START_LEVEL = 10;
  37.  
  38. string type; // identifier for type of fire
  39. int level; // level of the fire (higher # means worse)
  40. };
  41.  
  42. class SolidFire : public Fire
  43. {
  44. public:
  45. SolidFire(const string& initType)
  46. : Fire(initType) {};
  47. void douseWith(const string& material) override
  48. {
  49. if (material== "water")
  50. {
  51. level = level - 2;
  52. }
  53. else if (material == "foam")
  54. {
  55. level = level - 3;
  56. }
  57. else if (material == "powder")
  58. {
  59. level = level - 7;
  60. }
  61. else if (material == "CO2")
  62. {
  63. level = level;
  64. }
  65. }
  66.  
  67. };
  68. class LiquidFire : public Fire
  69. {
  70. public:
  71. LiquidFire(const string& initType)
  72. : Fire(initType) {};
  73. void douseWith(const string& material) override
  74. {
  75. if (material == "water")
  76. {
  77. level = level + 5;
  78. }
  79. else if (material == "foam")
  80. {
  81. level = level - 2;
  82. }
  83. else if (material == "powder")
  84. {
  85. level = level - 3;
  86. }
  87. else if (material == "CO2")
  88. {
  89. level = level -2;
  90. }
  91. }
  92. };
  93. class ElectricalFire : public Fire
  94. {
  95. public:
  96. ElectricalFire(const string& initType)
  97. : Fire(initType) {};
  98. void douseWith(const string& material) override
  99. {
  100. if (material == "water")
  101. {
  102. cout << "You receive a large electric shock" << endl;
  103. exit(0);
  104. }
  105. else if (material == "foam")
  106. {
  107. level = level + 2;
  108. }
  109. else if (material == "powder")
  110. {
  111. level = level - 3;
  112. }
  113. else if (material == "CO2")
  114. {
  115. level = level - 2;
  116. }
  117. }
  118. };
  119. class MetalFire : public Fire
  120. {
  121. public:
  122. MetalFire(const string& initType)
  123. : Fire(initType) {};
  124. void douseWith(const string& material) override
  125. {
  126. if (material == "water")
  127. {
  128. level = level + 7;
  129. }
  130. else if (material == "foam")
  131. {
  132. level = level + 3;
  133. }
  134. else if (material == "powder")
  135. {
  136. level = level - 2;
  137. }
  138. else if (material == "CO2")
  139. {
  140. level = level + 2;
  141. }
  142. }
  143. };
  144.  
  145. template<class T>
  146. int countNulls(T* list[], int length)
  147. {
  148. int counter = 0;
  149. for (int i = 0; i < length; i++)
  150. {
  151. if (list[i] = [NULL])
  152. counter++;
  153. }
  154. return counter;
  155. }
  156.  
  157. int main()
  158. {
  159. // Seed the random number generator to get different
  160. // sequences of numbers each time. When initially
  161. // testing, easier if always get same sequence.
  162. srand(static_cast<unsigned int>(time(NULL)));
  163.  
  164. // Limit number of fires to how many lab stations there are.
  165. const int MAX_FIRES = 8;
  166.  
  167. // Create array to store fire (if any) at each lab station.
  168. Fire *stations[MAX_FIRES] = { NULL }; // all NULL pointers
  169.  
  170. ifstream inStream("lab.txt");
  171. if (inStream.fail())
  172. {
  173. cout << "Cannot Open the file: Exiting program " << endl;
  174. exit(0);
  175. }
  176. while (inStream.peek != EOF)
  177. {
  178. string str;
  179. int i = 0;
  180. inStream >> str ;
  181. if (str == "solid")
  182. {
  183. SolidFire f(str);
  184. stations[i] = &f;
  185. }
  186. else if (str == "liquid")
  187. {
  188. LiquidFire f(str);
  189. stations[i] = &f;
  190. }
  191. else if (str == "electrical")
  192. {
  193. ElectricalFire f(str);
  194. stations[i] = &f;
  195. }
  196. else if (str == "metal")
  197. {
  198. MetalFire f(str);
  199. stations[i] = &f;
  200. }
  201. else if (str == "none")
  202. {
  203. *stations[i] = { NULL };
  204. }
  205. i++;
  206. }
  207. inStream.close();
  208. // Display how many fires were started.
  209. cout << "The worms have caused "
  210. << MAX_FIRES - countNulls(stations[MAX_FIRES] , MAX_FIRES)
  211. << " fires to start." << endl;
  212.  
  213. const string EXTINGUISHERS[] = { "water", "foam", "powder", "CO2" };
  214. // ONLY USE sizeof() WHERE DECLARE STATICALLY-ALLOCATED ARRAY.
  215. // IF USE PASSED ARRAY, YOU GET THE SIZE OF A POINTER.
  216. const int EXTINGUISHER_TYPES = sizeof(EXTINGUISHERS) / sizeof(EXTINGUISHERS[0]);
  217.  
  218. // Limit on fires above which fire department should be called.
  219. const int SAFE_TOTAL_FIRE_LEVEL = 50;
  220.  
  221. while (true)
  222. {
  223. // Display the type of fire (if any) and its level
  224. // at each station in the lab.
  225. // Total the level of all fires.
  226.  
  227. cout << "\nStations:" << endl;
  228.  
  229. int totalLevel = 0;
  230.  
  231. for (int i = 0; i < MAX_FIRES; i++)
  232. {
  233. cout << "#" << (i + 1) << ": ";
  234.  
  235. if (stations[i] == NULL) // no fire started?
  236. {
  237. cout << "no fire";
  238. }
  239. else
  240. {
  241. // Display the type of fire.
  242. cout << stations[i]->getType() << " fire ";
  243.  
  244. int level = stations[i]->getLevel();
  245.  
  246. if (level == 0) // is former fire now out?
  247. {
  248. cout << "is out";
  249. }
  250. else
  251. {
  252. // Display the level of fire.
  253. cout << "(level " << level << ")";
  254.  
  255. // Accumulate how much fire.
  256. totalLevel += level;
  257. }
  258. }
  259.  
  260. cout << endl;
  261. }
  262.  
  263. // Are all fires now at level zero (i.e., out)?
  264. if (totalLevel == 0)
  265. {
  266. cout << "All fires are out." << endl;
  267. break;
  268. }
  269.  
  270. // Even though don't want to expose worm experiments to public,
  271. // shouldn't attempt to take on dangerous fires.
  272. if (totalLevel > SAFE_TOTAL_FIRE_LEVEL)
  273. {
  274. cout << "Call the fire department!" << endl;
  275. break;
  276. }
  277.  
  278. // Randomly find some extinguisher material in lab.
  279. string material = EXTINGUISHERS[rand() % EXTINGUISHER_TYPES];
  280.  
  281. // Allow user to douse fire at one lab station with material.
  282. // Use enters station # using a 1-based index.
  283. cout << "\nYou look around and see some '" << material << "'" << endl;
  284. cout << "Throw at which station (0=don't use)? ";
  285. int stationNum;
  286. cin >> stationNum;
  287.  
  288. // User chose not to use material.
  289. if (stationNum == 0)
  290. continue;
  291.  
  292. // Ensure chosen lab station exists.
  293. if (stationNum < 0 || stationNum > MAX_FIRES)
  294. cout << "No such station #" << stationNum << endl;
  295.  
  296. }
  297.  
  298.  
  299. return 0;
  300. }
  301.  
  302. // Fire class member definitions.
  303.  
  304. // Store an identifier for the fire's "type" and
  305. // give the fire a random starting "level".
  306. Fire::Fire(const string &initType)
  307. : type(initType)
  308. {
  309. // Fire level in range 1...MAX_START_LEVEL (inclusive).
  310. level = 1 + rand() % MAX_START_LEVEL;
  311. }
  312.  
  313. // Return identifier for type of fire.
  314. string Fire::getType() const
  315. {
  316. return type;
  317. }
  318.  
  319. // Return current level of fire.
  320. int Fire::getLevel() const
  321. {
  322. return level;
  323. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement