diff --git a/cs-lab34.depend b/cs-lab34.depend index 688867f..bc5ca64 100644 --- a/cs-lab34.depend +++ b/cs-lab34.depend @@ -79,3 +79,83 @@ 1745825157 c:\users\diman3000\desktop\cs-lab34\histogram_internal.h +1748211621 source:c:\users\diman3000\desktop\cslab34\cs-lab34\main.cpp + + + "histogram.h" + "svg.h" + +1748211621 c:\users\diman3000\desktop\cslab34\cs-lab34\histogram.h + + +1748211621 c:\users\diman3000\desktop\cslab34\cs-lab34\svg.h + + + +1748211621 source:c:\users\diman3000\desktop\cslab34\cs-lab34\histogram.cpp + "histogram.h" + + +1748211621 source:c:\users\diman3000\desktop\cslab34\cs-lab34\svg.cpp + "svg.h" + + +1748211621 source:c:\users\diman3000\desktop\cslab34\cs-lab34\text.cpp + "text.h" + + +1748211621 c:\users\diman3000\desktop\cslab34\cs-lab34\text.h + + +1748211621 source:c:\users\diman3000\desktop\cslab34\cs-lab34\unittest.cpp + "doctest.h" + "histogram_internal.h" + +1748211995 c:\users\diman3000\desktop\cslab34\cs-lab34\doctest.h + + + + + + + "doctest_fwd.h" + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + +1748211621 c:\users\diman3000\desktop\cslab34\cs-lab34\histogram_internal.h + + diff --git a/doctest.h b/doctest.h index 826f0f5..c8535fa 100644 --- a/doctest.h +++ b/doctest.h @@ -1,3 +1,6 @@ +#ifndef DOCTEST_H_INCLUDED +#define DOCTEST_H_INCLUDED + // ====================================================================== lgtm [cpp/missing-header-guard] // == DO NOT MODIFY THIS FILE BY HAND - IT IS AUTO GENERATED BY CMAKE! == // ====================================================================== @@ -5724,7 +5727,7 @@ namespace { std::tm timeInfo; #ifdef DOCTEST_PLATFORM_WINDOWS -// gmtime_s(&timeInfo, &rawtime); + gmtime_s(&timeInfo, &rawtime); #else // DOCTEST_PLATFORM_WINDOWS gmtime_r(&rawtime, &timeInfo); #endif // DOCTEST_PLATFORM_WINDOWS @@ -7104,3 +7107,5 @@ DOCTEST_SUPPRESS_COMMON_WARNINGS_POP #undef NOMINMAX #undef DOCTEST_UNDEF_NOMINMAX #endif // DOCTEST_UNDEF_NOMINMAX + +#endif // DOCTEST_H_INCLUDED diff --git a/histogram.cpp b/histogram.cpp index 05d848e..641db08 100644 --- a/histogram.cpp +++ b/histogram.cpp @@ -1,39 +1,41 @@ #include "histogram.h" #include - using namespace std; void find_minmax(const vector& numbers, double& min, double& max) { - if (numbers.empty()) { + if (numbers.empty()) + { min = 0; max = 0; return; } - min = numbers[0]; max = numbers[0]; - for (float x : numbers) { + min = numbers[0]; + for (double x : numbers) { if (x < min) min = x; else if (x > max) max = x; } } -vector make_histogram(const vector& numbers, size_t bin_count) { +vector make_histogram(vector numbers, size_t bin_count) { double min, max; find_minmax(numbers, min, max); + double bin_size = (max - min) / bin_count; + vector bins(bin_count); - float k = (max - min) / bin_count; - vector bins(bin_count, 0); - - for (double number : numbers) { - bool flag = false; - for (size_t j = 0; (j < bin_count && !flag); j++) { - if (number >= (min + k * j) && number < (min + k * (j + 1))) { + for (size_t i = 0; i < numbers.size(); i++) { + bool found = false; + for (size_t j = 0; (j < bin_count - 1) && !found; j++) { + auto lo = min + j * bin_size; + auto hi = min + (j + 1) * bin_size; + if ((lo <= numbers[i]) && (numbers[i] < hi)) { bins[j]++; - flag = true; + found = true; } } - if (!flag) bins[bin_count - 1]++; + if (!found) { + bins[bin_count - 1]++; + } } - return bins; } diff --git a/histogram.h b/histogram.h index 07979b8..1ee7f4d 100644 --- a/histogram.h +++ b/histogram.h @@ -1,4 +1,9 @@ -#pragma once +#ifndef HISTOGRAM_H_INCLUDED +#define HISTOGRAM_H_INCLUDED + #include +using namespace std; + +vector make_histogram(vector numbers, size_t bin_count); -std::vector make_histogram(const std::vector& numbers, size_t bin_count); +#endif diff --git a/histogram_internal.h b/histogram_internal.h index 36788f4..7b204e9 100644 --- a/histogram_internal.h +++ b/histogram_internal.h @@ -1,4 +1,6 @@ -#pragma once -#include +#ifndef HISTOGRAM_INTERNAL_H_INCLUDED +#define HISTOGRAM_INTERNAL_H_INCLUDED void find_minmax(const std::vector& numbers, double& min, double& max); + +#endif diff --git a/main.cpp b/main.cpp index e919aa0..7e40a09 100644 --- a/main.cpp +++ b/main.cpp @@ -1,42 +1,35 @@ #include #include #include "histogram.h" +#include "text.h" #include "svg.h" - using namespace std; -// Структура для хранения входных данных struct Input { vector numbers; - size_t bin_count{}; + size_t bin_count; }; -// Функция ввода данных Input input_data() { Input in; - int number_count; - + size_t number_count; + cerr << "Enter number count: "; cin >> number_count; - while (number_count < 1) { - cin >> number_count; - } - - cin >> in.bin_count; - while (in.bin_count < 1) { - cin >> in.bin_count; - } in.numbers.resize(number_count); - for (int i = 0; i < number_count; i++) { + cerr << "Enter numbers: "; + for (size_t i = 0; i < number_count; i++) { cin >> in.numbers[i]; } + cerr << "Enter bin count: "; + cin >> in.bin_count; return in; } -// Главная функция int main() { - auto in = input_data(); // Ввод данных - auto bins = make_histogram(in.numbers, in.bin_count); // Расчёт гистограммы - show_histogram_svg(bins); // Вывод гистограммы в формате SVG + auto in = input_data(); + auto bins = make_histogram(in.numbers, in.bin_count); + show_histogram_svg(bins); + return 0; } diff --git a/svg.cpp b/svg.cpp index 9913faa..f17f5a8 100644 --- a/svg.cpp +++ b/svg.cpp @@ -1,56 +1,60 @@ -#include "svg.h" #include +#include +#include +#include "svg.h" using namespace std; void svg_begin(double width, double height) { cout << "\n"; - cout << "\n"; + cout << "\n"; } void svg_end() { cout << "\n"; } -void svg_text(double left, double baseline, const string& text) { +void svg_text(double left, double baseline, string text) { cout << "" << text << "\n"; } -void svg_rect(double x, double y, double width, double height, - const string& stroke, const string& fill) { - cout << "\n"; +void svg_rect(double x, double y, double width, double height, string stroke = "black", string fill = "black") { + cout << "\n"; } void show_histogram_svg(const vector& bins) { + if (bins.empty()) { + cerr << "Error: Empty bins vector\n"; + return; + } + 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; - - size_t max_count = 0; - for (size_t count : bins) { - if (count > max_count) { - max_count = count; - } - } - - double BLOCK_WIDTH = (IMAGE_WIDTH - TEXT_WIDTH) / static_cast(max_count); + const auto MAX_WIDTH = IMAGE_WIDTH - TEXT_WIDTH; svg_begin(IMAGE_WIDTH, IMAGE_HEIGHT); double top = 0; + size_t max_count = bins[0]; + for (size_t bin : bins) { + if (max_count < bin) { + max_count = bin; + } + } + for (size_t bin : bins) { - double bin_width = BLOCK_WIDTH * bin; + const double bin_width = MAX_WIDTH * (static_cast(bin) / max_count); svg_text(TEXT_LEFT, top + TEXT_BASELINE, to_string(bin)); - svg_rect(TEXT_WIDTH, top, bin_width, BIN_HEIGHT, "blue", "#aaaaff"); + svg_rect(TEXT_WIDTH, top, bin_width, BIN_HEIGHT, "black", "red"); top += BIN_HEIGHT; } diff --git a/svg.h b/svg.h index c771318..7c9f474 100644 --- a/svg.h +++ b/svg.h @@ -1,11 +1,8 @@ -#pragma once -#include -#include +#ifndef SVG_H_INCLUDED +#define SVG_H_INCLUDED -void svg_begin(double width, double height); -void svg_end(); -void svg_text(double left, double baseline, const std::string& text); -void svg_rect(double x, double y, double width, double height, - const std::string& stroke = "black", - const std::string& fill = "black"); +#include +#include // void show_histogram_svg(const std::vector& bins); + +#endif // SVG_H_INCLUDED diff --git a/text.cpp b/text.cpp index f90cead..9f8573c 100644 --- a/text.cpp +++ b/text.cpp @@ -1,33 +1,37 @@ #include "text.h" #include - using namespace std; -void show_histogram_text(const vector& bins) { +void show_histogram_text(vector bins, size_t bin_count) { const size_t SCREEN_WIDTH = 80; const size_t MAX_ASTERISK = SCREEN_WIDTH - 3 - 1; - - size_t max_count = 0; - for (size_t count : bins) { - if (count > max_count) { - max_count = count; + double max_bin; + size_t height; + max_bin = bins[0]; + height = bins[0]; + for (size_t i = 0; i < bin_count; i++) { + if (bins[i] > max_bin) { + max_bin = bins[i]; } } - for (size_t bin : bins) { - if (bin < 100) cout << " "; - if (bin < 10) cout << " "; - cout << bin << "|"; - - size_t height = bin; - if (max_count > MAX_ASTERISK) { - if (max_count != bin) - height = MAX_ASTERISK * (static_cast(bin) / max_count); - else - height = MAX_ASTERISK; + bool flag = false; + if (max_bin > 80) { + flag = true; + } + for (size_t i = 0; i < bin_count; i++) { + if (bins[i] < 100) cout << " "; + if (bins[i] < 10) cout << " "; + cout << bins[i] << "|"; + if (flag == true) { + height = MAX_ASTERISK * (static_cast(bins[i]) / max_bin); } - - for (size_t i = 0; i < height; i++) cout << "*"; - cout << "\n"; + else { + height = bins[i]; + } + for (size_t j = 0; j < height; j++) { + cout << "*"; + } + cout << endl; } } diff --git a/text.h b/text.h index 0407a22..f5c2730 100644 --- a/text.h +++ b/text.h @@ -1,4 +1,9 @@ -#pragma once +#ifndef TEXT_H_INCLUDED +#define TEXT_H_INCLUDED + #include +using namespace std; + +void show_histogram_text(vector bins, size_t bin_count); -void show_histogram_text(const std::vector& bins); +#endif diff --git a/unittest.cpp b/unittest.cpp index 7f44016..bf37c5e 100644 --- a/unittest.cpp +++ b/unittest.cpp @@ -1,3 +1,4 @@ +#define DOCTEST_CONFIG_NO_MULTITHREADING #define DOCTEST_CONFIG_IMPLEMENT_WITH_MAIN #include "doctest.h" #include "histogram_internal.h" @@ -10,28 +11,28 @@ TEST_CASE("distinct positive numbers") { CHECK(max == 2); } -TEST_CASE("all numbers are same") { + +TEST_CASE("distinct negative numbers"){ double min = 0; double max = 0; - find_minmax({5, 5, 5, 5}, min, max); - CHECK(min == 5); - CHECK(max == 5); + find_minmax({-1, -2}, min, max); + CHECK(min == -2); + CHECK(max == -1); } - -TEST_CASE("with negative numbers") { +TEST_CASE("vector of the same elements"){ double min = 0; double max = 0; - find_minmax({-3, -7, -1}, min, max); - CHECK(min == -7); - CHECK(max == -1); + find_minmax({3,3,3}, min, max); + CHECK(min == 3); + CHECK(max == 3); } -TEST_CASE("single number") { +TEST_CASE("single element vector") { double min = 0; double max = 0; - find_minmax({42}, min, max); - CHECK(min == 42); - CHECK(max == 42); + find_minmax({7}, min, max); + CHECK(min == 7); + CHECK(max == 7); } TEST_CASE("empty vector") {