Advertisement
Guest User

Untitled

a guest
Oct 22nd, 2019
121
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 7.35 KB | None | 0 0
  1. #include<iostream>
  2. #include<iomanip>
  3. #include<fstream>
  4. #include<string>
  5. #include<ctype.h>
  6. #include<set>
  7. #include<cmath>
  8. using namespace std;
  9. class Caller {
  10. public:
  11. float BoilT;// the specified boiling point
  12. float FreezeT; // the specified freezing point
  13. bool justHalfDegreeBoil=true; // Have I been further than 0.5C from boiling point?
  14. bool justHalfDegreeFreeze = true; // Have I been further than 0.5C from freezing point ?
  15. bool doNotIgnoreFlux; // Boolean expression for when to ignore fluctuations.
  16. // alert function for output of temperature in C & F
  17. void alert(float threshold, bool up, float tempCelsius , float Farh);
  18. string name;
  19. };
  20.  
  21. void Caller::alert(float threshold, bool up, float tempCelsius , float Farh) { // alert function definition.
  22. if (up)
  23. cout << " \n ALERT !!!!! Your threshold temp " << threshold
  24. << " has been reached going up, and the temperature is:" << tempCelsius << "C and " << Farh << "F";
  25. else
  26. cout << " \n ALERT !!!!! Your threshold temp " << threshold
  27. << " has been reached going up, and the temperature is:" << tempCelsius << "C and " << Farh << "F";
  28. }
  29.  
  30. class Sensor { // sensor class for reading the temperature and mimicing actual thermometer sensor
  31. public:
  32. float getNextSensorCelsius(); // function to read temperature from a file
  33. float getPreviousTemp(); // function to read and store previous temperature
  34. ifstream inputFile; // reads from file the next line
  35. float PreviousTemp; // previous temperature variable
  36. };
  37.  
  38. float Sensor::getNextSensorCelsius() // definition of function declaration for current temperature readings
  39. {
  40. float fromFile;
  41. if (inputFile >> fromFile) {
  42. PreviousTemp = fromFile;
  43. return fromFile;
  44. }
  45. else
  46. return -300;
  47. }
  48.  
  49. float Sensor::getPreviousTemp() // definition of function declaration for previous temperature readings
  50. {
  51. return PreviousTemp;
  52. }
  53.  
  54. class Thermometer { // thermometer class to mimic a real thermometer
  55. public:
  56. std::set<Caller*> callers;
  57. void registerCaller(Caller* c);
  58. Sensor* from;
  59. bool update();
  60. };
  61.  
  62. bool Thermometer::update()
  63. {
  64. float previous = from->getPreviousTemp();
  65. float Cel = from->getNextSensorCelsius();
  66. float Farh = (float)((1.8 * Cel) + 32.0);
  67. if (Cel == -300) // Below absolute zero used as marker value by sensor
  68. return false; // Stop
  69. // Iterate callers
  70. cout << "\nTemperature now " << Cel << " Celsius, " << Farh << " Farhenheit"; // output of regular thermometer readings
  71. bool tempGoingUp=Cel > previous;
  72. for (auto caller : callers)
  73. {
  74. if (caller->BoilT < Cel && caller->BoilT != -300)
  75. {
  76. if (caller->BoilT < previous)
  77. {
  78. // ignore
  79. }
  80. else
  81. {
  82. if (caller->justHalfDegreeBoil)
  83. {
  84. caller->alert(caller->BoilT, Cel, Farh, tempGoingUp);
  85. }
  86. else
  87. {
  88. if (caller->doNotIgnoreFlux)
  89. {
  90. caller->alert(caller->BoilT, Cel, Farh, tempGoingUp);
  91. }
  92. }
  93. }
  94. caller->justHalfDegreeBoil = false;
  95. }
  96. if (caller->BoilT > Cel&& caller->BoilT != -300)
  97. {
  98. if (caller->BoilT < previous)
  99. {
  100. float difference = caller->BoilT - Cel;
  101. if (abs(difference) > 0.5)
  102. {
  103. caller->justHalfDegreeBoil = true;
  104. }
  105. }
  106. }if (caller->FreezeT > Cel&& caller->FreezeT != -300)
  107. {
  108. if (caller->FreezeT > previous)
  109. {
  110. // ignore
  111. }
  112. else
  113. {
  114. if (caller->justHalfDegreeFreeze)
  115. {
  116. caller->alert(caller->FreezeT, Cel, Farh, tempGoingUp);
  117. }
  118. else
  119. {
  120. if (caller->doNotIgnoreFlux)
  121. {
  122. caller->alert(caller->FreezeT, Cel, Farh, tempGoingUp);
  123. }
  124. }
  125. caller->justHalfDegreeFreeze = false;
  126. }
  127. if (caller->FreezeT < Cel&& caller->FreezeT != -300)
  128. {
  129. if (caller->FreezeT > previous)
  130. {
  131. float difference = caller->FreezeT - Cel;
  132. if (abs(difference) > 0.5)
  133. {
  134. caller->justHalfDegreeFreeze = true;
  135. }
  136. }
  137. }
  138. }
  139.  
  140. }
  141. return true; // Found more data
  142. }
  143.  
  144. void Thermometer::registerCaller(Caller* c)
  145. {
  146. callers.insert(c);
  147. }
  148.  
  149. int main() {
  150. Sensor* s = new Sensor();
  151. s->inputFile.open("/Users/lwahonen/Downloads/data.csv"); //Opening the file
  152. Thermometer t;
  153. t.from = s;
  154. Caller* myCaller = new Caller();
  155. t.registerCaller(myCaller);
  156. cout << " Please specify the boiling point of substance. -300 means ignore all boiling: ";
  157. while (!(cin >> myCaller->BoilT))
  158. {
  159. cout << "error, not a temperature, please try again! \n ";
  160. cout << "Please specify the boiling point of substance. -300 means ignore all boiling: ";
  161. cin.clear();
  162. cin.ignore(123, '\n');
  163. }
  164. cout << " Please specify the freezing point of substance. -300 means ignore all freezing: ";
  165. while (!(cin >> myCaller->FreezeT))
  166. {
  167. cout << "error, not a temperature, please try again! \n ";
  168. cout << "Please specify the boiling point of substance. -300 means ignore all boiling: ";
  169. cin.clear();
  170. cin.ignore(123, '\n');
  171. }
  172. char AlertDecision;
  173. cout << " Do you want to be alerted only if the fluxuation is over 0.5C? (Y/N) ";
  174. bool goodInputs = false;
  175. while (!goodInputs)
  176. {
  177. cin >> AlertDecision;
  178. goodInputs = false;
  179. if (AlertDecision == 'Y' || AlertDecision == 'y') {
  180. goodInputs = true;
  181. myCaller->doNotIgnoreFlux = false;
  182. }
  183. if (AlertDecision == 'N' || AlertDecision == 'n') {
  184. myCaller->doNotIgnoreFlux = true;
  185. goodInputs = true;
  186. }
  187. if (goodInputs)
  188. break;
  189. cout << " Wrong input conditions, please try again. \n";
  190. cout << "Do you want to be alerted only if the fluxuation is over 0.5C? (Y/N) ";
  191. }
  192.  
  193. cout << " Do you want to be alerted only if the boiling point is reached as substance is heating up ? (Y/N) ";
  194. goodInputs = false;
  195. while (!goodInputs)
  196. {
  197. cin >> AlertDecision;
  198. if (AlertDecision == 'Y' || AlertDecision == 'y') {
  199. goodInputs = true;
  200. // ignore
  201. }
  202. if (AlertDecision == 'N' || AlertDecision == 'n') {
  203. // Boiling alarm coming down is just a fancy name for a normal freezing alarm. Let's create that.
  204. Caller* boilingGoindDown = new Caller();
  205. boilingGoindDown->FreezeT = myCaller->BoilT;
  206. boilingGoindDown->BoilT = -300;
  207. boilingGoindDown->doNotIgnoreFlux = myCaller->doNotIgnoreFlux;
  208. t.registerCaller(boilingGoindDown);
  209. goodInputs = true;
  210. }
  211. if (goodInputs)
  212. break;
  213. cout << " Wrong input conditions, please try again. \n";
  214. cout << "Do you want to be alerted only if the fluxuation is over 0.5C? (Y/N) ";
  215. }
  216. cout << " Do you want to be alerted only if the freezing point is reached as substance is cooling down ? (Y/N)";
  217. goodInputs = false;
  218. while (!goodInputs)
  219. {
  220. cin >> AlertDecision;
  221. if (AlertDecision == 'Y' || AlertDecision == 'y') {
  222. goodInputs = true;
  223. // ignore
  224. }
  225. if (AlertDecision == 'N' || AlertDecision == 'n') {
  226. // Freezing alarm going up is just a fancy name for a normal boiling alarm. Let's create that.
  227. Caller* freezingGoingUp = new Caller();
  228. freezingGoingUp->BoilT = myCaller->FreezeT;
  229. freezingGoingUp->FreezeT = -300;
  230. freezingGoingUp->doNotIgnoreFlux = myCaller->doNotIgnoreFlux;
  231. t.registerCaller(freezingGoingUp);
  232. goodInputs = true;
  233. }
  234. if (goodInputs)
  235. break;
  236. cout << " Wrong input conditions, please try again. \n";
  237. cout << "Do you want to be alerted only if the fluxuation is over 0.5C? (Y/N) ";
  238. }
  239. while (true)
  240. { // calls the sensor.read
  241. bool hadData = t.update();
  242. if (hadData == false)
  243. break;
  244. }
  245. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement