diff --git a/homework2/plot.py b/homework2/plot.py index 8af1a89..2ed038a 100644 --- a/homework2/plot.py +++ b/homework2/plot.py @@ -1,21 +1,34 @@ import numpy as np import matplotlib.pyplot as plt import sys -if (not len(sys.argv) == 2): - print("Please give the data file as the only argument for this program") +if (not len(sys.argv) == 3): + print("Please give the data file as the first argument and the separator as the second argument to this program") sys.exit() file_name = sys.argv[1] -data = np.loadtxt(file_name, delimiter = " ") + +if sys.argv[2] == "comma": + separator = "," +elif sys.argv[2] == "space": + separator = " " +elif sys.argv[2] == "tab": + separator = "\t" +elif sys.argv[2] == "pipe": + separator = "|" +else: + print("not known separator! please choose one of {comma, space, tab, pipe}") + exit(0); + +data = np.loadtxt(file_name, delimiter = separator) plt.figure() plt.plot(data[:,0], data[:,1], '-bo') if (np.shape(data)[1] == 3): - plt.plot(data[:,0], data[:,1]/data[:,2]*100.0, '-r', linewidth=2) + plt.plot(data[:,0], data[:,1]/data[:,2]*100.0, '-r', linewidth=4) plt.legend(['Series','Analytical value'], loc = 4) plt.grid() plt.xlabel("i") plt.ylabel("value") plt.show() diff --git a/homework2/src/dumper_series.h b/homework2/src/dumper_series.h index a2bcefa..0e074a5 100644 --- a/homework2/src/dumper_series.h +++ b/homework2/src/dumper_series.h @@ -1,27 +1,33 @@ #ifndef DUMPER_SERIES #define DUMPER_SERIES #include "Series.hh" #include class DumperSeries{ public: DumperSeries(Series & series):series(series) { } virtual void dump(std::ostream & os) = 0; virtual void setPrecision(unsigned int precision) { this->precision = precision; } protected: Series & series; unsigned int precision = 8; }; +inline std::ostream & operator << (std::ostream & stream, DumperSeries & _this) +{ + _this.dump(stream); + return stream; +} + #endif diff --git a/homework2/src/main.cc b/homework2/src/main.cc index 157c960..1112646 100644 --- a/homework2/src/main.cc +++ b/homework2/src/main.cc @@ -1,88 +1,92 @@ //main #include "ComputeArithmetic.hh" #include "ComputePi.hh" #include "print_series.h" #include "write_series.h" #include "arglist.hh" #include #include #include #include using namespace std; int main(int argc, char ** argv){ ArgList args(argc, argv, "Computation of numerical Series"); const string which_series = args.getstr("-st", "--seriestype", "pi", " type of series, one of [arithmetic, pi]"); const string which_dumper = args.getstr("-dt", "--dumpertype", "print", " type of output"); const string separator = args.getstr("-sep", "--sep", "", ""); const string fname = args.getstr("-o", "--outfile", "", "name of the output file"); const int frequency = args.getint("-f", "--freq", 2, "..."); const int maxiter = args.getint("-Nmax","--Nmax", 100, "..."); if (which_dumper=="write" & ((separator=="")||(fname==""))){ cerr<<"to write the output on a file, specify the file name (-o) and the separator (-sep) "<> which_series; //sstr >> which_dumper; //sstr >> frequency; //sstr >> maxiter; Series * my_series = NULL; DumperSeries * my_dumper = NULL; if (which_series == "pi") my_series = new ComputePi(); else if (which_series == "arithmetic") my_series = new ComputeArithmetic(); else { cout<<"insert one of the two available options:" "\n\t arithmetic (to compute arithmetic series)" "\n\t pi (to compute Pi series)"<setPrecision(6); }else if (which_dumper == "write"){ my_dumper = new WriteSeries(*my_series, frequency, maxiter, separator); + os = & file; }else { cout<<"insert one of the two available options:" "\n\t print (to print on screen)" "\n\t write (to write in file)"<setPrecision(12); - my_dumper->dump(*os); - - file.close(); + + //my_dumper->dump(*os); + *os << *my_dumper; + //double tmpi = my_series->compute(N); //cout<<"Result for the "<frequency = frequency; this->maxiter = maxiter; } void PrintSeries::dump(std::ostream & os){ os.precision(precision); for(int i=frequency; i