3. Разделение программы на файлы

main
ShevchukDS 2 недель назад
Родитель f6bdfa4bf2
Сommit 613c647f38

@ -0,0 +1,35 @@
#include "histogram.h"
#include <vector>
using namespace std;
static void find_minmax(const vector<double>& numbers, double& min, double& max) {
if (numbers.empty()) return;
min = numbers[0];
max = numbers[0];
for (double x : numbers) {
if (x < min) min = x;
if (x > max) max = x;
}
}
vector<size_t> make_histogram(const vector<double>& numbers, size_t bin_count) {
double min, max;
find_minmax(numbers, min, max);
float k = (max - min) / bin_count;
vector<size_t> bins(bin_count, 0);
for (double num : numbers) {
bool flag = false;
for (size_t j = 0; j < bin_count && !flag; j++) {
if (num >= (min + k * j) && num < (min + k * (j + 1))) {
bins[j]++;
flag = true;
}
}
if (!flag) bins[bin_count - 1]++;
}
return bins;
}

@ -0,0 +1,9 @@
#ifndef HISTOGRAM_H_INCLUDED
#define HISTOGRAM_H_INCLUDED
#include <vector>
std::vector<size_t>
make_histogram(const std::vector<double>& numbers, size_t bin_count);
#endif // HISTOGRAM_H_INCLUDED

@ -1,6 +1,7 @@
#include <iostream>
#include <vector>
#include "histogram.h"
#include "text.h"
using namespace std;
struct Input {
@ -32,67 +33,6 @@ Input input_data() {
return in;
}
void find_minmax(const vector<double>& numbers, double& min, double& max) {
if (numbers.empty()) return;
min = numbers[0];
max = numbers[0];
for (double x : numbers) {
if (x < min) min = x;
if (x > max) max = x;
}
}
vector<int> make_histogram(const vector<double>& numbers, size_t bin_count) {
double min, max;
find_minmax(numbers, min, max);
float k = (max - min) / bin_count;
vector<int> bins(bin_count, 0);
for (double num : numbers) {
bool flag = false;
for (size_t j = 0; j < bin_count && !flag; j++) {
if (num >= (min + k * j) && num < (min + k * (j + 1))) {
bins[j]++;
flag = true;
}
}
if (!flag) bins[bin_count - 1]++;
}
return bins;
}
void show_histogram_text(const vector<int>& bins) {
const size_t SCREEN_WIDTH = 80;
const size_t MAX_ASTERISK = SCREEN_WIDTH - 3 - 1;
int max_count = 0;
for (int count : bins) {
if (count > max_count) max_count = count;
}
for (size_t j = 0; j < bins.size(); j++) {
if (bins[j] < 100) cout << " ";
if (bins[j] < 10) cout << " ";
cout << bins[j] << "|";
size_t height = bins[j];
if (max_count > MAX_ASTERISK) {
if (max_count != bins[j]) {
height = static_cast<size_t>(MAX_ASTERISK * (static_cast<float>(bins[j]) / max_count));
}
else if (max_count == bins[j]) {
height = MAX_ASTERISK;
}
}
for (size_t i = 0; i < height; i++) cout << "*";
cout << "\n";
}
}
int main() {
auto input = input_data();
auto bins = make_histogram(input.numbers, input.bin_count);

@ -0,0 +1,33 @@
#include "text.h"
#include <iostream>
#include <vector>
using namespace std;
void show_histogram_text(const vector<size_t>& bins) {
const size_t SCREEN_WIDTH = 80;
const size_t MAX_ASTERISK = SCREEN_WIDTH - 3 - 1;
int max_count = 0;
for (int count : bins) {
if (count > max_count) max_count = count;
}
for (size_t j = 0; j < bins.size(); j++) {
if (bins[j] < 100) cout << " ";
if (bins[j] < 10) cout << " ";
cout << bins[j] << "|";
size_t height = bins[j];
if (max_count > MAX_ASTERISK) {
if (max_count != bins[j]) {
height = static_cast<size_t>(MAX_ASTERISK * (static_cast<float>(bins[j]) / max_count));
}
else if (max_count == bins[j]) {
height = MAX_ASTERISK;
}
}
for (size_t i = 0; i < height; i++) cout << "*";
cout << "\n";
}
}

@ -0,0 +1,9 @@
#ifndef TEXT_H_INCLUDED
#define TEXT_H_INCLUDED
#include <vector>
void show_histogram_text(const std::vector<size_t>& bins);
#endif // TEXT_H_INCLUDED
Загрузка…
Отмена
Сохранить