commit a23367aaf1d9498a98a44804235f0a3d6458cec1 Author: Ilya (OgarkovIA) Date: Sun Oct 27 15:40:48 2024 +0300 code: заготовка кода diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..55ef8f6 --- /dev/null +++ b/.gitignore @@ -0,0 +1,5 @@ +cs-lab34.cbp +cs-lab34.depend +unittest.cbp +/bin +/obj diff --git a/cs-lab34.layout b/cs-lab34.layout new file mode 100644 index 0000000..423caf5 --- /dev/null +++ b/cs-lab34.layout @@ -0,0 +1,10 @@ + + + + + + + + + + diff --git a/histogram.cpp b/histogram.cpp new file mode 100644 index 0000000..498dc66 --- /dev/null +++ b/histogram.cpp @@ -0,0 +1,43 @@ +#include "histogram.h" +#include + +void find_minmax(const std::vector &numbers, double &min, double &max, bool &res ){ + + if (numbers.size()==0){ + res = true; + return; + } + + min = numbers[0]; + max = numbers[0]; + for ( double x : numbers ){ + if ( x < min ){ + min = x; + } else if ( x > max ){ + max = x; + } + } +} + +std::vector make_histogram(const std::vector &numbers, std::size_t bin_count) { + double min, max; + bool res = false; + find_minmax(numbers, min, max, res); + double bin_size = ( max - min ) / bin_count; + std::vector bins ( bin_count ); + for (std::size_t i=0; i < numbers.size(); i++ ){ + bool found = false; + for (std::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]++; + } + } + return bins; +} diff --git a/histogram.h b/histogram.h new file mode 100644 index 0000000..0d8dc8c --- /dev/null +++ b/histogram.h @@ -0,0 +1,9 @@ +#ifndef HISTOGRAM_H_INCLUDED +#define HISTOGRAM_H_INCLUDED + +#include + +std::vector +make_histogram(const std::vector& numbers, size_t bin_count); + +#endif // HISTOGRAM_H_INCLUDED diff --git a/main.cpp b/main.cpp new file mode 100644 index 0000000..7ae72b6 --- /dev/null +++ b/main.cpp @@ -0,0 +1,52 @@ +#include +#include +#include "histogram.h" +#include "text.h" +#include "svg.h" + +using namespace std; + + +struct Input { + vector numbers; + size_t bin_count{}; +}; + +Input input_data() { + + size_t number_count; + + cerr << "Enter number count: "; + cin >> number_count; + + Input in; + + cerr << "Enter number of bins: "; + cin >> in.bin_count; + + in.numbers.resize(number_count); + + cerr << "Enter numbers: "; + for (size_t i = 0; i < number_count; i++) + { + cin >> in.numbers[i]; + } + + return in; +} + + + + +int main(){ + + bool res = false; + + Input in = input_data(); + + auto bins = make_histogram(in.numbers, in.bin_count); + + show_histogram_svg(bins); + + + } diff --git a/svg.cpp b/svg.cpp new file mode 100644 index 0000000..dbee47e --- /dev/null +++ b/svg.cpp @@ -0,0 +1,67 @@ +#include +#include +#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::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 std::size_t MAX_ASTERISK = IMAGE_WIDTH - TEXT_WIDTH; + svg_begin(IMAGE_WIDTH, IMAGE_HEIGHT); + double top = 0; + + std::size_t maxbin=bins[0]; + for (std::size_t i=0; i < bins.size(); i++){ + if (maxbin < bins[i]){ + maxbin=bins[i]; + } + } + + if (maxbin<=76){ + for (std::size_t i=0; i < bins.size(); i++) { + 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); + top += BIN_HEIGHT; + } + svg_end(); + } else { + for (std::size_t i=0; i < bins.size(); i++) { + double bin_width= MAX_ASTERISK * (static_cast(bins[i]) / maxbin); + svg_text(TEXT_LEFT, top + TEXT_BASELINE, std::to_string(bins[i])); + svg_rect(TEXT_WIDTH, top, bin_width, BIN_HEIGHT); + top += BIN_HEIGHT; + } + svg_end(); + } +} diff --git a/svg.h b/svg.h new file mode 100644 index 0000000..c7da98d --- /dev/null +++ b/svg.h @@ -0,0 +1,9 @@ +#ifndef SVG_H_INCLUDED +#define SVG_H_INCLUDED + +#include + +void +show_histogram_svg(const std::vector& bins); + +#endif // SVG_H_INCLUDED diff --git a/text.cpp b/text.cpp new file mode 100644 index 0000000..1ca3e67 --- /dev/null +++ b/text.cpp @@ -0,0 +1,45 @@ +#include +#include +#include "text.h" + +const std::size_t SCREEN_WIDTH = 80; +const std::size_t MAX_ASTERISK = SCREEN_WIDTH - 3 - 1; + +void show_histogram_text(const std::vector &bins){ + std::size_t maxbin = bins[0]; + for (std::size_t i=1; i < bins.size(); i++){ + if (maxbin < bins[i]){ + maxbin = bins[i]; + } + } + if (maxbin <= MAX_ASTERISK){ + for (size_t i=0; i < bins.size(); i++ ){ + if (bins[i] < 10 ){ + std::cout << " " << bins[i] << "|"; + } else if (bins[i] < 100) { + std::cout << " " << bins[i] << "|"; + } else if (bins[i] < 1000) { + std::cout << bins[i] << "|"; + } + for (size_t j=0; j < bins[i]; j++ ){ + std::cout << "*"; + } + std::cout << std::endl; + } + } else { + for (std::size_t i=0; i < bins.size(); i++){ + std::size_t heightG= MAX_ASTERISK * (static_cast(bins[i]) / maxbin); + if (bins[i] < 10 ){ + std::cout << " " << bins[i] << "|"; + } else if (bins[i] < 100) { + std::cout << " " << bins[i] << "|"; + } else if (bins[i] < 1000) { + std::cout << bins[i] << "|"; + } + for (std::size_t j=0; j < heightG; j++ ){ + std::cout << "*"; + } + std::cout << std::endl; + } + } +} diff --git a/text.h b/text.h new file mode 100644 index 0000000..86bd41e --- /dev/null +++ b/text.h @@ -0,0 +1,8 @@ +#ifndef TEXT_H_INCLUDED +#define TEXT_H_INCLUDED + +#include + +void show_histogram_text(const std::vector &bins); + +#endif // TEXT_H_INCLUDED