diff --git a/hw2/src/dumper_series.cc b/hw2/src/dumper_series.cc index 39b5478d34b1bba025aa2aa7d70c8a61dffdfd80..30ff48fcb85166121bec09a3f8fb4f8423d7ed06 100644 --- a/hw2/src/dumper_series.cc +++ b/hw2/src/dumper_series.cc @@ -6,4 +6,5 @@ DumperSeries::DumperSeries(Series & in_series) : series(in_series) {}; DumperSeries::~DumperSeries() {}; +//void DumperSeries::setPrecision(uint precision) {}; diff --git a/hw2/src/dumper_series.hh b/hw2/src/dumper_series.hh index 0d07592b3130574b6341382e45ff3e7f082c436b..249d87cad2ce94e49e653e0051fb084686ace5fa 100644 --- a/hw2/src/dumper_series.hh +++ b/hw2/src/dumper_series.hh @@ -3,19 +3,27 @@ #define DUMPERSERIES_HPP #include "series.hh" +#include <iostream> class DumperSeries{ public: DumperSeries(Series & in_series); virtual ~DumperSeries(); - virtual void dump() = 0; - + virtual void dump(std::ostream & os) = 0; + virtual void setPrecision(uint precision) = 0; + protected: Series & series; }; +inline std::ostream & operator <<(std::ostream & stream, DumperSeries & _this) +{ + _this.dump(stream); + return stream; +}; + #endif diff --git a/hw2/src/main_temp.cc b/hw2/src/main_temp.cc index 046fc41f4304802f9e7077e866105bae98ca62ee..e71c8f3af3cc07f1f17495777ed74369bb2aad6d 100644 --- a/hw2/src/main_temp.cc +++ b/hw2/src/main_temp.cc @@ -10,6 +10,7 @@ #include "compute_arithmetic.hh" #include "write_series.hh" #include <sstream> +#include <fstream> int main(int argc, char *argv[]) { @@ -23,7 +24,12 @@ int main(int argc, char *argv[]) if ( option == "p" ) { PrintSeries test2 = PrintSeries( test, 2, 20 ); - test2.dump(); + test2.setPrecision(3); + //test2.dump(); + + std::ofstream outFile("output.txt"); + test2.dump(outFile); + outFile.close(); } else if ( option == "w") { std::string sep; diff --git a/hw2/src/print_series.cc b/hw2/src/print_series.cc index aa549b4795857fcf9ebbf096d678b01f4132d82c..eb74e32e84bad58243e65482683d25feb2703ec1 100644 --- a/hw2/src/print_series.cc +++ b/hw2/src/print_series.cc @@ -3,6 +3,7 @@ #include "print_series.hh" #include <iostream> #include <cmath> +#include <iomanip> PrintSeries::PrintSeries(Series &series, uint a, uint b) : DumperSeries(series) { @@ -12,10 +13,9 @@ PrintSeries::PrintSeries(Series &series, uint a, uint b) PrintSeries::~PrintSeries() {}; -void PrintSeries::dump() { +void PrintSeries::dump(std::ostream & os) { uint n_evals = maxiter / f; - double pred = series.getAnalyticPrediction(); for( uint j = 1; j <= n_evals; j++ ) { @@ -24,14 +24,19 @@ void PrintSeries::dump() { double s = series.compute( temp ); double diff = pred - s; - std::cout << "Sum for N = " << temp << " : " << s; + os << "Sum for N = " << temp << " : " << std::fixed + << std::setprecision(precision) << s; if ( !std::isnan(pred) ) { - std::cout << " with deviation of " << diff << std::endl; + os << " with deviation of " << diff << std::endl; } - else std::cout << std::endl; + else os << std::endl; }; }; +void PrintSeries::setPrecision(uint precision) { + this->precision = precision; +}; + diff --git a/hw2/src/print_series.hh b/hw2/src/print_series.hh index 1fd7f254a2ba4062a157a0f166e09ef5bec9cb75..02c3538b79c710a6817f8699868ba722fe559c32 100644 --- a/hw2/src/print_series.hh +++ b/hw2/src/print_series.hh @@ -7,12 +7,13 @@ class PrintSeries : public DumperSeries { private: - uint f, maxiter; + uint f, maxiter, precision; public: PrintSeries(Series &series, uint a, uint b); ~PrintSeries(); - void dump(); + void dump(std::ostream & os = std::cout ); + void setPrecision(uint precision); }; diff --git a/hw2/src/write_series.cc b/hw2/src/write_series.cc index 16bc76eafabd5b3d3a88b67bd372ba604badc9a0..f56190b00c368f9e4d66a33fb3bfaac94d5ecb54 100644 --- a/hw2/src/write_series.cc +++ b/hw2/src/write_series.cc @@ -14,7 +14,7 @@ WriteSeries::WriteSeries(Series &series, uint a, uint b) WriteSeries::~WriteSeries() {}; -void WriteSeries::dump() { +void WriteSeries::dump(std::ostream & os) { std::ofstream outFile("data." + type); uint n_evals = maxiter / f; @@ -47,6 +47,7 @@ void WriteSeries::setSeparator(std::string sep) { }; +void WriteSeries::setPrecision(uint precision) {}; diff --git a/hw2/src/write_series.hh b/hw2/src/write_series.hh index 59dba9cf299f70ada072eb71d80c894d286796b6..bb170de036bdea0dcfeee3e39dffc452f8637b0b 100644 --- a/hw2/src/write_series.hh +++ b/hw2/src/write_series.hh @@ -14,8 +14,9 @@ class WriteSeries : public DumperSeries { public: WriteSeries(Series &series, uint a, uint b); ~WriteSeries(); - void dump(); + void dump(std::ostream & os = std::cout); void setSeparator(std::string sep = " "); + void setPrecision(uint precision); };