diff --git a/CommonVar.cpp b/CommonVar.cpp index 9bcb988..2e44fe9 100644 --- a/CommonVar.cpp +++ b/CommonVar.cpp @@ -1,75 +1,43 @@ #include #include #include +#include "histogram.h" +#include "text.h" +#include "svg.h" using namespace std; -int main() { - const size_t SCREEN_WIDTH = 80; - const size_t MAX_ASTERISK = SCREEN_WIDTH - 3 - 1; - double maxx, minn, MaxCount = 0; - size_t NumberCount, BinCount; +struct Input { + vector Numbers; + size_t BinCount{}; +}; + +Input +InputData() { + Input in; + size_t NumberCount; cerr << "Enter number count: "; cin >> NumberCount; cerr << "Enter bin count: "; - cin >> BinCount; - vector Numbers(NumberCount); + cin >> in.BinCount; + in.Numbers.resize(NumberCount); for (int i = 0; i < NumberCount; i++) { - cin >> Numbers[i]; - } - vector Bins(BinCount); - maxx = Numbers[0]; - minn = Numbers[0]; - for (auto x : Numbers) { - if (x > maxx) { - maxx = x; - } - if (x < minn) { - minn = x; - } + cin >> in.Numbers[i]; } - double BinSize = (maxx - minn) / BinCount; + return in; +} - for (size_t i = 0; i < NumberCount; i++) { - bool found = false; - for (size_t j = 0; (j < BinCount - 1) && !found; j++) { - auto lo = minn + j * BinSize; - auto hi = minn + (j + 1) * BinSize; - if ((lo <= Numbers[i]) && (Numbers[i] < hi)) { - Bins[j]++; - found = true; - if (Bins[j] > MaxCount) { - MaxCount = Bins[j]; - } - } - } - if (!found) { - Bins[BinCount - 1]++; - if (Bins[BinCount - 1] > MaxCount) { - MaxCount = Bins[BinCount - 1]; - } - } - } - for (auto bin : Bins) { - if (bin < 100) { - cout << " "; - } - if (bin < 10) { - cout << " "; - } - cout << bin << "|"; - size_t height = MAX_ASTERISK * (static_cast(bin) / MaxCount); - if (MaxCount <= 76) { - for (int i = 0; i < bin; i++) { - cout << '*'; - } - } - else { - for (int i = 0; i < height; i++) { - cout << '*'; - } - } - cout << endl; - } +void FindMinMax(const vector& Numbers, double& minn, double& maxx); + +vector MakeHistogram(vector Numbers, size_t BinCount, double& MaxCount); + +void ShowHistogrammText(vector Bins); + +int main() { + double MaxCount = 0; + Input in = InputData(); + vector Bins(in.BinCount); + Bins = MakeHistogram(in.Numbers, in.BinCount, MaxCount); + ShowHistogramSvg(Bins); return 0; } diff --git a/histogram.cpp b/histogram.cpp new file mode 100644 index 0000000..7109f5d --- /dev/null +++ b/histogram.cpp @@ -0,0 +1,43 @@ +#include "histogram.h" +#include +using namespace std; +void FindMinMax(const vector& Numbers, double& minn, double& maxx) { + maxx = Numbers[0]; + minn = Numbers[0]; + for (auto x : Numbers) { + if (x > maxx) { + maxx = x; + } + if (x < minn) { + minn = x; + } + } +} + +vector MakeHistogram(vector Numbers, size_t BinCount, double& MaxCount) { + double maxx, minn; + FindMinMax(Numbers, minn, maxx); + double BinSize = (maxx - minn) / BinCount; + vector Bins(BinCount); + for (size_t i = 0; i < Numbers.size(); i++) { + bool found = false; + for (size_t j = 0; (j < BinCount - 1) && !found; j++) { + auto lo = minn + j * BinSize; + auto hi = minn + (j + 1) * BinSize; + if ((lo <= Numbers[i]) && (Numbers[i] < hi)) { + Bins[j]++; + found = true; + if (Bins[j] > MaxCount) { + MaxCount = Bins[j]; + } + } + } + if (!found) { + Bins[BinCount - 1]++; + if (Bins[BinCount - 1] > MaxCount) { + MaxCount = Bins[BinCount - 1]; + } + } + } + return Bins; +} \ No newline at end of file diff --git a/histogram.h b/histogram.h new file mode 100644 index 0000000..a6ab3ae --- /dev/null +++ b/histogram.h @@ -0,0 +1,4 @@ +#pragma once +#include +std::vector +MakeHistogram(const std::vector Numbers, size_t BinCount, double& MaxCount); \ No newline at end of file diff --git a/histogram_internal.h b/histogram_internal.h new file mode 100644 index 0000000..81c293d --- /dev/null +++ b/histogram_internal.h @@ -0,0 +1,3 @@ +#pragma once +#include +void FindMinMax(const std::vector& Numbers, double& minn, double& maxx); \ No newline at end of file diff --git a/svg.cpp b/svg.cpp new file mode 100644 index 0000000..7fba8f8 --- /dev/null +++ b/svg.cpp @@ -0,0 +1,64 @@ +#include +#include "svg.h" +#include +#include +using namespace std; + +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; + +void +SvgBegin(double width, double height) { + cout << "\n"; + cout << "\n"; +} + +void +SvgEnd() { + cout << "\n"; +} + +void +SvgText(double left, double baseline, string text) { + cout << "" << text << "\n"; +} + +void SvgRect(double x, double y, double width, double height, string stroke = "black", string fill = "black") { + cout << "\n"; +} + +void +ShowHistogramSvg(const vector& bins) { + double top = 0; + SvgBegin(400, 300); + + size_t MaxCount = 0; + for (auto bin : bins) { + if (MaxCount < bin) { + MaxCount = bin; + } + } + int k = (IMAGE_WIDTH - TEXT_WIDTH) / MaxCount * BLOCK_WIDTH; + + if (k > 1) k = 1; + + for (size_t bin : bins) { + bin = bin * k; + const double bin_width = BLOCK_WIDTH * bin; + SvgText(TEXT_LEFT, top + TEXT_BASELINE, to_string(bin)); + SvgRect(TEXT_WIDTH, top, bin_width, BIN_HEIGHT, "red", "#FF69B4"); + top += BIN_HEIGHT; + } + SvgEnd(); +} + + diff --git a/svg.h b/svg.h new file mode 100644 index 0000000..7bda522 --- /dev/null +++ b/svg.h @@ -0,0 +1,6 @@ +#pragma once +#include + +void +ShowHistogramSvg(const std::vector& bins); + diff --git a/text.cpp b/text.cpp new file mode 100644 index 0000000..e28158e --- /dev/null +++ b/text.cpp @@ -0,0 +1,39 @@ +#include "text.h" +#include +using namespace std; +const size_t SCREEN_WIDTH = 80; +const size_t MAX_ASTERISK = SCREEN_WIDTH - 3 - 1; + +void ShowHistogrammText(vector Bins) { + size_t MaxCount = 0; + for (auto bin : Bins) { + if (MaxCount < bin) { + MaxCount = bin; + } + } + for (auto bin : Bins) { + if (bin < 100) { + cout << " "; + } + if (bin < 10) { + cout << " "; + } + cout << bin << "|"; + size_t height = MAX_ASTERISK * (static_cast(bin) / MaxCount); + if (MaxCount <= 76) { + for (int i = 0; i < bin; i++) { + cout << '*'; + } + } + else { + for (int i = 0; i < height; i++) { + cout << '*'; + } + } + cout << endl; + } +} + + + + \ No newline at end of file diff --git a/text.h b/text.h new file mode 100644 index 0000000..36a7f7e --- /dev/null +++ b/text.h @@ -0,0 +1,6 @@ +#pragma once +#include + + +void +ShowHistogrammText(std::vector Bins); \ No newline at end of file