From efadf5adc73657af9e1c5627c12bb7561779db6e Mon Sep 17 00:00:00 2001
From: DobrovolskaY <DobrovolskaY@mpei.ru>
Date: Tue, 23 Apr 2024 19:51:36 +0200
Subject: [PATCH] 3 (4): devide programm on files

---
 histogram.cpp | 45 +++++++++++++++++++++++++++++++++++++++++++++
 histogram.h   |  5 +++++
 text.cpp      | 38 ++++++++++++++++++++++++++++++++++++++
 text.h        |  5 +++++
 4 files changed, 93 insertions(+)
 create mode 100644 histogram.cpp
 create mode 100644 histogram.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..ec14323
--- /dev/null
+++ b/histogram.cpp
@@ -0,0 +1,45 @@
+#include <iostream>
+#include <vector>
+#include <cmath>
+#include "histogram.h"
+using namespace std;
+
+
+static void find_minmax(const vector<double>& numbers, double& mini, double& maxi) {
+
+    mini = numbers[0];
+    maxi = numbers[0];
+    for (double x : numbers) {
+        if (x < mini) {
+            mini = x;
+        }
+        else if (x > maxi) {
+            maxi = x;
+        }
+    }
+}
+vector<size_t>
+make_histogram(const vector<double>& numbers, size_t bin_count) {
+    double mini, maxi;
+    find_minmax(numbers, mini, maxi);
+
+    vector<size_t> bins(bin_count);
+
+    double bin_size = (maxi - mini) / bin_count;
+    for (double num : numbers) {
+        bool found = false;
+        for (size_t j = 0; (j < bin_count - 1) && !found; j++){
+            auto lo = mini + j * bin_size;
+            auto hi = mini + (j + 1) * bin_size;
+            if ((lo <= num) && (num < 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..a86c9a3
--- /dev/null
+++ b/histogram.h
@@ -0,0 +1,5 @@
+#pragma once
+#include <vector>
+
+std::vector<size_t>
+make_histogram(const std::vector<double>& numbers, size_t bin_count);
diff --git a/text.cpp b/text.cpp
new file mode 100644
index 0000000..37fe5c2
--- /dev/null
+++ b/text.cpp
@@ -0,0 +1,38 @@
+#include <iostream>
+#include <vector>
+#include <cmath>
+#include "text.h"
+using namespace std;
+
+void
+show_histogram_text(const vector<size_t>& bins, size_t bin_count) {
+    size_t max_count = bins[0];
+    for (size_t i = 0; i < bin_count; i++) {
+        if (bins[i] > max_count) {
+            max_count = bins[i];
+        }
+    }
+
+    vector<size_t> height(bin_count);
+    for (size_t i = 0; i < bin_count; i++) {
+        if (max_count > MAX_ASTERISK) {
+            height[i] = round(MAX_ASTERISK * (static_cast<double>(bins[i]) / max_count));
+        } else {
+            height[i] = bins[i];
+        }
+    }
+
+    for (size_t i = 0; i < bin_count; i++) {
+        if (bins[i] < 10) {
+            cout << "  ";
+        } else if (bins[i] < 100) {
+            cout << " ";
+        }
+        cout << bins[i] << "| ";
+
+        for (size_t j = 0; j < height[i]; j++) {
+            cout << " * ";
+        }
+        cout << endl;
+    }
+}
diff --git a/text.h b/text.h
new file mode 100644
index 0000000..db04afe
--- /dev/null
+++ b/text.h
@@ -0,0 +1,5 @@
+#pragma once
+#include <vector>
+
+void
+show_histogram_text(const vector<size_t>& bins, size_t bin_count);