Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include<iostream>
- #include<iomanip>
- #include<fstream>
- #include<string>
- #include<ctype.h>
- #include<set>
- #include<cmath>
- using namespace std;
- class Caller {
- public:
- float BoilT;// the specified boiling point
- float FreezeT; // the specified freezing point
- bool justHalfDegreeBoil=true; // Have I been further than 0.5C from boiling point?
- bool justHalfDegreeFreeze = true; // Have I been further than 0.5C from freezing point ?
- bool doNotIgnoreFlux; // Boolean expression for when to ignore fluctuations.
- // alert function for output of temperature in C & F
- void alert(float threshold, bool up, float tempCelsius , float Farh);
- string name;
- };
- void Caller::alert(float threshold, bool up, float tempCelsius , float Farh) { // alert function definition.
- if (up)
- cout << " \n ALERT !!!!! Your threshold temp " << threshold
- << " has been reached going up, and the temperature is:" << tempCelsius << "C and " << Farh << "F";
- else
- cout << " \n ALERT !!!!! Your threshold temp " << threshold
- << " has been reached going up, and the temperature is:" << tempCelsius << "C and " << Farh << "F";
- }
- class Sensor { // sensor class for reading the temperature and mimicing actual thermometer sensor
- public:
- float getNextSensorCelsius(); // function to read temperature from a file
- float getPreviousTemp(); // function to read and store previous temperature
- ifstream inputFile; // reads from file the next line
- float PreviousTemp; // previous temperature variable
- };
- float Sensor::getNextSensorCelsius() // definition of function declaration for current temperature readings
- {
- float fromFile;
- if (inputFile >> fromFile) {
- PreviousTemp = fromFile;
- return fromFile;
- }
- else
- return -300;
- }
- float Sensor::getPreviousTemp() // definition of function declaration for previous temperature readings
- {
- return PreviousTemp;
- }
- class Thermometer { // thermometer class to mimic a real thermometer
- public:
- std::set<Caller*> callers;
- void registerCaller(Caller* c);
- Sensor* from;
- bool update();
- };
- bool Thermometer::update()
- {
- float previous = from->getPreviousTemp();
- float Cel = from->getNextSensorCelsius();
- float Farh = (float)((1.8 * Cel) + 32.0);
- if (Cel == -300) // Below absolute zero used as marker value by sensor
- return false; // Stop
- // Iterate callers
- cout << "\nTemperature now " << Cel << " Celsius, " << Farh << " Farhenheit"; // output of regular thermometer readings
- bool tempGoingUp=Cel > previous;
- for (auto caller : callers)
- {
- if (caller->BoilT < Cel && caller->BoilT != -300)
- {
- if (caller->BoilT < previous)
- {
- // ignore
- }
- else
- {
- if (caller->justHalfDegreeBoil)
- {
- caller->alert(caller->BoilT, Cel, Farh, tempGoingUp);
- }
- else
- {
- if (caller->doNotIgnoreFlux)
- {
- caller->alert(caller->BoilT, Cel, Farh, tempGoingUp);
- }
- }
- }
- caller->justHalfDegreeBoil = false;
- }
- if (caller->BoilT > Cel&& caller->BoilT != -300)
- {
- if (caller->BoilT < previous)
- {
- float difference = caller->BoilT - Cel;
- if (abs(difference) > 0.5)
- {
- caller->justHalfDegreeBoil = true;
- }
- }
- }if (caller->FreezeT > Cel&& caller->FreezeT != -300)
- {
- if (caller->FreezeT > previous)
- {
- // ignore
- }
- else
- {
- if (caller->justHalfDegreeFreeze)
- {
- caller->alert(caller->FreezeT, Cel, Farh, tempGoingUp);
- }
- else
- {
- if (caller->doNotIgnoreFlux)
- {
- caller->alert(caller->FreezeT, Cel, Farh, tempGoingUp);
- }
- }
- caller->justHalfDegreeFreeze = false;
- }
- if (caller->FreezeT < Cel&& caller->FreezeT != -300)
- {
- if (caller->FreezeT > previous)
- {
- float difference = caller->FreezeT - Cel;
- if (abs(difference) > 0.5)
- {
- caller->justHalfDegreeFreeze = true;
- }
- }
- }
- }
- }
- return true; // Found more data
- }
- void Thermometer::registerCaller(Caller* c)
- {
- callers.insert(c);
- }
- int main() {
- Sensor* s = new Sensor();
- s->inputFile.open("/Users/lwahonen/Downloads/data.csv"); //Opening the file
- Thermometer t;
- t.from = s;
- Caller* myCaller = new Caller();
- t.registerCaller(myCaller);
- cout << " Please specify the boiling point of substance. -300 means ignore all boiling: ";
- while (!(cin >> myCaller->BoilT))
- {
- cout << "error, not a temperature, please try again! \n ";
- cout << "Please specify the boiling point of substance. -300 means ignore all boiling: ";
- cin.clear();
- cin.ignore(123, '\n');
- }
- cout << " Please specify the freezing point of substance. -300 means ignore all freezing: ";
- while (!(cin >> myCaller->FreezeT))
- {
- cout << "error, not a temperature, please try again! \n ";
- cout << "Please specify the boiling point of substance. -300 means ignore all boiling: ";
- cin.clear();
- cin.ignore(123, '\n');
- }
- char AlertDecision;
- cout << " Do you want to be alerted only if the fluxuation is over 0.5C? (Y/N) ";
- bool goodInputs = false;
- while (!goodInputs)
- {
- cin >> AlertDecision;
- goodInputs = false;
- if (AlertDecision == 'Y' || AlertDecision == 'y') {
- goodInputs = true;
- myCaller->doNotIgnoreFlux = false;
- }
- if (AlertDecision == 'N' || AlertDecision == 'n') {
- myCaller->doNotIgnoreFlux = true;
- goodInputs = true;
- }
- if (goodInputs)
- break;
- cout << " Wrong input conditions, please try again. \n";
- cout << "Do you want to be alerted only if the fluxuation is over 0.5C? (Y/N) ";
- }
- cout << " Do you want to be alerted only if the boiling point is reached as substance is heating up ? (Y/N) ";
- goodInputs = false;
- while (!goodInputs)
- {
- cin >> AlertDecision;
- if (AlertDecision == 'Y' || AlertDecision == 'y') {
- goodInputs = true;
- // ignore
- }
- if (AlertDecision == 'N' || AlertDecision == 'n') {
- // Boiling alarm coming down is just a fancy name for a normal freezing alarm. Let's create that.
- Caller* boilingGoindDown = new Caller();
- boilingGoindDown->FreezeT = myCaller->BoilT;
- boilingGoindDown->BoilT = -300;
- boilingGoindDown->doNotIgnoreFlux = myCaller->doNotIgnoreFlux;
- t.registerCaller(boilingGoindDown);
- goodInputs = true;
- }
- if (goodInputs)
- break;
- cout << " Wrong input conditions, please try again. \n";
- cout << "Do you want to be alerted only if the fluxuation is over 0.5C? (Y/N) ";
- }
- cout << " Do you want to be alerted only if the freezing point is reached as substance is cooling down ? (Y/N)";
- goodInputs = false;
- while (!goodInputs)
- {
- cin >> AlertDecision;
- if (AlertDecision == 'Y' || AlertDecision == 'y') {
- goodInputs = true;
- // ignore
- }
- if (AlertDecision == 'N' || AlertDecision == 'n') {
- // Freezing alarm going up is just a fancy name for a normal boiling alarm. Let's create that.
- Caller* freezingGoingUp = new Caller();
- freezingGoingUp->BoilT = myCaller->FreezeT;
- freezingGoingUp->FreezeT = -300;
- freezingGoingUp->doNotIgnoreFlux = myCaller->doNotIgnoreFlux;
- t.registerCaller(freezingGoingUp);
- goodInputs = true;
- }
- if (goodInputs)
- break;
- cout << " Wrong input conditions, please try again. \n";
- cout << "Do you want to be alerted only if the fluxuation is over 0.5C? (Y/N) ";
- }
- while (true)
- { // calls the sensor.read
- bool hadData = t.update();
- if (hadData == false)
- break;
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement