diff --git a/exercice_2/src/dumper_series.cc b/exercice_2/src/dumper_series.cc index f3c6e88..687ce69 100644 --- a/exercice_2/src/dumper_series.cc +++ b/exercice_2/src/dumper_series.cc @@ -1,5 +1,9 @@ #include "dumper_series.hh" DumperSeries::DumperSeries(Series &series) : series(series) {} +void DumperSeries::setPrecision(unsigned int precision) { + this->precision = precision; +} + DumperSeries::~DumperSeries() = default; diff --git a/exercice_2/src/dumper_series.hh b/exercice_2/src/dumper_series.hh index f7f8203..1c7d270 100644 --- a/exercice_2/src/dumper_series.hh +++ b/exercice_2/src/dumper_series.hh @@ -1,18 +1,26 @@ #ifndef DUMPER_SERIES_HH #define DUMPER_SERIES_HH #include "series.hh" +#include class DumperSeries { public: DumperSeries(Series &series); virtual ~DumperSeries(); public: - virtual void dump() = 0; + virtual void dump(std::ostream &os) = 0; + virtual void setPrecision(unsigned int precision); protected: Series &series; + unsigned int precision; }; +inline std::ostream &operator<<(std::ostream &stream, DumperSeries &_this) { + _this.dump(stream); + return stream; +} + #endif diff --git a/exercice_2/src/main.cc b/exercice_2/src/main.cc index 8f9961d..9a8d025 100644 --- a/exercice_2/src/main.cc +++ b/exercice_2/src/main.cc @@ -1,60 +1,62 @@ /* created by Marti Bosch and Marc Schwaerzel*/ /* -------------------------------------------------------------------------- */ #include #include +#include #include #include #include "compute_arithmetic.hh" #include "compute_pi.hh" #include "print_series.hh" #include "series.hh" #include "write_series.hh" using namespace std; /* -------------------------------------------------------------------------- */ int main(int argc, char **argv) { if (argc != 5) { cout << "Arguments: frequency, maxiter, series_type (arithmetic, pi), " "dump_type (print, write)" << endl; return EXIT_FAILURE; } std::stringstream sstr; for (int i = 1; i < argc; ++i) sstr << argv[i] << " "; int frequency; int maxiter; std::string series_type; std::string dump_type; sstr >> frequency; sstr >> maxiter; sstr >> series_type; sstr >> dump_type; Series *series; if (series_type == "arithmetic") series = new ComputeArithmetic(); else if (series_type == "pi") series = new ComputePi(); else { std::cout << "input error" << std::endl; return EXIT_FAILURE; } + std::ofstream of("output.txt"); if (dump_type == "print") { PrintSeries printseries(*series, maxiter, frequency); - printseries.dump(); + printseries.dump(of); } else if (dump_type == "write") { WriteSeries writeseries(*series, maxiter, frequency); - writeseries.dump(); + writeseries.dump(of); } delete series; } diff --git a/exercice_2/src/print_series.cc b/exercice_2/src/print_series.cc index c2afd2e..259cd9e 100644 --- a/exercice_2/src/print_series.cc +++ b/exercice_2/src/print_series.cc @@ -1,19 +1,21 @@ #include "print_series.hh" #include +#include #include PrintSeries::PrintSeries(Series &series, unsigned int maxiter, unsigned int frequency) : DumperSeries(series), maxiter(maxiter), frequency(frequency) {} -void PrintSeries::dump() { +void PrintSeries::dump(std::ostream &os) { double analytic_prediction = this->series.getAnalyticPrediction(); if (~std::isnan(analytic_prediction)) { for (int i = 1; i <= this->maxiter; i += this->frequency) { double value = this->series.compute(i); - std::cout << value << " / " << analytic_prediction << std::endl; + os << std::setprecision(this->precision) << value << " / " + << analytic_prediction << std::endl; } } } PrintSeries::~PrintSeries() = default; diff --git a/exercice_2/src/print_series.hh b/exercice_2/src/print_series.hh index 004ef0b..0388607 100644 --- a/exercice_2/src/print_series.hh +++ b/exercice_2/src/print_series.hh @@ -1,19 +1,19 @@ #ifndef PRINT_SERIES_HH #define PRINT_SERIES_HH #include "dumper_series.hh" class PrintSeries : public DumperSeries { public: PrintSeries(Series &series, unsigned int maxiter, unsigned int frequency); virtual ~PrintSeries(); public: - void dump(); + void dump(std::ostream &os = std::cout); protected: unsigned int maxiter; unsigned int frequency; }; #endif diff --git a/exercice_2/src/write_series.cc b/exercice_2/src/write_series.cc index d464941..15005c4 100644 --- a/exercice_2/src/write_series.cc +++ b/exercice_2/src/write_series.cc @@ -1,33 +1,33 @@ #include "write_series.hh" #include #include WriteSeries::WriteSeries(Series &series, unsigned int maxiter, unsigned int frequency) : DumperSeries(series), maxiter(maxiter), frequency(frequency), separator(" ") {} -void WriteSeries::dump() { - std::string filepath("series"); +void WriteSeries::dump(std::ostream &os) { std::string separator = this->separator; - if (separator == ",") - filepath += ".csv"; - else if (separator == "|") - filepath += ".psv"; - else - filepath += ".txt"; - std::ofstream f(filepath); + // std::string filepath("series"); + // if (separator == ",") + // filepath += ".csv"; + // else if (separator == "|") + // filepath += ".psv"; + // else + // filepath += ".txt"; + // std::ofstream f(filepath); double analytic_prediction = this->series.getAnalyticPrediction(); if (~std::isnan(analytic_prediction)) { for (int i = 1; i <= this->maxiter; i += this->frequency) { double value = this->series.compute(i); - f << value << separator << analytic_prediction << std::endl; + os << value << separator << analytic_prediction << std::endl; } } } void WriteSeries::setSeparator(std::string separator) { this->separator = separator; } WriteSeries::~WriteSeries() = default; diff --git a/exercice_2/src/write_series.hh b/exercice_2/src/write_series.hh index 61d9832..ce1f5f0 100644 --- a/exercice_2/src/write_series.hh +++ b/exercice_2/src/write_series.hh @@ -1,22 +1,22 @@ #ifndef WRITE_SERIES_HH #define WRITE_SERIES_HH #include "dumper_series.hh" #include class WriteSeries : public DumperSeries { public: WriteSeries(Series &series, unsigned int maxiter, unsigned int frequency); virtual ~WriteSeries(); public: - void dump(); + void dump(std::ostream &os); void setSeparator(std::string separator); protected: unsigned int maxiter; unsigned int frequency; std::string separator; }; #endif