From c4a60da36fd345a99a2f7868913a2d41ccade648 Mon Sep 17 00:00:00 2001 From: david <david@lipn.fr> Date: Tue, 10 Sep 2019 09:31:48 +0200 Subject: [PATCH] Ajout analyzer.cpp --- TP1/CPP/analyzer.cpp | 37 +++++++++++++++++++++++++++++++++++++ 1 file changed, 37 insertions(+) create mode 100644 TP1/CPP/analyzer.cpp diff --git a/TP1/CPP/analyzer.cpp b/TP1/CPP/analyzer.cpp new file mode 100644 index 0000000..9d2659e --- /dev/null +++ b/TP1/CPP/analyzer.cpp @@ -0,0 +1,37 @@ +#include "analyzer.hpp" +#include <fstream> + + +void Analyzer::append(const double & x){ + cost.push_back(x); + cumulative_cost.push_back( (cumulative_cost.size()) ? cumulative_cost.back()+x : x); + cumulative_square += x*x; +} + +double Analyzer::get_average_cost(){ + if(cumulative_cost.empty()) + throw std::runtime_error("List is empty"); + return cumulative_cost.back()/cumulative_cost.size(); +} + +double Analyzer::get_variance(){ + double mean, mean_square; + mean = get_average_cost(); + mean_square = mean * mean; + return cumulative_square - mean_square; +} + +void Analyzer::save_values(const std::string & path){ + std::ofstream f; + size_t i; + f.open(path.c_str()); + for (i = 0; i < cost.size(); ++i) + f<<i<<" "<<cost.at(i)<<" "<<get_amortized_cost(i)<<std::endl; + f.close(); +} + +void Analyzer::plot_values(){ + size_t i; + for (i = 0; i < cost.size(); ++i) + std::cout<<i<<" "<<cost.at(i)<<" "<<get_amortized_cost(i)<<std::endl; +} -- GitLab