Page MenuHomec4science

main.cc
No OneTemporary

File Metadata

Created
Tue, Nov 12, 06:14
//
// Homework 2
// 2019
// CP & AASU
//
#include "series.h"
#include "user_interface.h"
#include "DumperSeries.h"
#include <iostream> // cout
#include <sstream> // to use stringstream object to concatenate the arguments stored in argv
using namespace std;
int main(int argc, char **argv) {
user_choices choices;
istringstream ss;
// Load program argument
if (argc == 4)
{ choices.guided_input=false;
/* Read the first argument into choices.N. */
ss.str(argv[1]);
if (ss >> choices.N) {
printf("Argument N: %d\n", choices.N);
} else {
fprintf(stderr, "Argument 1 -- %s is not an integer.\n", argv[1]);
return 1;
}
/* Read the second argument into choices.write_or_print. */
ss.clear();
ss.str(argv[2]);
if (ss >> choices.frequency) {
printf("Argument frequency: %d\n", choices.frequency);
} else {
fprintf(stderr, "Argument 2 -- %s is not an integer.\n", argv[2]);
return 1;
}
/* Read the third argument into choices.write_or_print. */
ss.clear();
ss.str(argv[3]);
if (ss >> choices.write_or_print) {
printf("Write = 1 and Print = 2: %d\n", choices.write_or_print);
} else {
fprintf(stderr, "Argument 3 -- %s is not an integer.\n", argv[3]);
return 1;
}
std::cout << "\n";
}
else
{ choices.guided_input=true;
std::cerr << "You can input the parameters directly when calling the executable, OR PRESS ENTER to CONTINUE and input the parameters during the upcoming dialog..." << std::endl;
std::cerr << "Arguments are: " << "\n";
std::cerr << "N: integer number of iterations" << "\n";
std::cerr << "frequency: integer number equal to the frequency to print results" << "\n";
std::cerr << "option: '1' for writing the results, '2' for printing them " << "\n";
std::cin.get();
}
// Ask for the type of series or exiting the code
bool he_has_chosen = false ;
while ( not he_has_chosen) {
he_has_chosen = ask_type_of_serie(choices);
}
if (choices.type_of_serie != 3) {
// When asking for inputs on the terminal
if (choices.guided_input){ask_N(choices);}
if (choices.guided_input){ask_write_or_print(choices);}
if (choices.guided_input and choices.write_or_print !=1){ask_frequency(choices);}
Series *ptr;
if (choices.type_of_serie == 1) {
std::cout << "\n" << "Your result:" << "\n";
ComputeArithmetic tst;
ptr = &tst;
}
else if (choices.type_of_serie == 2) {
std::cout << "\n" << "Your result:" << "\n";
ComputePi tst;
ptr = &tst;
}
// ask the number of decimals to use when printing
ask_precision(choices);
// compute the series
double Sn = ptr->compute(choices.N);
std::cout << "Sum = " << Sn <<"\n" ;
// re-define series object to initialize current_index and current_value again
Series *ptr2;
if (choices.type_of_serie == 1) {
ComputeArithmetic tst2;
ptr2 = &tst2;
}
else if (choices.type_of_serie == 2) {
ComputePi tst2;
ptr2 = &tst2;
}
if (choices.write_or_print==1){
// WriteSeries;
WriteSeries mywrite(*ptr2, choices.N);
mywrite.setSeparator(choices);
// name of the output file
string filename="result";
filename=filename + mywrite.extension;
// If the file exist: delete it
std::ifstream ifile(filename.c_str());
if ((bool)ifile){
char const *filenametoberemoved=filename.data();
std::remove(filenametoberemoved); // delete file
}
// open the output file
std::ofstream myout;
myout.open(filename,std::ios::out|std::ios::app);
// feed the stream with the output file
mywrite.dump(myout);
myout.close();
// check the type of series, if it is ComputePi then plot
Series *checkptr = dynamic_cast<ComputePi *>(ptr2);
if (checkptr != nullptr){
// try to plot
if (system(nullptr) ){
// Command processor exists
string command="python ../../src/plotnumeric.py "+filename;
char cstr[command.size()+1];
strcpy(cstr,command.c_str());
int status = system(cstr);}
else{
// Command processor doesn't exists
std::cout << "execute python script manually ";}
}
}
else{
// PrintSeries;
PrintSeries myprint(*ptr2, choices.N, choices.frequency);
myprint.setPrecision(choices.precision);
myprint.dump();
}
}
}

Event Timeline