From b7fd7da9cbfd97e463333f505b3b96d0b86dfede Mon Sep 17 00:00:00 2001 From: NikitkinTY Date: Mon, 24 Apr 2023 01:41:05 +0300 Subject: [PATCH] =?UTF-8?q?code:=20=D0=B4=D0=BE=D0=B1=D0=B0=D0=B2=D0=BB?= =?UTF-8?q?=D0=B5=D0=BD=20=D0=B2=D1=8B=D0=B2=D0=BE=D0=B4=20=D1=88=D0=BA?= =?UTF-8?q?=D0=B0=D0=BB=D1=8B=20=D0=B2=20=D1=84=D0=BE=D1=80=D0=BC=D0=B0?= =?UTF-8?q?=D1=82=D0=B5=20svg?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .gitignore | 7 ++++ cs-lab34.cbp | 7 ++++ histogram.cpp | 45 +++++++++++++---------- main.cpp | 16 ++++++-- marks.svg | 3 ++ marks.txt | 6 +++ svg.cpp | 100 ++++++++++++++++++++++++++++++++++++++++++++++++++ svg.h | 4 ++ text.cpp | 43 ++++++++++++++-------- text.h | 5 --- 10 files changed, 192 insertions(+), 44 deletions(-) create mode 100644 marks.svg create mode 100644 marks.txt create mode 100644 svg.cpp create mode 100644 svg.h diff --git a/.gitignore b/.gitignore index bcadf1d..fe5ed81 100644 --- a/.gitignore +++ b/.gitignore @@ -2,4 +2,11 @@ /obj /cs-lab34.layout /cs-lab34.depend +/a.out +/start_01.sh +/test +/unittest.depend +/NUL /unittest.layout +/start.sh +/cs-lab34 diff --git a/cs-lab34.cbp b/cs-lab34.cbp index 49b0653..9a549a7 100644 --- a/cs-lab34.cbp +++ b/cs-lab34.cbp @@ -32,7 +32,14 @@ + + + + + + + diff --git a/histogram.cpp b/histogram.cpp index f340ae9..456da95 100644 --- a/histogram.cpp +++ b/histogram.cpp @@ -1,23 +1,31 @@ #include "histogram.h" +#include "histogram_internal.h" void -find_minmax(const std::vector& numbers, double& min, double& max) { - min = numbers[0]; - for(double element : numbers) - { - if(element < min) {min = element;} - } +find_minmax (const std::vector& numbers, double& min, double& max) { + if (numbers.size() == 0) { //empty array situation check + return; + } else { + min = numbers[0]; + for (double element : numbers) { + if (element < min) { + min = element; + } + } - max = numbers[0]; - for(double element : numbers) - { - if(element > max) {max = element;} + max = numbers[0]; + for (double element : numbers) { + if (element > max) { + max = element; + } + } + return; } } std::vector -make_histogram(const std::vector& numbers, std::size_t bin_count){ +make_histogram (const std::vector& numbers, std::size_t bin_count) { double max; double min; @@ -28,19 +36,18 @@ make_histogram(const std::vector& numbers, std::size_t bin_count){ std::vector bins(bin_count); - for(std::size_t i = 0; i < numbers.size(); ++i) //Checking if a number is in a bin. - { + for (std::size_t i = 0; i < numbers.size(); ++i) { //Checking if a number is in a bin. bool found = false; - for(std::size_t j = 0; (j < bin_count - 1) && !found; ++j) - { - if( (numbers[i] >= (min + j * bin_size)) && //Where (min + j * bin_size) is equal to the lower border - (numbers[i] < (min + (j + 1) * bin_size)) ) //and (min + (j + 1) * bin_size) is equal to the higher border. - { + for (std::size_t j = 0; (j < bin_count - 1) && !found; ++j) { + if ( (numbers[i] >= (min + j * bin_size)) && //Where (min + j * bin_size) is equal to the lower border + (numbers[i] < (min + (j + 1) * bin_size)) ) { //and (min + (j + 1) * bin_size) is equal to the higher border. ++bins[j]; found = true; } } - if(!found) {++bins[bin_count - 1];} //A special case when current number is equal to the maximum. + if (!found) { + ++bins[bin_count - 1]; //A special case when current number is equal to the maximum. + } } return bins; } diff --git a/main.cpp b/main.cpp index c1b72d7..e170d64 100755 --- a/main.cpp +++ b/main.cpp @@ -5,13 +5,14 @@ #include #include "histogram.h" #include "text.h" +#include "svg.h" using namespace std; struct Input { vector numbers; size_t bin_count{}; - //size_t interval_task{}; + size_t interval_task{}; }; @@ -29,15 +30,22 @@ input_data() { cin >> in.bin_count; - //cin >> in.interval_task; - return in; + cin >> in.interval_task; + + if ((in.interval_task >= 2.0) && (in.interval_task <= 9.0)) { + return in; + } else { + std::cout << "ERROR"; + exit(1); + } + } int main() { auto in = input_data(); auto bins = make_histogram(in.numbers, in.bin_count); - show_histogram_text(bins); + show_histogram_svg(bins, in.interval_task); return 0; } diff --git a/marks.svg b/marks.svg new file mode 100644 index 0000000..124b741 --- /dev/null +++ b/marks.svg @@ -0,0 +1,3 @@ + + +9331000630 diff --git a/marks.txt b/marks.txt new file mode 100644 index 0000000..d4b30eb --- /dev/null +++ b/marks.txt @@ -0,0 +1,6 @@ +142 +1 1 1 1 1 1 1 1 1 +2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 +3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 +3 +6 diff --git a/svg.cpp b/svg.cpp new file mode 100644 index 0000000..fd4046a --- /dev/null +++ b/svg.cpp @@ -0,0 +1,100 @@ +#include +#include "svg.h" +#include +#include + +static void +find_max (const std::vector& numbers, std::size_t& max) { + max = numbers[0]; + for (double element : numbers) { + if (element > max) { + max = element; + } + } +} + +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 = "black", std::string fill = "black") { + std::cout << ""; +} + + + +void +show_histogram_svg (const std::vector& bins, std::size_t& interval_task) { + 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 = 50; + const auto MAX_WIDTH = IMAGE_WIDTH - TEXT_WIDTH; + //const auto SPACE_BETWEEN_INTERVALS = 10; + const auto INTERVAL_SCALED = interval_task * 10; + const auto SCALE_WIDTH = 8; + //const auto HALF_SPACE_BETWEEN_INTERVALS = static_cast(SPACE_BETWEEN_INTERVALS) / (2); + const auto HALF_DEFAULT_FONT = 4; + const auto MINIMAL_FONT_SPACE = 4 * 4; + + std::size_t max_bin; + find_max(bins, max_bin); + double modifier; + + modifier = static_cast(MAX_WIDTH) / (max_bin); + + size_t times; + + for (times = 0; (times * INTERVAL_SCALED) < (MAX_WIDTH - MINIMAL_FONT_SPACE); ++times); + + double interval_space = static_cast(MAX_WIDTH - INTERVAL_SCALED * (times - 1)) / (times - 2); + double half_interval_space = interval_space / 2; + + svg_begin(410, 300); + + double top = 0; + for (size_t bin : bins) { + const double bin_width = modifier * bin; + svg_text(TEXT_LEFT, top + TEXT_BASELINE, std::to_string(bin)); + svg_rect(TEXT_WIDTH, top, bin_width, BIN_HEIGHT, "#483D8B", "#9370DB"); + top += BIN_HEIGHT; + } + + top += 10; + + times -= 1; + for (size_t i = 0; i < times; ++i) { + svg_rect(TEXT_WIDTH + i * (INTERVAL_SCALED + interval_space), top, INTERVAL_SCALED, SCALE_WIDTH, "chocolate", "tan"); + } + + top += 10; + + svg_text(TEXT_LEFT, top + TEXT_BASELINE, std::to_string(0)); + svg_text(TEXT_WIDTH + INTERVAL_SCALED + half_interval_space - HALF_DEFAULT_FONT, top + TEXT_BASELINE, std::to_string(interval_task)); + svg_text(TEXT_WIDTH + (INTERVAL_SCALED * times) + interval_space * (times - 1) - (HALF_DEFAULT_FONT * 2), top + TEXT_BASELINE, std::to_string(interval_task * times)); + + svg_end(); +} + + + diff --git a/svg.h b/svg.h new file mode 100644 index 0000000..79e66bb --- /dev/null +++ b/svg.h @@ -0,0 +1,4 @@ +#include + +void +show_histogram_svg(const std::vector& bins, std::size_t& interval_task); diff --git a/text.cpp b/text.cpp index 13e8bbe..4efa9ef 100644 --- a/text.cpp +++ b/text.cpp @@ -1,17 +1,21 @@ #include #include "text.h" +const size_t screen_width = 80; +const size_t max_asterisk = screen_width - 3 - 1; + static void -find_max(const std::vector& numbers, std::size_t& max) { +find_max (const std::vector& numbers, std::size_t& max) { max = numbers[0]; - for(double element : numbers) - { - if(element > max) {max = element;} + for (double element : numbers) { + if (element > max) { + max = element; + } } } void -show_histogram_text(const std::vector& bins){ +show_histogram_text (const std::vector& bins) { std::size_t max_bin; find_max(bins, max_bin); double modifier; @@ -23,22 +27,29 @@ show_histogram_text(const std::vector& bins){ * In other case, histogram won't be scaled, i.e, asterisk count depends on the current bin number. */ - if(max_bin > 76) {modifier = static_cast(74) / (max_bin);} - else {modifier = 1;} + if (max_bin > max_asterisk) { + modifier = static_cast(max_asterisk) / (max_bin); + } else { + modifier = 1; + } - for(std::size_t i = 0; i < bins.size(); ++i) //Histogram output with alignment, if necessary. - { - if(bins[i] >= 10) - { - if(bins[i] >= 100) {std::cout << bins[i] << '|';} //Output a three-digit number. - else {std::cout << ' ' << bins[i] << '|';} //Output a two-digit number with alignment. - } - else {std::cout << " " << bins[i] << '|';} //Output a single-digit number with alignment. + for (std::size_t i = 0; i < bins.size(); ++i) { //Histogram output with alignment, if necessary. + if (bins[i] >= 10) { + if (bins[i] >= 100) { + std::cout << bins[i] << '|'; + } //Output a three-digit number. + else { + std::cout << ' ' << bins[i] << '|'; + } //Output a two-digit number with alignment. + } else { + std::cout << " " << bins[i] << '|'; + } //Output a single-digit number with alignment. size_t height = modifier * bins[i]; //Height stands for the number of output asterisks. - for(size_t k = 0; k < height; ++k) + for (size_t k = 0; k < height; ++k) { std::cout << '*'; + } std::cout << "\n"; } } diff --git a/text.h b/text.h index 9a147d8..29c07a4 100644 --- a/text.h +++ b/text.h @@ -1,8 +1,3 @@ -#ifndef HISTOGRAM_H_INCLUDED -#define HISTOGRAM_H_INCLUDED - -#endif // HISTOGRAM_H_INCLUDED - #include void