Advertisement
Guest User

Untitled

a guest
Oct 22nd, 2019
146
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 7.40 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, float tempCelsius , float Farh, bool up);
  18. string name;
  19. };
  20.  
  21. void Caller::alert(float threshold, float tempCelsius , float Farh, bool up) { // 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 down, 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. std::cout.setf(std::ios::unitbuf);
  151. Sensor* s = new Sensor();
  152. s->inputFile.open("/Users/lwahonen/Downloads/data.csv"); //Opening the file
  153. Thermometer t;
  154. t.from = s;
  155. Caller* myCaller = new Caller();
  156. t.registerCaller(myCaller);
  157. cout << " Please specify the boiling point of substance. -300 means ignore all boiling: ";
  158. while (!(cin >> myCaller->BoilT))
  159. {
  160. cout << "error, not a temperature, please try again! \n ";
  161. cout << "Please specify the boiling point of substance. -300 means ignore all boiling: ";
  162. cin.clear();
  163. cin.ignore(123, '\n');
  164. }
  165. cout << " Please specify the freezing point of substance. -300 means ignore all freezing: ";
  166. while (!(cin >> myCaller->FreezeT))
  167. {
  168. cout << "error, not a temperature, please try again! \n ";
  169. cout << "Please specify the boiling point of substance. -300 means ignore all boiling: ";
  170. cin.clear();
  171. cin.ignore(123, '\n');
  172. }
  173. char AlertDecision;
  174. cout << " Do you want to be alerted only if the fluxuation is over 0.5C? (Y/N) ";
  175. bool goodInputs = false;
  176. while (!goodInputs)
  177. {
  178. cin >> AlertDecision;
  179. goodInputs = false;
  180. if (AlertDecision == 'Y' || AlertDecision == 'y') {
  181. goodInputs = true;
  182. myCaller->doNotIgnoreFlux = false;
  183. }
  184. if (AlertDecision == 'N' || AlertDecision == 'n') {
  185. myCaller->doNotIgnoreFlux = true;
  186. goodInputs = true;
  187. }
  188. if (goodInputs)
  189. break;
  190. cout << " Wrong input conditions, please try again. \n";
  191. cout << "Do you want to be alerted only if the fluxuation is over 0.5C? (Y/N) ";
  192. }
  193.  
  194. cout << " Do you want to be alerted only if the boiling point is reached as substance is heating up ? (Y/N) ";
  195. goodInputs = false;
  196. while (!goodInputs)
  197. {
  198. cin >> AlertDecision;
  199. if (AlertDecision == 'Y' || AlertDecision == 'y') {
  200. goodInputs = true;
  201. // ignore
  202. }
  203. if (AlertDecision == 'N' || AlertDecision == 'n') {
  204. // Boiling alarm coming down is just a fancy name for a normal freezing alarm. Let's create that.
  205. Caller* boilingGoindDown = new Caller();
  206. boilingGoindDown->FreezeT = myCaller->BoilT;
  207. boilingGoindDown->BoilT = -300;
  208. boilingGoindDown->doNotIgnoreFlux = myCaller->doNotIgnoreFlux;
  209. t.registerCaller(boilingGoindDown);
  210. goodInputs = true;
  211. }
  212. if (goodInputs)
  213. break;
  214. cout << " Wrong input conditions, please try again. \n";
  215. cout << "Do you want to be alerted only if the fluxuation is over 0.5C? (Y/N) ";
  216. }
  217. cout << " Do you want to be alerted only if the freezing point is reached as substance is cooling down ? (Y/N)";
  218. goodInputs = false;
  219. while (!goodInputs)
  220. {
  221. cin >> AlertDecision;
  222. if (AlertDecision == 'Y' || AlertDecision == 'y') {
  223. goodInputs = true;
  224. // ignore
  225. }
  226. if (AlertDecision == 'N' || AlertDecision == 'n') {
  227. // Freezing alarm going up is just a fancy name for a normal boiling alarm. Let's create that.
  228. Caller* freezingGoingUp = new Caller();
  229. freezingGoingUp->BoilT = myCaller->FreezeT;
  230. freezingGoingUp->FreezeT = -300;
  231. freezingGoingUp->doNotIgnoreFlux = myCaller->doNotIgnoreFlux;
  232. t.registerCaller(freezingGoingUp);
  233. goodInputs = true;
  234. }
  235. if (goodInputs)
  236. break;
  237. cout << " Wrong input conditions, please try again. \n";
  238. cout << "Do you want to be alerted only if the fluxuation is over 0.5C? (Y/N) ";
  239. }
  240. while (true)
  241. { // calls the sensor.read
  242. bool hadData = t.update();
  243. if (hadData == false)
  244. break;
  245. }
  246. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement