From 5623cec915cd847c1348a20095b01f895812f492 Mon Sep 17 00:00:00 2001
From: "Bob (ZharkovIG)" <ZharkovIG@mpei.ru>
Date: Sat, 19 Oct 2024 21:23:37 +0300
Subject: [PATCH] =?UTF-8?q?build:=20=D0=B2=D1=8B=D0=B4=D0=B5=D0=BB=D0=B5?=
 =?UTF-8?q?=D0=BD=D0=B8=D0=B5=20=D0=B2=20=D0=BE=D1=82=D0=B4=D0=B5=D0=BB?=
 =?UTF-8?q?=D1=8C=D0=BD=D1=8B=D0=B5=20=D1=84=D0=B0=D0=B9=D0=BB=D1=8B=20?=
 =?UTF-8?q?=D1=84=D1=83=D0=BD=D0=BA=D1=86=D0=B8=D0=B8=20show=5Fhistogram?=
 =?UTF-8?q?=5Ftext()?=
MIME-Version: 1.0
Content-Type: text/plain; charset=UTF-8
Content-Transfer-Encoding: 8bit

---
 main.cpp | 38 ++------------------------------------
 text.cpp | 36 ++++++++++++++++++++++++++++++++++++
 text.h   | 12 ++++++++++++
 3 files changed, 50 insertions(+), 36 deletions(-)
 create mode 100644 text.cpp
 create mode 100644 text.h

diff --git a/main.cpp b/main.cpp
index b0925aa..e933a88 100644
--- a/main.cpp
+++ b/main.cpp
@@ -1,11 +1,9 @@
 #include <iostream>
 #include <vector>
 #include "histogram.h"
+#include "text.h"
 using namespace std;
 
-const size_t SCREEN_WIDTH = 80;
-const size_t MAX_ASTERISK = SCREEN_WIDTH - 3 - 1;
-
 struct Input {
     vector<double> numbers;
     size_t bin_count{};
@@ -31,38 +29,6 @@ input_data(){
     return in;
 }
 /*
-void
-find_minmax(vector<double>& numbers, double& min, double& max){
-    min = numbers[0];
-    max = numbers[0];
-    for (double x : numbers) {
-        if (x < min) {
-            min = x;
-        }
-        else if (x > max) {
-            max = x;
-        }
-    }
-}
-
-vector<size_t> make_histogram(vector<double>& numbers, size_t bin_count) {
-    double min, max;
-    find_minmax(numbers, min, max);
-
-    vector<size_t> bins(bin_count, 0);
-    double bin_size = (max - min) / bin_count;
-
-    for (double number : numbers) {
-        size_t bin_index = bin_count - 1;  // default to last bin
-        if (number < max) {
-            bin_index = static_cast<size_t>((number - min) / bin_size);
-        }
-        bins[bin_index]++;
-    }
-
-    return bins;
-}*/
-
 void
 show_histogram_text(const vector<size_t>& bins){
     size_t i;
@@ -97,7 +63,7 @@ show_histogram_text(const vector<size_t>& bins){
             cout<<bin<<endl;
         }
     }
-}
+}*/
 
 int
 main()
diff --git a/text.cpp b/text.cpp
new file mode 100644
index 0000000..68b8db1
--- /dev/null
+++ b/text.cpp
@@ -0,0 +1,36 @@
+#include "text.h"
+#include <iostream>
+
+void
+show_histogram_text(std::vector<size_t>& bins){
+    size_t max_count = 0;
+    for (size_t x: bins) {
+        if (x > max_count) {
+            max_count = x;
+        }
+    }
+    if (max_count > MAX_ASTERISK) {
+        for(size_t bin:bins){
+            size_t height = bin;
+            height = MAX_ASTERISK * (static_cast<double>(bin) / max_count);
+            for(size_t i = 0; i < height; i++){
+                std::cout<<"*";
+            }
+            std::cout<<"|";
+            if(bin<100) std::cout<<" ";
+            if(bin<10) std::cout<<" ";
+            std::cout<<bin<<std::endl;
+        }
+    } else {
+        for(size_t bin:bins){
+            size_t height = bin;
+            for(size_t i = 0; i < height; i++){
+                std::cout<<"*";
+            }
+            std::cout<<"|";
+            if(bin<100) std::cout<<" ";
+            if(bin<10) std::cout<<" ";
+            std::cout<<bin<<std::endl;
+        }
+    }
+}
diff --git a/text.h b/text.h
new file mode 100644
index 0000000..cf43159
--- /dev/null
+++ b/text.h
@@ -0,0 +1,12 @@
+#ifndef TEXT_H_INCLUDED
+#define TEXT_H_INCLUDED
+
+#include <vector>
+
+const size_t SCREEN_WIDTH = 80;
+const size_t MAX_ASTERISK = SCREEN_WIDTH - 3 - 1;
+
+void
+show_histogram_text(std::vector<size_t>& bins);
+
+#endif // TEXT_H_INCLUDED