diff --git a/Laba N 1.cbp b/Laba N 1.cbp new file mode 100644 index 0000000..f9de682 --- /dev/null +++ b/Laba N 1.cbp @@ -0,0 +1,49 @@ + + + + + + diff --git a/histogram.cpp b/histogram.cpp index ca9a39b..1ceca5d 100644 --- a/histogram.cpp +++ b/histogram.cpp @@ -1,12 +1,17 @@ #include "histogram.h" - +#include using namespace std; -void +bool find_minmax(const std::vector& numbers, double& Min, double& Max) { + bool flag=true; + if(numbers.size()==0 ){ + flag=false;}else + { Min = numbers[0]; Max = numbers[0]; - for(size_t x : numbers) + + for(double x : numbers) { if(x < Min) { @@ -19,6 +24,8 @@ find_minmax(const std::vector& numbers, double& Min, double& Max) { } } } + } + return flag; } std::vector make_histogram(std::vector numbers, size_t bin_count) diff --git a/histogram_internal.h b/histogram_internal.h index a312935..4fef3fc 100644 --- a/histogram_internal.h +++ b/histogram_internal.h @@ -1,23 +1,11 @@ #ifndef HISTOGRAM_INTERNAL_H_INCLUDED #define HISTOGRAM_INTERNAL_H_INCLUDED -void -find_minmax(const std::vector& numbers, double& Min, double& Max) { - Min = numbers[0]; - Max = numbers[0]; - for(size_t x : numbers) - { - if(x < Min) - { - Min = x; - }else - { - if(x > Max) - { - Max = x; - } - } - } -} +#include +#include +using namespace std; + +bool +find_minmax(const std::vector& numbers, double& Min, double& Max); #endif // HISTOGRAM_INTERNAL_H_INCLUDED diff --git a/main.cpp b/main.cpp index ad3db7a..387439a 100644 --- a/main.cpp +++ b/main.cpp @@ -3,6 +3,7 @@ #include "text.h" #include "histogram.h" #include "svg.h" +#include "procent.h" using namespace std; struct Input { std::vector numbers; @@ -25,6 +26,7 @@ input_data() { int main() { auto in = input_data(); + auto bins = make_histogram(in.numbers, in.bin_count); auto max_count=bins[0]; for(size_t x : bins){ @@ -33,7 +35,8 @@ int main() max_count=x; } } + vector procent = make_histogram_proc(in.numbers, in.bin_count, bins); show_histogram_text(bins,in.bin_count,in.numbers,max_count); - //show_histogram_svg(bins, max_count); + //show_histogram_svg(bins, max_count,procent); return 0; } diff --git a/svg.cpp b/svg.cpp index 72de81b..2cd80f1 100644 --- a/svg.cpp +++ b/svg.cpp @@ -1,6 +1,7 @@ #include "svg.h" #include #include +#include using namespace std; void @@ -17,8 +18,14 @@ void svg_text(double left, double baseline, std::string text) { std::cout << ""<< text <<""; } -void svg_rect(double x, double y, double width, double height,std::string stroke = "black", std::string fil = "black"){ - std::cout<< ""; + +void +svg_proc(double left, double baseline,size_t procent) { + std::cout << ""<< procent <<"%"; +} + +void svg_rect(double x, double y, double width, double height,std::string stroke = "black", std::string fill = "green"){ + std::cout<< ""; } void svg_end() { @@ -26,23 +33,51 @@ svg_end() { } void -show_histogram_svg(std::vector bins,size_t max_count) { +show_histogram_svg(std::vector bins,size_t max_count, const std::vector procent) { const auto IMAGE_WIDTH = 400; const auto IMAGE_HEIGHT = 300; const auto TEXT_LEFT = 20; const auto TEXT_BASELINE = 20; const auto TEXT_WIDTH = 50; const auto BIN_HEIGHT = 30; - const auto BLOCK_WIDTH = (IMAGE_WIDTH - TEXT_WIDTH)/max_count; + const auto SCALE = 10; + const auto BLOCK_WIDTH = (IMAGE_WIDTH - TEXT_WIDTH); + svg_begin(IMAGE_WIDTH, IMAGE_HEIGHT); double top = 0; - for (size_t bin : bins) { - const double bin_width = BLOCK_WIDTH * bin; - svg_text(TEXT_LEFT, top + TEXT_BASELINE, std::to_string(bin)); + + if((SCALE*max_count)>BLOCK_WIDTH) + { + for (size_t i = 0; i < bins.size(); i++) { + const double bin_width = BLOCK_WIDTH * (bins[i]/max_count); + const auto TEXT_LEFT_PROCENT = SCALE+TEXT_WIDTH+20; + svg_text(TEXT_LEFT, top + TEXT_BASELINE, std::to_string(bins[i])); svg_rect(TEXT_WIDTH, top, bin_width, BIN_HEIGHT); + svg_proc(TEXT_LEFT_PROCENT, top + TEXT_BASELINE,procent[i]); + + top += BIN_HEIGHT; + }} + else{ + for (size_t i = 0; i < bins.size(); i++) { + const double bin_width = bins[i] * SCALE; + const auto TEXT_LEFT_PROCENT = (max_count*SCALE)+TEXT_WIDTH+20; + svg_text(TEXT_LEFT, top + TEXT_BASELINE, std::to_string(bins[i])); + svg_rect(TEXT_WIDTH, top, bin_width, BIN_HEIGHT); + svg_proc(TEXT_LEFT_PROCENT, top + TEXT_BASELINE,procent[i]); + top += BIN_HEIGHT; } svg_end(); -} + } + /*for (size_t i = 0; i < bins.size(); i++) { + const double bin_width = BLOCK_WIDTH * bins[i]; + svg_text(TEXT_LEFT, top + TEXT_BASELINE, std::to_string(bins[i])); + svg_rect(TEXT_WIDTH, top, bin_width, BIN_HEIGHT); + svg_proc(BLOCK_WIDTH*bins[i]+TEXT_LEFT+50, top + TEXT_BASELINE,procent[i]); + + top += BIN_HEIGHT; + } + svg_end(); +}*/} diff --git a/svg.h b/svg.h index c9e4a8e..1281860 100644 --- a/svg.h +++ b/svg.h @@ -5,6 +5,6 @@ using namespace std; void -show_histogram_svg(std::vector bins,size_t max_count); +show_histogram_svg(std::vector bins,size_t max_count, const std::vector procent); #endif // SVG_H_INCLUDED