Skip to content

Instantly share code, notes, and snippets.

@kaldas
Created February 27, 2012 17:54
Show Gist options
  • Save kaldas/1925856 to your computer and use it in GitHub Desktop.
Save kaldas/1925856 to your computer and use it in GitHub Desktop.
p2_src.cpp
/*********************************************
P2, Instituto Infnet, Prof. Cleuton Sampaio
Alunos: Rodrigo Caldas de Moura Duarte
Thyago de Luca Silva Costa (d0j0 guy)
*********************************************/
/* PROFESSOR, ESTE É UM CODE DE QUEM FAZ DOJO!
ENTRADA:
2
1 2
20 1291320423
10 1291320423
2 1
132 1291320423 */
#include <iostream>
#include <ctime>
#include <vector>
#include <fstream>
class Medicao
{
public:
Medicao() { }
Medicao(int temp, time_t hora) : temp(temp), hora(hora) { }
void serializa(std::ostream& out) {
out << temp << " " << hora << std::endl;
}
void deserializa(std::istream& in) {
in >> temp >> hora;
}
int temp;
time_t hora;
};
class Caldeira
{
public:
Caldeira() { }
Caldeira(int id) : id(id) { }
void addMedicao(Medicao *medicao) { medicoes.push_back(medicao); }
int calcMedia() {
int soma = 0;
for(Medicoes::iterator it = medicoes.begin(); it != medicoes.end(); ++it)
soma += (*it)->temp;
if(medicoes.size() == 0)
return 0;
return (soma / medicoes.size());
}
void serializa(std::ostream& out) {
out << id << " " << medicoes.size() << std::endl;
for(Medicoes::iterator it = medicoes.begin(); it != medicoes.end(); ++it)
(*it)->serializa(out);
}
void deserializa(std::istream& in) {
int size;
in >> id >> size;
for(int i=0;i<size;++i) {
Medicao *medicao = new Medicao;
medicao->deserializa(in);
medicoes.push_back(medicao);
}
}
int id;
typedef std::vector<Medicao*> Medicoes;
Medicoes medicoes;
};
typedef std::vector<Caldeira*> Caldeiras;
Caldeiras g_caldeiras;
void serializa()
{
std::ofstream fout("saida");
int num = g_caldeiras.size();
fout << num << std::endl;
for(Caldeiras::iterator it = g_caldeiras.begin(); it != g_caldeiras.end(); ++it)
(*it)->serializa(fout);
}
void deserializa()
{
int num;
std::ifstream fin("entrada");
if(fin.good()) {
fin >> num;
for(int i=0;i<num;++i) {
Caldeira *caldeira = new Caldeira;
caldeira->deserializa(fin);
g_caldeiras.push_back(caldeira);
}
}
}
void testaSerializa()
{
Caldeira *caldeira = new Caldeira(1);
caldeira->addMedicao(new Medicao(30, std::time(NULL)));
caldeira->addMedicao(new Medicao(10, std::time(NULL)));
caldeira->addMedicao(new Medicao(20, std::time(NULL)));
g_caldeiras.push_back(caldeira);
caldeira = new Caldeira(2);
caldeira->addMedicao(new Medicao(100, std::time(NULL)));
caldeira->addMedicao(new Medicao(300, std::time(NULL)));
g_caldeiras.push_back(caldeira);
caldeira = new Caldeira(3);
caldeira->addMedicao(new Medicao(100, std::time(NULL)));
caldeira->addMedicao(new Medicao(300, std::time(NULL)));
g_caldeiras.push_back(caldeira);
serializa();
}
void testaDeserializa()
{
deserializa();
for(Caldeiras::iterator it = g_caldeiras.begin(); it != g_caldeiras.end(); ++it) {
int media = (*it)->calcMedia();
std::cout << "Caldeira: " << (*it)->id << " => Media: " << media << std::endl;
}
}
int main(int argc, char **argv)
{
testaSerializa();
g_caldeiras.clear();
testaDeserializa();
return 0;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment