From 7b1963d820a867bb4bafaecfb4e8a0a085ae3760 Mon Sep 17 00:00:00 2001 From: andre Date: Wed, 15 Mar 2017 16:56:19 -0300 Subject: [PATCH] Exercicio de treinamento cpp 15/03 --- intercept | 21 +++++++++++ src/Calculo.cpp | 2 +- src/Calculo.h | 2 +- src/Fibonacci.cpp | 24 +++++++++---- src/Fibonacci.h | 4 +-- src/Goulomb.cpp | 82 ++++++++++++++++++++++++++++++++++++++++++ src/Goulomb.h | 79 ++++++++++++++++++++++++++++++++++++++++ src/Interceptador.cpp | 2 +- src/Interceptador.h | 2 +- src/Main.cpp | 28 ++++++++++++--- src/Primos.cpp | 8 ++--- src/Primos.h | 4 +-- src/SalvaCalculo.cpp | 4 +++ test/TestFibonacci.cpp | 2 +- test/TestGoulomb.cpp | 80 +++++++++++++++++++++++++++++++++++++++++ 15 files changed, 319 insertions(+), 25 deletions(-) create mode 100644 intercept create mode 100644 src/Goulomb.cpp create mode 100644 src/Goulomb.h create mode 100644 test/TestGoulomb.cpp diff --git a/intercept b/intercept new file mode 100644 index 0000000..e16562b --- /dev/null +++ b/intercept @@ -0,0 +1,21 @@ +Calculo: Goulomb +0 1 +1 2 +2 2 +3 3 +4 3 +5 3 +6 4 +7 4 +8 4 +9 5 +10 1 +11 2 +12 2 +13 3 +14 3 +15 3 +16 4 +17 4 +18 4 +19 5 diff --git a/src/Calculo.cpp b/src/Calculo.cpp index 72f2a54..059c3f9 100644 --- a/src/Calculo.cpp +++ b/src/Calculo.cpp @@ -31,7 +31,7 @@ unsigned int Calculo::numeroResultados(){ return 0; } -int Calculo::resultado(unsigned int indice){ +unsigned long Calculo::resultado(unsigned int indice){ // TODO: Implementar return 0; } diff --git a/src/Calculo.h b/src/Calculo.h index 9fb2b5b..97e33e8 100644 --- a/src/Calculo.h +++ b/src/Calculo.h @@ -46,7 +46,7 @@ class Calculo { * @param indice * @return */ - virtual int resultado(unsigned int indice); + virtual unsigned long resultado(unsigned int indice); /** * Nome do cálculo diff --git a/src/Fibonacci.cpp b/src/Fibonacci.cpp index ea02f3d..f95cbf4 100644 --- a/src/Fibonacci.cpp +++ b/src/Fibonacci.cpp @@ -7,24 +7,30 @@ #include "Fibonacci.h" #include +#include +#include Fibonacci::Fibonacci(int inicio, unsigned int tamanho, Interceptador *interceptador): Calculo(inicio, tamanho, interceptador) { this->resultados.reserve(tamanho); } void Fibonacci::calcula(){ - int nr = this->inicio; - int last = nr; + unsigned long nr = this->inicio; + unsigned long last = nr; + this->resultados.push_back(nr); while(this->resultados.size() < this->tamanho){ if(last == nr){ nr = nr + 1; }else{ - int lst = last; + unsigned long long lst = last; last = nr; nr = nr + lst; + + } this->resultados.push_back(nr); + } } @@ -36,20 +42,24 @@ string Fibonacci::nome() const{ return "Fibonacci"; } -int Fibonacci::resultado(unsigned int indice){ - int rtn = 0; +unsigned long Fibonacci::resultado(unsigned int indice){ + unsigned long rtn = 0; if(indice + 1 <= this->resultados.size()) { rtn = this->resultados.at(indice); + } + //printf("\n index %d result %d\n", indice, rtn); return this->interceptador->intercepta(rtn); + } string Fibonacci::toString(char sep){ stringstream ss; - unsigned int i = 0; - for(vector::iterator it = this->resultados.begin(); it != this->resultados.end(); it++){ + unsigned long i = 0; + for(vector::iterator it = this->resultados.begin(); it != this->resultados.end(); it++){ ss << *it; if(i < (this->resultados.size() - 1)){ + ss << sep; } i++; diff --git a/src/Fibonacci.h b/src/Fibonacci.h index 3ed59c2..7be4b0e 100644 --- a/src/Fibonacci.h +++ b/src/Fibonacci.h @@ -47,7 +47,7 @@ class Fibonacci: public Calculo { * @param indice * @return */ - virtual int resultado(unsigned int indice); + virtual unsigned long resultado(unsigned int indice); /** * Retorna o nome do calculo @@ -71,7 +71,7 @@ class Fibonacci: public Calculo { /** * Lista de resultados */ - vector resultados; + vector resultados; }; diff --git a/src/Goulomb.cpp b/src/Goulomb.cpp new file mode 100644 index 0000000..a872d77 --- /dev/null +++ b/src/Goulomb.cpp @@ -0,0 +1,82 @@ +/* + * File: Goulomb.cpp + * Author: Diogo Dias + * + * Created on 14 de Marco de 2017, 09:50 + */ + +#include "Goulomb.h" +#include + +Goulomb::Goulomb(int inicio, unsigned int tamanho, Interceptador *interceptador): Calculo(inicio, tamanho, interceptador) { + this->resultados.reserve(tamanho); +} + +void Goulomb::calcula(){ + int nr = this->inicio; + //int last = nr; + unsigned int iter = 0; + if(nr == 0){ + this->resultados.push_back(1); + iter = 1; + //resultados[iter-1] = nr; + }else{ + this->resultados.push_back(nr); + iter = nr; + //resultados[iter-1] = nr; + } +// iter = 1; + while(iter < this->tamanho){ + + if(iter != 0 && iter != 1){ + + // resultados[iter] = 1; + // }else{ + resultados[iter] = 1 + resultados[iter - resultados[resultados[iter-1]]]; + } + if(iter == 1){ + resultados[iter] = 1; + } + nr = resultados[iter]; + ++iter; + this->resultados.push_back(nr); + } +} + +unsigned int Goulomb::numeroResultados(){ + return this->resultados.size(); +} + +string Goulomb::nome() const{ + return "Goulomb"; +} + +unsigned long Goulomb::resultado(unsigned int indice){ + unsigned long rtn = 0; + if(indice + 1 <= this->resultados.size()) { + rtn = this->resultados.at(indice); + } + return this->interceptador->intercepta(rtn); +} + +string Goulomb::toString(char sep){ + stringstream ss; + unsigned int i = 0; + for(vector::iterator it = this->resultados.begin(); it != this->resultados.end(); it++){ + ss << *it; + if(i < (this->resultados.size() - 1)){ + ss << sep; + } + i++; + } + return ss.str(); +} + +void Goulomb::limpaCalculo() { + this->resultados.clear(); +} + +Goulomb::~Goulomb() { + this->limpaCalculo(); +} + diff --git a/src/Goulomb.h b/src/Goulomb.h new file mode 100644 index 0000000..52c2366 --- /dev/null +++ b/src/Goulomb.h @@ -0,0 +1,79 @@ +/* + * File: Goulomb.h + * Author: Diogo Dias + * + * Created on 14 de Marco de 2017, 09:40 + */ + +#ifndef GOULOMB_H +#define GOULOMB_H + +#include +#include "Calculo.h" + +using namespace std; + +/** + * Implementa o calculo de Fibonacci. + */ +class Goulomb: public Calculo { + +public: + /** + * Constructor + * @param inicio Inicio do calculo + * @params tamanho Numero de resultados para calcular + */ + Goulomb(int inicio = 0, unsigned int tamanho = 10, Interceptador *interceptador = 0); + + /** + * Calcula + */ + void calcula(); + + /** + * Resultados + * @return + */ + virtual unsigned int numeroResultados(); + + /** + * Limpa o calculo + */ + virtual void limpaCalculo(); + + /** + * Retorna o resultado em uma determinada posição + * @param indice + * @return + */ + virtual unsigned long resultado(unsigned int indice); + + /** + * Retorna o nome do calculo + * @return + */ + virtual string nome() const; + + /** + * Transforma o resultado em string. + * @param sep Separador + * @return + */ + virtual string toString(char sep); + + /** + * Destructor + */ + virtual ~Goulomb(); +private: + + /** + * Lista de resultados + */ + vector resultados; + +}; + +#endif /* GOULOMB_H */ + diff --git a/src/Interceptador.cpp b/src/Interceptador.cpp index d2fcbc6..8220786 100644 --- a/src/Interceptador.cpp +++ b/src/Interceptador.cpp @@ -17,7 +17,7 @@ Interceptador::Interceptador() { // } -int Interceptador::intercepta(int i){ +unsigned long Interceptador::intercepta(unsigned long i){ return i; } diff --git a/src/Interceptador.h b/src/Interceptador.h index 0f5b89d..d2fb4a1 100644 --- a/src/Interceptador.h +++ b/src/Interceptador.h @@ -30,7 +30,7 @@ class Interceptador { * @param i * @return */ - virtual int intercepta(int i); + virtual unsigned long intercepta(unsigned long i); /** * Destrutor diff --git a/src/Main.cpp b/src/Main.cpp index f916175..115196c 100644 --- a/src/Main.cpp +++ b/src/Main.cpp @@ -10,8 +10,11 @@ #include "Calculo.h" #include "Fibonacci.h" #include "Primos.h" +#include "Goulomb.h" #include "SalvaCalculo.h" +#include "Interceptador.h" #include +#include #ifdef WITH_UNIT_TEST #include @@ -33,11 +36,14 @@ void imprimeCalculo(Calculo *calculo) { printf("%s\n", calculo->nome().c_str()); printf("%s\t\t%s\n", "Indice", "Valor"); for (unsigned int i = 0; i < calculo->numeroResultados(); i++) { - printf("%d\t\t%d\n", i, calculo->resultado(i)); + + printf("%d\t\t%lu\n", i, calculo->resultado(i)); + } + } -/* +/*; * Este é o ponto de início da aplicação. * @param argc Número de argumentos passados * @param argv Vetor com os parâmetros @@ -48,29 +54,41 @@ int main(int argc, char** argv) { printf("%s", "Ex: ./calculo 1 100 fibonacci ./teste_fibonacci\n"); return EXIT_SUCCESS; } - - int inicio = atoi(argv[1]); - int tamanho = atoi(argv[2]); + int intercept_output = 0; + unsigned int inicio = atoi(argv[1]); + unsigned int tamanho = atoi(argv[2]); map calculos; calculos.insert(pair("fibonacci", new Fibonacci(inicio, tamanho))); calculos.insert(pair("primos", new Primos(inicio, tamanho))); + calculos.insert(pair("goulomb", new Goulomb(inicio, tamanho))); + //calculos. + Interceptador *interceptator = new Interceptador(); + intercept_output = interceptator->intercepta(tamanho); + printf("tamanho %d", intercept_output); + // Retorna sucesso if (argc > 3) { if (calculos.count(argv[3]) > 0) { Calculo *calculo = calculos.at(argv[3]); calculo->calcula(); +// printf("map b4 %lu\n",calculo->resultado(48)); + if (argc > 4) { SalvaCalculo *sc = new SalvaCalculo(calculo); sc->salva(argv[4]); } else { imprimeCalculo(calculo); + + } } } else { for (map::iterator it = calculos.begin(); it != calculos.end(); it++) { + imprimeCalculo(it->second); + } } diff --git a/src/Primos.cpp b/src/Primos.cpp index d784f73..775a9dc 100644 --- a/src/Primos.cpp +++ b/src/Primos.cpp @@ -2,12 +2,12 @@ #include #include -Primos::Primos(int inicio, unsigned int tamanho, Interceptador *interceptador) : Calculo(inicio, tamanho, interceptador) { +Primos::Primos(unsigned long long inicio, unsigned int tamanho, Interceptador *interceptador) : Calculo(inicio, tamanho, interceptador) { this->resultados.reserve(tamanho); } void Primos::calcula() { - int ini = this->inicio; + unsigned long long ini = this->inicio; if (ini == 0 || ini == 1) { ini = 3; this->resultados.push_back(2); @@ -29,8 +29,8 @@ unsigned int Primos::numeroResultados() { return this->resultados.size(); } -int Primos::resultado(unsigned int indice) { - int rtn = 0; +unsigned long Primos::resultado(unsigned int indice) { + unsigned int rtn = 0; if(indice + 1 <= this->resultados.size()) { rtn = this->resultados.at(indice); } diff --git a/src/Primos.h b/src/Primos.h index 40d5f4c..0eb95c5 100644 --- a/src/Primos.h +++ b/src/Primos.h @@ -15,11 +15,11 @@ class Primos : public Calculo { public: - Primos(int inicio = 0, unsigned int tamanho = 0, Interceptador *interceptador = 0); + Primos(unsigned long long inicio = 0, unsigned int tamanho = 0, Interceptador *interceptador = 0); virtual unsigned int numeroResultados(); - virtual int resultado(unsigned int indice); + virtual unsigned long resultado(unsigned int indice); virtual void limpaCalculo(); diff --git a/src/SalvaCalculo.cpp b/src/SalvaCalculo.cpp index 2e08b5d..aec8127 100644 --- a/src/SalvaCalculo.cpp +++ b/src/SalvaCalculo.cpp @@ -7,6 +7,8 @@ #include "SalvaCalculo.h" #include +#include +#include SalvaCalculo::SalvaCalculo(Calculo *calculo) { // Cria a instancia baseado no template @@ -19,7 +21,9 @@ void SalvaCalculo::salva(string filePath){ this->calculo->calcula(); for(unsigned int i = 0; i < this->calculo->numeroResultados(); i++){ ofs << i << "\t\t" << this->calculo->resultado(i) << endl; + } + //printf("\n salvacalc %lld\n ",this->calculo->resultado(47)); } SalvaCalculo::~SalvaCalculo() { diff --git a/test/TestFibonacci.cpp b/test/TestFibonacci.cpp index 47de485..01a68c1 100644 --- a/test/TestFibonacci.cpp +++ b/test/TestFibonacci.cpp @@ -76,4 +76,4 @@ TEST_F(TestFibonacciInterceptador, TesteDeResultadoZero) { EXPECT_TRUE(fibonacci->resultado(3) == 15); EXPECT_FALSE(fibonacci->resultado(4) == 99); EXPECT_TRUE(fibonacci->resultado(6) == 44); -} \ No newline at end of file +} diff --git a/test/TestGoulomb.cpp b/test/TestGoulomb.cpp new file mode 100644 index 0000000..a3a9d09 --- /dev/null +++ b/test/TestGoulomb.cpp @@ -0,0 +1,80 @@ +#include +#include +#include +#include +#include +#include "Goulomb.h" + +class TestGoulomb : public ::testing::Test { + +protected: + + Calculo* goulomb; + + virtual void SetUp( ) { + goulomb = new Goulomb(0, 10); + goulomb->calcula(); + } + + virtual void TearDown( ) { + goulomb->limpaCalculo(); + delete goulomb; + } + +}; + +class TestGoulombInterceptador : public ::testing::Test { + +protected: + Calculo* goulomb; + MockRepository *mocks; + Interceptador *interceptador; + + virtual void SetUp( ) { + mocks = new MockRepository(); + interceptador = mocks->Mock(); + goulomb = new Goulomb(0, 10, interceptador); + goulomb->calcula(); + } + + virtual void TearDown( ) { + mocks->OnCallDestructor(interceptador); + delete goulomb; + } + +}; + +TEST_F(TestGoulomb, TesteDeTamanho){ + EXPECT_TRUE(goulomb->numeroResultados() == 10); +} + +TEST_F(TestGoulomb, TesteDeResultado){ + EXPECT_TRUE(goulomb->resultado(0) == 1); + EXPECT_TRUE(goulomb->resultado(1) == 1); + EXPECT_TRUE(goulomb->resultado(2) == 2); + EXPECT_TRUE(goulomb->resultado(3) == 2); + EXPECT_TRUE(goulomb->resultado(4) == 3); + EXPECT_TRUE(goulomb->resultado(5) == 3); + EXPECT_TRUE(goulomb->resultado(6) == 4); + EXPECT_TRUE(goulomb->resultado(7) == 4); + EXPECT_TRUE(goulomb->resultado(8) == 4); + EXPECT_TRUE(goulomb->resultado(9) == 5); + EXPECT_FALSE(goulomb->resultado(9) == 6); +} + +TEST_F(TestGoulomb, TesteDeNome) { + EXPECT_STREQ(goulomb->nome().c_str(), "Goulomb"); +} + +TEST_F(TestGoulomb, TesteToString) { + EXPECT_STREQ(goulomb->toString(',').c_str(), "1,1,2,2,3,3,4,4,4,5"); + EXPECT_STREQ(goulomb->toString(';').c_str(), "1;1;2;2;3;3;4;4;4;5"); +} + +TEST_F(TestGoulombInterceptador, TesteDeResultadoZero) { + mocks->ExpectCall(this->interceptador, Interceptador::intercepta).With(2).Return(2); + //EXPECT_FALSE(goulomb->resultado(2) == 99); + EXPECT_TRUE(goulomb->resultado(2) == 2); +// printf("expect goulomb1 %d", goulomb->resultado(2)); + +}