diff --git a/Homework2/src/dumper_series.hh b/Homework2/src/dumper_series.hh index 73c457b..d182e96 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 class DumperSeries { public: // Constructor - Destructor DumperSeries(Series& series): series(series), dump_precision(4){} virtual ~DumperSeries(){} // Functions - virtual void dump(std::ostream& os) = 0; // The function that finally dumps numbers + virtual void dump(std::ostream& os = std::cout) = 0; // The function that finally dumps numbers void setPrecision(unsigned int precision); // Setter function for dump_precision protected: // Members Series& series; // Series to be dumped unsigned int dump_precision; // Number of decimal places in writing either on screen or in file }; 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 3177b93..5a965f8 100644 --- a/Homework2/src/main.cc +++ b/Homework2/src/main.cc @@ -1,154 +1,152 @@ #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); -/* + void decide_dumper(DumperSeries *&new_dumper, Series *&ptrSeries, string type, int Nmbr, int frqncy, string sprtr, int prcsn) { if (type == "print") { new_dumper = new PrintSeries(*ptrSeries, Nmbr, frqncy, prcsn); } else if (type == "write") { new_dumper = new WriteSeries(*ptrSeries, Nmbr, sprtr, prcsn); } else { cout<<"ERROR: Invalid dumbper type. Please enter 'print' or 'write'."<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; // Pointer to Series DumperSeries *ptrDumper = nullptr; // Pointer to Dumper // Choose series to be implemented decide_series(ptrSeries, serie_type); - /* + decide_dumper(ptrDumper, ptrSeries, dump_type, N, freq, separator, precision); - ptrDumper = new PrintSeries(*ptrSeries, N, freq, precision); ptrDumper -> dump(); - */ - +/* // Dumper and writing if (dump_type == "print") { 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, separator, precision); 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'."<