1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84
|
#include <iostream>
#include <fstream>
#include <limits>
#include <string>
#include <sstream>
#include <stdlib.h>
#include <algorithm>
std::istream& ignoreline(std::ifstream& in, std::ifstream::pos_type& pos)
{
pos = in.tellg();
return in.ignore(std::numeric_limits<std::streamsize>::max(), '\n');
}
std::string getLastLine(std::ifstream& in)
{
std::ifstream::pos_type pos = in.tellg();
std::ifstream::pos_type lastPos;
while (in >> std::ws && ignoreline(in, lastPos))
pos = lastPos;
in.clear();
in.seekg(pos);
std::string line;
std::getline(in, line);
return line;
}
//converter para float
float to_float(const std::string& str)
{
std::istringstream is(str) ;
float result ;
is >> result ;
return result ;
}
int main()
{
std::string date;
std::string time;
std::string t;
std::ifstream file("temp.txt");
if (file)
{
std::string line = getLastLine(file);
std::istringstream iss(line);
getline(iss, date, ' ');
getline(iss, time, '\t');
getline(iss, t);
}
else {
std::cout << "Error read temp.txt\n";
return 2; }
//converte temperatura para float
std::replace(t.begin(), t.end(), ',', '.');
float temp = to_float(t) ;
if (temp >= 27)
{
std::cout << "Temperature High: " << temp << " C - " << date << " " << time;
return 2;
}
if (temp <= 10)
{
std::cout << "Temperature Low: " << temp << " C - " << date << " " << time;
return 2;
}
if (temp >10 && temp <27)
{
std::cout << "Temp Backup: " << temp << " C - " << date << " " << time;
return 0;
}
else
{
std::cout << "Error Read Temp";
return 2;
}
}
|