From 839f3e99427d3c67104026acfd8894bb6669b135 Mon Sep 17 00:00:00 2001 From: BykovDA Date: Wed, 30 Apr 2025 16:59:48 +0300 Subject: [PATCH] =?UTF-8?q?=D0=B2=D1=8B=D0=B2=D0=BE=D0=B4=20=D0=B3=D0=B8?= =?UTF-8?q?=D1=81=D1=82=D0=BE=D0=B3=D1=80=D0=B0=D0=BC=D0=BC=D1=8B=20=D0=B2?= =?UTF-8?q?=20svg=20=D1=84=D0=BE=D1=80=D0=BC=D0=B0=D1=82=D0=B5?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- histogram.cpp | 17 ++++++---- histogram.h | 8 +++++ main.cpp | 90 +++------------------------------------------------ svg.cpp | 48 +++++++++++++++++++++++++++ svg.h | 8 +++++ text.cpp | 51 +++++++++++++++++++++++++++++ text.h | 8 +++++ 7 files changed, 138 insertions(+), 92 deletions(-) create mode 100644 histogram.h create mode 100644 svg.cpp create mode 100644 svg.h create mode 100644 text.cpp create mode 100644 text.h diff --git a/histogram.cpp b/histogram.cpp index 3fa3cf2..73e54da 100644 --- a/histogram.cpp +++ b/histogram.cpp @@ -1,16 +1,21 @@ #include "histogram.h" bool -find_minmax(const std::vector& numbers, double& min, double& max) { +find_minmax(std::vector numbers, double& min, double& max) { + if(numbers.size() == 0){ + return false; + } min = numbers[0]; max = numbers[0]; - for(double x : numbers){ - if(xmax){ - max=x; + else if (x > max) + { + max = x; } } + return true; } std::vector make_histogram(const std::vector& numbers,size_t bin_count){ diff --git a/histogram.h b/histogram.h new file mode 100644 index 0000000..8f9c9fa --- /dev/null +++ b/histogram.h @@ -0,0 +1,8 @@ +#ifndef HISTOGRAM_H_INCLUDED +#define HISTOGRAM_H_INCLUDED +#include +#include +std::vector +make_histogram(const std::vector& numbers, size_t bin_count); + +#endif diff --git a/main.cpp b/main.cpp index 3214831..e0aec15 100644 --- a/main.cpp +++ b/main.cpp @@ -1,6 +1,8 @@ +#include "histogram.h" +#include "text.h" #include #include - +#include "svg.h" using namespace std; struct Input { vector numbers; @@ -20,95 +22,11 @@ input_data(){ cin >> in.bin_count; return in; } -void -find_minmax(const vector& numbers, double& min, double& max) { - min = numbers[0]; - max = numbers[0]; - for(double x : numbers){ - if(xmax){ - max=x; - } - } -} -vector -make_histogram(const vector& numbers,size_t bin_count){ - vector bins(bin_count); - double min,max; - find_minmax(numbers,min,max); - double bin_size=(max-min)/bin_count; - for(size_t i=0; ibins,size_t bin_count){ - size_t Maxbins = 0; - for (size_t i = 0; i < bin_count; i++) - { - if (bins[i] > Maxbins) - { - Maxbins = bins[i]; - } - } - const size_t MAX_HEIGHT = 76; - if (Maxbins <= MAX_HEIGHT) - { - for (size_t i = 0; i < bin_count; i++) - { - if (bins[i] < 10) - { - cout << " "; - } - cout << " " << bins[i]<< "|"; - for (size_t j = 0; j < bins[i]; j++) - { - cout << "*"; - } - cout << endl; - } - } - else - { - for (size_t i = 0; i < bin_count; i++) - { - size_t height = static_cast(MAX_HEIGHT * (static_cast(bins[i]) / Maxbins)); - - if (bins[i] < 100) - { - cout << " "; - } - if (bins[i] < 10) - { - cout << " "; - } - cout << bins[i] << "|"; - for (size_t j = 0; j < height; j++) - { - cout << "*"; - } - cout << endl; - } - } -} int main() { auto in = input_data(); auto bins = make_histogram(in.numbers, in.bin_count); - show_histogram(bins,in.bin_count); + show_histogram_svg(bins); return 0; } diff --git a/svg.cpp b/svg.cpp new file mode 100644 index 0000000..30ad08d --- /dev/null +++ b/svg.cpp @@ -0,0 +1,48 @@ +#include "svg.h" +void +svg_begin(double width, double height) { + std::cout << "\n"; + std::cout << "\n"; +} + +void +svg_end() { + std::cout << "\n"; +} +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 = "red",std::string fill= "black"){ + std::cout << "";} +void +show_histogram_svg(const std::vector& bins) { + 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 = 10; + const auto COLOUR_1 = "green"; + const auto COLOUR_2 = "blue"; + svg_begin(400, 300); + + 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)); + svg_rect(TEXT_WIDTH, top, bin_width, BIN_HEIGHT,COLOUR_1,COLOUR_2); + top += BIN_HEIGHT; + } + + svg_end(); +} + + diff --git a/svg.h b/svg.h new file mode 100644 index 0000000..4eeadf2 --- /dev/null +++ b/svg.h @@ -0,0 +1,8 @@ +#ifndef SVG_H_INCLUDED +#define SVG_H_INCLUDED +#include +#include +void +show_histogram_svg(const std::vector& bins); + +#endif diff --git a/text.cpp b/text.cpp new file mode 100644 index 0000000..25a4b35 --- /dev/null +++ b/text.cpp @@ -0,0 +1,51 @@ +#include "text.h" +void +show_histogram_text(std::vectorbins,size_t bin_count){ + size_t Maxbins = 0; + for (size_t i = 0; i < bin_count; i++) + { + if (bins[i] > Maxbins) + { + Maxbins = bins[i]; + } + } + const size_t MAX_HEIGHT = 76; + if (Maxbins <= MAX_HEIGHT) + { + for (size_t i = 0; i < bin_count; i++) + { + if (bins[i] < 10) + { + std::cout << " "; + } + std::cout << " " << bins[i]<< "|"; + for (size_t j = 0; j < bins[i]; j++) + { + std::cout << "*"; + } + std::cout << std::endl; + } + } + else + { + for (size_t i = 0; i < bin_count; i++) + { + size_t height = static_cast(MAX_HEIGHT * (static_cast(bins[i]) / Maxbins)); + + if (bins[i] < 100) + { + std::cout << " "; + } + if (bins[i] < 10) + { + std::cout << " "; + } + std::cout << bins[i] << "|"; + for (size_t j = 0; j < height; j++) + { + std::cout << "*"; + } + std::cout << std::endl; + } + } +} diff --git a/text.h b/text.h new file mode 100644 index 0000000..9e28320 --- /dev/null +++ b/text.h @@ -0,0 +1,8 @@ +#ifndef TEXT_H_INCLUDED +#define TEXT_H_INCLUDED +#include +#include +void +show_histogram_text(std::vectorbins,size_t bin_count); + +#endif