From 6d92425a8c003843ebfc0f1ba3b9236ce786a67a Mon Sep 17 00:00:00 2001 From: Stepan Siniavskii Date: Mon, 5 Jun 2023 10:51:03 +0300 Subject: [PATCH] =?UTF-8?q?=D0=94=D0=BE=D0=B1=D0=B0=D0=B2=D0=B8=D0=BB=20?= =?UTF-8?q?=D1=84=D0=B0=D0=B9=D0=BB=D1=8B=20=D0=BF=D1=80=D0=BE=D0=B5=D0=BA?= =?UTF-8?q?=D1=82=D0=B0?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .gitignore | 34 ++++++++++++++++++++++++ histogram.cpp | 73 +++++++++++++++++++++++++++++++++++++++++++++++++++ histogram.h | 13 +++++++++ lab34.cbp | 40 ++++++++++++++++++++++++++++ lab34.depend | 21 +++++++++++++++ main.cpp | 39 +++++++++++++++++++++++++++ svg.cpp | 63 ++++++++++++++++++++++++++++++++++++++++++++ svg.h | 15 +++++++++++ text.h | 13 +++++++++ 9 files changed, 311 insertions(+) create mode 100644 .gitignore create mode 100644 histogram.cpp create mode 100644 histogram.h create mode 100644 lab34.cbp create mode 100644 lab34.depend create mode 100644 main.cpp create mode 100644 svg.cpp create mode 100644 svg.h create mode 100644 text.h diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..c356251 --- /dev/null +++ b/.gitignore @@ -0,0 +1,34 @@ +# Prerequisites +*.d + +# Compiled Object files +*.slo +*.lo +*.o +*.obj + +# Precompiled Headers +*.gch +*.pch + +# Compiled Dynamic libraries +*.so +*.dylib +*.dll + +# Fortran module files +*.mod +*.smod + +# Compiled Static libraries +*.lai +*.la +*.a +*.lib + +# Executables +*.exe +*.out +*.app + +/curl diff --git a/histogram.cpp b/histogram.cpp new file mode 100644 index 0000000..bf16329 --- /dev/null +++ b/histogram.cpp @@ -0,0 +1,73 @@ +#include "histogram.h" +#include "vector" + +using namespace std; + + +vector make_histogram(const vector& numbers, size_t bin_count) { + + vector bins(bin_count); + vector binss(bin_count); + + double max, min; + find_minmax(numbers, min, max); + double bin_size = (max / min) / bin_count; + + 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]++; + found = true; + } + } + if (!found) { + bins[bin_count - 1]++; + } + } + + int max_count = bins[0]; + for (size_t i = 0; i < bin_count; i++) { + if (bins[i] > max_count) { + max_count = bins[i]; + } + } + + if (max_count > 76) { + + for (size_t i = 0; i < bin_count; i++) { + int count = bins[i]; + size_t height = 76 * (static_cast(count) / max_count); + bins[i] = height; + } + } + return bins; +} + +bool find_minmax(const vector& numbers, double& min, double& max) { + if (numbers.size() == 0) { + return false; + } + for (auto i = 0; i < numbers.size(); i++) { + if ( + numbers[i] < 0 + ) return false; + } + + min = numbers[0]; + for (auto i = 0; i < numbers.size(); i++) { + if (numbers[i] < min) { + min = numbers[i]; + } + } + + max = numbers[0]; + for (auto i = 0; i < numbers.size(); i++) { + if (numbers[i] > max) { + max = numbers[i]; + } + } + return true; +} \ No newline at end of file diff --git a/histogram.h b/histogram.h new file mode 100644 index 0000000..64fd542 --- /dev/null +++ b/histogram.h @@ -0,0 +1,13 @@ +#include +#pragma once + +#ifndef LAB4_HISTOGRAM_H +#define LAB4_HISTOGRAM_H + +#endif //LAB4_HISTOGRAM_H + +#include +#include + +std::vector make_histogram(const std::vector& numbers, size_t bin_count); +bool find_minmax(const std::vector& numbers, double& min, double& max); \ No newline at end of file diff --git a/lab34.cbp b/lab34.cbp new file mode 100644 index 0000000..0c0e7e4 --- /dev/null +++ b/lab34.cbp @@ -0,0 +1,40 @@ + + + + + + diff --git a/lab34.depend b/lab34.depend new file mode 100644 index 0000000..ca1501c --- /dev/null +++ b/lab34.depend @@ -0,0 +1,21 @@ +# depslib dependency file v1.0 +1684755117 source:c:\users\stepa\desktop\lab04\histogram.cpp + "histogram.h" + "vector" + +1684755161 c:\users\stepa\desktop\lab04\histogram.h + + + + +1684755057 source:c:\users\stepa\desktop\lab04\svg.cpp + "svg.h" + + + + "cmath" + +1684757154 c:\users\stepa\desktop\lab04\svg.h + + + diff --git a/main.cpp b/main.cpp new file mode 100644 index 0000000..af1f15e --- /dev/null +++ b/main.cpp @@ -0,0 +1,39 @@ +#include +#include "vector" +#include "svg.h" +#include "histogram.h" +//#include "curl/curl.h" +using namespace std; + + + + +struct Input { + vector numbers; + size_t bin_count{}; +}; + + +Input +input_data(istream& inn, bool promt) { + size_t number_count; + cin >> number_count; + Input in; + in.numbers.resize(number_count); + + for (size_t i = 0; i < number_count; i++) { + cin >> in.numbers[i]; + } + + cin >> in.bin_count; + return in; +} + +int main() { +// curl_global_init(CURL_GLOBAL_ALL); + auto in = input_data(cin, true); + auto bins = make_histogram(in.numbers, in.bin_count); + show_histogram_text(bins, in.bin_count); + //show_histogram_svg(bins); + return 0; +} diff --git a/svg.cpp b/svg.cpp new file mode 100644 index 0000000..9cb12c3 --- /dev/null +++ b/svg.cpp @@ -0,0 +1,63 @@ +#include "svg.h" +#include +#include +#include +#include "cmath" + +using namespace std; + +void +svg_begin(double width, double height) { + cout << "\n"; + cout << "\n"; +} + +void +svg_end() { + cout << "\n"; +} + +int color(int bin, int max_count) { + int color; + return color = round((10 - (bin * 9) / max_count)); +} + +void +svg_text(double left, double baseline, string text) { + cout << " " << text << ""; +} + +void svg_rect(double x, double y, double width, double height, string stroke, string fill) { + cout << "" << ""; +} + +void show_histogram_svg(const 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; + + + svg_begin(400, 300); + double top = 0; + double max_count = bins[0]; + for (size_t i = 0; i < bins.size(); i++) { + if (bins[i] > max_count) + max_count = bins[i]; + } + for (size_t bin : bins) { + int temp_color = color(bin, max_count); + const double bin_width = (IMAGE_WIDTH - TEXT_WIDTH) * (bin / max_count); + svg_text(TEXT_LEFT, top + TEXT_BASELINE, to_string(bin)); + svg_rect(TEXT_WIDTH, top, bin_width, BIN_HEIGHT, to_string(temp_color), to_string(temp_color)); + top += BIN_HEIGHT; + } + svg_end(); +} \ No newline at end of file diff --git a/svg.h b/svg.h new file mode 100644 index 0000000..6082a20 --- /dev/null +++ b/svg.h @@ -0,0 +1,15 @@ + + +#include +#include + +#ifndef LAB4_SVG_H +#define LAB4_SVG_H + +#endif //LAB4_SVG_H + +void svg_begin(double width, double height); +void svg_end(); +void svg_text(double left, double baseline, std::string text); +void svg_rect(double x, double y, double width, double height, std::string stroke, std::string fill); +void show_histogram_svg(const std::vector& bins); diff --git a/text.h b/text.h new file mode 100644 index 0000000..b588a53 --- /dev/null +++ b/text.h @@ -0,0 +1,13 @@ +// +// Created by stepa on 22.05.2023. +// + +#include +#include + +#ifndef LAB4_TEXT_H +#define LAB4_TEXT_H + +#endif //LAB4_TEXT_H + +void show_histogram_text(std::vector bins, size_t bin_count); \ No newline at end of file