diff --git a/Homework2/src/dumper_series.hh b/Homework2/src/dumper_series.hh index b064b63..bff9797 100644 --- a/Homework2/src/dumper_series.hh +++ b/Homework2/src/dumper_series.hh @@ -1,32 +1,32 @@ #ifndef DUMPER_SERIES_HH #define DUMPER_SERIES_HH #include "series.hh" -#include #include -#include +#include +#include class DumperSeries { public: // Constructor - Destructor DumperSeries(Series& series): series(series), dump_precision(4){} virtual ~DumperSeries(){} // Functions virtual void dump(std::ostream& os) = 0; void setPrecision(unsigned int precision); protected: // Members Series& series; unsigned int dump_precision; }; inline std::ostream & operator << (std::ostream & stream, DumperSeries & _this) { _this.dump(stream); return stream; } #endif \ No newline at end of file diff --git a/Homework2/src/main.cc b/Homework2/src/main.cc index 35e3fa6..d992e54 100644 --- a/Homework2/src/main.cc +++ b/Homework2/src/main.cc @@ -1,130 +1,129 @@ #include #include #include #include #include "series.hh" #include "compute_arithmetic.hh" #include "compute_pi.hh" #include "dumper_series.hh" #include "print_series.hh" #include "write_series.hh" using namespace std; // Functions declaration void decide_series(Series *&new_series, string type); int main(int argc, char ** argv) { /* // ========== TEST 1: Checking the function "compute(int N)" ========== compute_arithmetic my_arithmetic; // Object of type "compute_arithmetic" compute_pi my_pi; // Object of type "compute_pi" cout << my_arithmetic.compute(10) << endl; //Arithmetic series up to 10. Expected value: 55 cout << my_pi.compute(10) << endl; //Pi series up to 10. Expected value: 3.04936 */ /* // ========== TEST 2: Working with a Series pointer" ========== Series *SERIES1, *SERIES2; //Pointers of type Series SERIES1 = new compute_arithmetic(); //SERIES1 points to an object of type compute_arithmetic SERIES2 = new compute_pi(); //SERIES2 points to an object of type compute_pi cout << SERIES1->compute(10) << endl; //Arithmetic series up to 10. Expected value: 55 cout << SERIES2->compute(10) << endl; //Pi series up to 10. Expected value: 3.04936 delete SERIES1; //Free up memory delete SERIES2; //Free up memory */ int N_inputs = argc; std::stringstream inputs; for (int i = 1; i < argc; ++i){ inputs << argv[i] << " "; } std::string serie_type; std::string dump_type; std::string separator; unsigned long N; // max number of iteration entered as input unsigned long freq; //frequency for dumper series unsigned int precision; inputs >> serie_type; inputs >> dump_type; inputs >> separator; inputs >> N; inputs >> freq; inputs >> precision; printf("###### User Inputs ###### \n"); std::cout << "Serie type: " << serie_type << std::endl; std::cout << "Dump type: " << dump_type << std::endl; std::cout << "Separator type: " << separator << std::endl; std::cout << "Max iterations: " << N << std::endl; std::cout << "Dump frequency: " << freq << std::endl; std::cout << "Precision: " << precision << std::endl; printf("######################## \n"); Series *ptrSeries = nullptr; // Choose series to be implemented decide_series(ptrSeries, serie_type); // Dumper and writing if (dump_type == "print") { - PrintSeries my_dump(*ptrSeries, N, freq); + PrintSeries my_dump(*ptrSeries, N, freq, precision); my_dump.dump(); std::string file_name = "ostream_output.txt"; std::ofstream my_os_file; my_os_file.open(file_name, std::ios::out | std::ios::trunc); my_dump.dump(my_os_file); my_os_file.close(); } else if (dump_type == "write") { - WriteSeries my_dump(*ptrSeries, N); - my_dump.setPrecision(precision); + WriteSeries my_dump(*ptrSeries, N, precision); if ((separator == "comma") || (separator == "pipe")){ my_dump.setSeparator(separator); } else{ std::cout << "Data are written to a txt fille with a tab separator." << std::endl; } my_dump.dump(); } else { std::cout << "Dump type does not exist" << std::endl; abort(); } delete ptrSeries; return 0; } void decide_series(Series *&new_series, string type) { if (type == "arithmetic") { new_series = new compute_arithmetic(); } else if (type == "pi") { new_series = new compute_pi(); } else { cout<<"ERROR: Invalid series type. Please enter 'arithmetic' or 'pi'."< -#include -#include -#include + +WriteSeries::WriteSeries(Series& series, unsigned int N, unsigned int precision): DumperSeries(series) +{ + maxiter = N; +} void WriteSeries::setSeparator(std::string separator){ if (separator == "comma"){ sep = ", "; format = ".csv"; } else if (separator == "pipe"){ sep = "|"; format = ".psv"; } } void WriteSeries::dump(std::ostream & os){ std::string file_name = "series_output"; file_name.append(format); std::ofstream my_file; my_file.open(file_name, std::ios::out | std::ios::trunc); double computed = 0.0; double analytical = 0.0; double error = 0.0; for (unsigned long i = 0.0; i <= maxiter; ++i){ if (std::isnan(series.getAnalyticPrediction())){ computed = series.compute(i); my_file << std::setprecision(dump_precision) << i << "\t" << computed << std::endl; }else { computed = series.compute(i); analytical = series.getAnalyticPrediction(); error = 100.0*(analytical - computed)/analytical; my_file << std::setprecision(dump_precision) << i << "\t" << computed << "\t" << analytical << "\t" << error < #include "dumper_series.hh" - class WriteSeries: public DumperSeries { +public: + // Constructor + WriteSeries(Series& series, unsigned int N, unsigned int precision = 4); + + void setSeparator(std::string separator); + void dump(std::ostream& os = std::cout); + private: unsigned long maxiter; std::string sep = "\t"; //default separator std::string format = ".txt"; //default file format - -public: - WriteSeries(Series & series, unsigned long N) : DumperSeries(series){ - maxiter = N; - } - - void setSeparator(std::string separator); - void dump(std::ostream & os = std::cout); - //void setPrecision(unsigned int precision) {dump_precision = precision;}; }; #endif \ No newline at end of file