diff --git a/graph.cc b/graph.cc index 97130f2..bd172b3 100644 --- a/graph.cc +++ b/graph.cc @@ -3,34 +3,42 @@ #include "graph.h" -char * getGraphByPercentage(unsigned value, unsigned len) { - unsigned step = 0; - char * bars = new char[len + 1]; +std::string get_graph_by_percentage(unsigned value, unsigned len) +{ + unsigned step = 0; + std::string bars; - unsigned barCount = (static_cast(value) / 99.9 * len); + unsigned bar_count = (static_cast(value) / 99.9 * len); - for(step; step < barCount; step++) - bars[step] = '|'; - for(step; step < len; step++) - bars[step] = ' '; - bars[len]='\0'; + for(step; step < bar_count; step++) + { + bars.append("|"); + } + for(step; step < len; step++) + { + bars.append(" "); + } - return bars; + return bars; } -char * getGraphByValue(unsigned value, unsigned max, unsigned len) { - unsigned step = 0; - char * bars = new char[len + 1]; +std::string get_graph_by_value(unsigned value, unsigned max, unsigned len) +{ + unsigned step = 0; + std::string bars; - unsigned barCount = (static_cast(value / (max - 0.1)) * len); + unsigned bar_count = (static_cast(value / (max - 0.1)) * len); - for(step; step < barCount; step++) - bars[step] = '|'; - for(step; step < len; step++) - bars[step] = ' '; - bars[len]='\0'; + for(step; step < bar_count; step++) + { + bars.append("|"); + } + for(step; step < len; step++) + { + bars.append(" "); + } - return bars; + return bars; } diff --git a/graph.h b/graph.h index 34f96e1..7dd4c4e 100644 --- a/graph.h +++ b/graph.h @@ -1,7 +1,9 @@ #ifndef GRAPH_H_ #define GRAPH_H_ -char * getGraphByPercentage(unsigned, unsigned len = 10); -char * getGraphByValue(unsigned, unsigned, unsigned len = 10); +#include + +std::string get_graph_by_percentage(unsigned, unsigned len = 10); +std::string get_graph_by_value(unsigned, unsigned, unsigned len = 10); #endif