From 2be18e6286301441f5ad2e5879805f6178824b5b Mon Sep 17 00:00:00 2001 From: MachulinaDV Date: Tue, 25 Apr 2023 01:03:21 +0300 Subject: [PATCH] code: main --- histogram.cpp | 68 +++++++++++++++++++++++++++++++++++++++++++++++++++ histogram.h | 9 +++++++ main.cpp | 42 +++++++++++++++++++++++++++++++ svg.cpp | 64 ++++++++++++++++++++++++++++++++++++++++++++++++ svg.h | 8 ++++++ text.cpp | 29 ++++++++++++++++++++++ text.h | 6 +++++ 7 files changed, 226 insertions(+) create mode 100644 histogram.cpp create mode 100644 histogram.h create mode 100644 main.cpp 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 new file mode 100644 index 0000000..4198d98 --- /dev/null +++ b/histogram.cpp @@ -0,0 +1,68 @@ +#include +#include +#include +#include "histogram.h" + + +using namespace std; + +bool find_minmax(const vector& A, double& min, double& max) +{ + bool empt; + + if (A.size() != 0) + { + + min = A[0]; + for (auto i = 0; i < A.size(); i++) + { + if (A[i] < min) + { + min = A[i]; + } + } + + max = A[0]; + for (auto i = 0; i < A.size(); i++) + { + if (A[i] > max) + { + max = A[i]; + } + } + empt =true; + } + else + empt = false; + return empt; +} + + +vector make_histogram(const vector& A, size_t bin) +{ + vectorB(bin); + size_t max_count; + double max, min; + bool empt; + find_minmax(A, min, max); + double step = (max - min) / (bin); + + for (size_t i = 0; i < A.size(); i++) + { + for (size_t j = 0; j < bin; j++) + { + if ((A[i] >= (min + j * step)) && (A[i] < (min + (j + 1)*step))) + { + B[j]++; + break; + } + } + } + + for (size_t i = 0; i < A.size(); i++) + { + if (A[i] == max) + B[bin - 1]++; + } + return B; +} diff --git a/histogram.h b/histogram.h new file mode 100644 index 0000000..f06c41f --- /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& A, size_t bin); + +#endif // HISTOGRAM_H_INCLUDED diff --git a/main.cpp b/main.cpp new file mode 100644 index 0000000..1d7ec2d --- /dev/null +++ b/main.cpp @@ -0,0 +1,42 @@ +#include +#include +#include +#include "histogram.h" +#include "text.h" +#include "svg.h" + +using namespace std; + + struct Input { + vectorA; + size_t bin{}; + }; + +Input +input_data() +{ + size_t n; + cerr<<"Marks: "; + cin>>n; + + Input in; + in.A.resize(n); + for (size_t i=0; i>in.bin; + return in; +} + +int main() +{ + Input in = input_data(); + auto B = make_histogram(in.A, in.bin); + show_histogram_svg (B); + + return 0; +} diff --git a/svg.cpp b/svg.cpp new file mode 100644 index 0000000..ecfae83 --- /dev/null +++ b/svg.cpp @@ -0,0 +1,64 @@ +#include +#include +#include +#include +#include +#include "svg.h" +using namespace std; + +void +svg_begin(double width, double height) { + cout << "\n"; + cout << "\n"; +} + +void +svg_end() { + cout << "\n"; +} +void +svg_text(double left, double baseline, string text) { + cout << "" << text << ""; +} + +void +svg_rect(double x, double y, double width, double height, string opacity , string stroke, string fill) { +cout << ""; + +} + + +void +show_histogram_svg(const vector& B) { + 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 MAX_WIDTH = IMAGE_WIDTH-TEXT_WIDTH; + + + svg_begin(IMAGE_WIDTH,IMAGE_HEIGHT); + + double top = 0; + double max_count = B[0]; + for (size_t i = 0; i < B.size(); i++) { + if (B[i] > max_count) + max_count = B[i]; + } + for (size_t bin : B) { + double opacity = (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(opacity), "blue", "#9BB0D2"); + top += BIN_HEIGHT; + } + + svg_end(); +} diff --git a/svg.h b/svg.h new file mode 100644 index 0000000..d269374 --- /dev/null +++ b/svg.h @@ -0,0 +1,8 @@ +#ifndef SVG_H_INCLUDED +#define SVG_H_INCLUDED + +#include +void +show_histogram_svg(const std::vector& B); + +#endif // SVG_H_INCLUDED diff --git a/text.cpp b/text.cpp new file mode 100644 index 0000000..d4f1174 --- /dev/null +++ b/text.cpp @@ -0,0 +1,29 @@ +#include +#include +#include +#include "text.h" + +using namespace std; + +void show_histogram(vectorB, size_t bin) +{ + for (size_t i = 0; i < bin; i++) + { + if (B[i] < 10) + { + cout << " "; + } + else if (B[i] < 100) + { + cout << " "; + } + cout << B[i] << "|"; + for (size_t j = 0; j < B[i]; j++) + { + cout << "*"; + } + cout << endl; + } + return; +} + diff --git a/text.h b/text.h new file mode 100644 index 0000000..5875d32 --- /dev/null +++ b/text.h @@ -0,0 +1,6 @@ +#pragma once + +#include + +void show_histogram(std::vectorB, size_t bin); +