From b4a90ddfa7235ff0839a86203499f6387b54e852 Mon Sep 17 00:00:00 2001 From: IshutinaYI Date: Sun, 23 Apr 2023 05:25:04 +0300 Subject: [PATCH] =?UTF-8?q?build:=20=D0=92=D1=8B=D0=BF=D0=BE=D0=BB=D0=BD?= =?UTF-8?q?=D0=B5=D0=BD=D0=B0=20=D0=BF=D0=B5=D1=80=D0=B5=D1=81=D0=B1=D0=BE?= =?UTF-8?q?=D1=80=D0=BA=D0=B0=20=D0=BF=D1=80=D0=BE=D0=B5=D0=BA=D1=82=D0=B0?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- fin_lab34.cpp | 50 ++------------------------------------------ histogram.cpp | 40 +++++++++++++++++++++++++++++++++++ histogram.h | 8 +++++++ histogram_internal.h | 8 +++++++ text.cpp | 23 ++++++++++++++++++++ text.h | 7 +++++++ 6 files changed, 88 insertions(+), 48 deletions(-) create mode 100644 histogram.cpp create mode 100644 histogram.h create mode 100644 histogram_internal.h create mode 100644 text.cpp create mode 100644 text.h diff --git a/fin_lab34.cpp b/fin_lab34.cpp index 83ca543..fc9a2dc 100644 --- a/fin_lab34.cpp +++ b/fin_lab34.cpp @@ -1,6 +1,8 @@ #include #include #include +#include "histogram.h" +#include "text.h" const size_t WINDOW_WIDTH = 80; const size_t MAX_VALUE = WINDOW_WIDTH - 3 - 1; using namespace std; @@ -23,54 +25,6 @@ input_data(){ return in; } -void -find_minmax(const vector &numbers, double &min, double &max){ - min = numbers[0]; - max = numbers[0]; - for (float now: numbers){ - if (now < min) {min = now;} - if (now > max) {max = now;} - } - return; -} - -vector -make_histogram(vector numbers, size_t bin_count){ - vector bins(bin_count); - double min = 0, max = 0; - find_minmax(numbers, min, max); - double bin_size = (max - min) / bin_count; - double lo = min, hi = min + bin_size; - for (size_t i = 0; i < bin_count; i++){ - for (auto now : numbers){ - if (i == bin_count - 1) { - if ((now >= lo) && (now <= hi)) {bins[i]++;} - } - else { - if ((now >= lo) && (now < hi)) {bins[i]++;} - } - } - lo = hi; hi += bin_size; - } - return bins; -} - -void show_histogram_text(const vector &bins){ - size_t max_count = 0; - for (auto now: bins) - {if (now > max_count) {max_count = now;}} - cout << "mc = " << max_count << endl; - for (size_t now : bins){ - if (now < 100) {cout << ' ';} if (now < 10) {cout << ' ';} - cout << now << "|"; - int height; - if (now == max_count) {height = MAX_VALUE * 1.0;} - else {height = MAX_VALUE * (static_cast (now) / max_count);} - for (int i = 0; i < round(height); i++) {cout << "*";} - cout << endl; - } -} - int main(){ double min = 0, max = 0; diff --git a/histogram.cpp b/histogram.cpp new file mode 100644 index 0000000..853f58f --- /dev/null +++ b/histogram.cpp @@ -0,0 +1,40 @@ +#include +#include +#include "histogram.h" +#include "histogram_internal.h" + +using namespace std; +void +find_minmax(const vector &numbers, double &min, double &max){ + if (numbers.size() == 0){min = 0; max = 0;} + else{ + min = numbers[0]; + max = numbers[0]; + for (float now: numbers){ + if (now < min) {min = now;} + if (now > max) {max = now;} + } + return; + } +} + +vector +make_histogram(const vector &numbers, size_t bin_count){ + vector bins(bin_count); + double min = 0, max = 0; + find_minmax(numbers, min, max); + double bin_size = (max - min) / bin_count; + double lo = min, hi = min + bin_size; + for (size_t i = 0; i < bin_count; i++){ + for (auto now : numbers){ + if (i == bin_count - 1) { + if ((now >= lo) && (now <= hi)) {bins[i]++;} + } + else { + if ((now >= lo) && (now < hi)) {bins[i]++;} + } + } + lo = hi; hi += bin_size; + } + return bins; +} diff --git a/histogram.h b/histogram.h new file mode 100644 index 0000000..7e35b93 --- /dev/null +++ b/histogram.h @@ -0,0 +1,8 @@ +#ifndef HISTOGRAM_H_INCLUDED +#define HISTOGRAM_H_INCLUDED +#include +#include "histogram_internal.h" +using namespace std; +vector make_histogram(const vector &numbers, size_t bin_count); + +#endif // HISTOGRAM_H_INCLUDED diff --git a/histogram_internal.h b/histogram_internal.h new file mode 100644 index 0000000..396afe8 --- /dev/null +++ b/histogram_internal.h @@ -0,0 +1,8 @@ +#ifndef HISTOGRAM_INTERNAL_H_INCLUDED +#define HISTOGRAM_INTERNAL_H_INCLUDED +#include +using namespace std; +void find_minmax(const vector &numbers, double &min, double &max); + +#endif // HISTOGRAM_INTERNAL_H_INCLUDED + diff --git a/text.cpp b/text.cpp new file mode 100644 index 0000000..ca03750 --- /dev/null +++ b/text.cpp @@ -0,0 +1,23 @@ +#include +#include +#include +#include "text.h" +const size_t WINDOW_WIDTH = 80; +const size_t MAX_VALUE = WINDOW_WIDTH - 3 - 1; +using namespace std; +void +show_histogram_text(const vector &bins){ + size_t max_count = 0; + for (auto now: bins) + {if (now > max_count) {max_count = now;}} + cout << "mc = " << max_count << endl; + for (size_t now : bins){ + if (now < 100) {cout << ' ';} if (now < 10) {cout << ' ';} //форматирование строк + cout << now << "|"; + int height; + if (now == max_count) {height = MAX_VALUE * 1.0;} + else {height = MAX_VALUE * (static_cast (now) / max_count);} + for (int i = 0; i < round(height); i++) {cout << "*";} + cout << endl; + } +} diff --git a/text.h b/text.h new file mode 100644 index 0000000..eb4acd2 --- /dev/null +++ b/text.h @@ -0,0 +1,7 @@ +#ifndef TEXT_H_INCLUDED +#define TEXT_H_INCLUDED +#include +using namespace std; +void show_histogram_text(const vector &bins); + +#endif // TEXT_H_INCLUDED