diff --git a/work/week6/series/homework2/src/main.cc b/work/week6/series/homework2/src/main.cc index 37e21a3..9e099e4 100644 --- a/work/week6/series/homework2/src/main.cc +++ b/work/week6/series/homework2/src/main.cc @@ -1,58 +1,66 @@ #include #include #include #include #include #include #include "arithmetic.hh" #include "pi.hh" #include "series.hh" #include "print.hh" #include "write.hh" #include "dumper.hh" /* -------------------------------------------------------------------------- */ int main(int argc, char ** argv) { - // Parse method type - std::string method = "ar"; // default value - if (argc > 1) { - method = argv[1]; + // Parse arguments into stringstream + std::stringstream sstr; + for (int i = 1; i < argc; ++i) { + sstr << argv[i] << " "; } - // Parse number of iterations - unsigned int N = 10; // default value - if (argc > 2) { - N = atoi(argv[2]); - } + // Argument 1: series type ("ar" or "pi") + std::string seriestype; + sstr >> seriestype; + + // Argument 2: number of iterations + unsigned int N; + sstr >> N; + + // Argument 3: dump method ("print" or "write") + std::string dumpmethod; + sstr >> dumpmethod; - // Parse frequency - unsigned int freq = 1; // default value - if (argc > 3) { - freq= atoi(argv[3]); + // Argument 4 (conditional): frequency (for "print") or separator (for "write") + unsigned int freq; + std::string separator; + if (dumpmethod == "print") { + sstr >> freq; + } else if (dumpmethod == "write") { + sstr >> separator; } // Instanciate appropriate series object std::unique_ptr series; // doesn't point to anything yet - if (method == "ar") { + if (seriestype == "ar") { series.reset(new ComputeArithmetic); - } else if (method == "pi") { + } else if (seriestype == "pi") { series.reset(new ComputePi); } - // Compute series - std::cout << series->getName() << "(" << N << ") = " << series->compute(N) << std::endl; - - - // Print series values from 1 to N onto screen (with specific frequency) - PrintSeries printer(*series,N,freq); - printer.dump(); + // Instanciate appropriate dumper object + std::unique_ptr dumper; // doesn't point to anything yet + if (dumpmethod == "print") { + dumper.reset(new PrintSeries(*series, N, freq)); + } else if (dumpmethod == "write") { + dumper.reset(new WriteSeries(*series, N, separator)); + } - // Dump series values from 1 to N into file - WriteSeries writer(*series, N, ","); - writer.dump(std::cout); + // Dump series + dumper->dump(std::cout); return EXIT_SUCCESS; }