build: добавлено разделение подпрограммы на файлы
Этот коммит содержится в:
51
histogram.cpp
Обычный файл
51
histogram.cpp
Обычный файл
@@ -0,0 +1,51 @@
|
|||||||
|
#include "histogram.h"
|
||||||
|
#include <vector>
|
||||||
|
using namespace std;
|
||||||
|
|
||||||
|
|
||||||
|
static void find_minmax(const vector<double> &numbers, double &min, double &max)
|
||||||
|
{
|
||||||
|
min = numbers[1];
|
||||||
|
max = numbers[0];
|
||||||
|
for (double x: numbers)
|
||||||
|
{
|
||||||
|
if (x < min)
|
||||||
|
{
|
||||||
|
min = x;
|
||||||
|
}
|
||||||
|
else if (x > max)
|
||||||
|
{
|
||||||
|
|
||||||
|
max = x;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
vector<size_t> make_histogram (vector<double> numbers, size_t bin_count)
|
||||||
|
{
|
||||||
|
double min, max;
|
||||||
|
find_minmax (numbers, min, max);
|
||||||
|
double bin_size = (max - min) / bin_count;
|
||||||
|
vector<size_t> bins(bin_count);
|
||||||
|
for (size_t i = 0; i < numbers.size(); i++)
|
||||||
|
{
|
||||||
|
bool found = false;
|
||||||
|
for (size_t j = 0; (j < bin_count - 1) && !found; j++)
|
||||||
|
{
|
||||||
|
auto lo = min + j * bin_size;
|
||||||
|
auto hi = min + (j + 1) * bin_size;
|
||||||
|
if ((lo <= numbers[i]) && (numbers[i] < hi))
|
||||||
|
{
|
||||||
|
bins[j]++;
|
||||||
|
found = true;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
if (!found)
|
||||||
|
{
|
||||||
|
bins[bin_count - 1]++;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return bins;
|
||||||
|
}
|
||||||
9
histogram.h
Обычный файл
9
histogram.h
Обычный файл
@@ -0,0 +1,9 @@
|
|||||||
|
#ifndef HISTOGRAM_H_INCLUDED
|
||||||
|
#define HISTOGRAM_H_INCLUDED
|
||||||
|
|
||||||
|
#include <vector>
|
||||||
|
using namespace std;
|
||||||
|
|
||||||
|
vector<size_t> make_histogram(vector<double> numbers, size_t bin_count);
|
||||||
|
|
||||||
|
#endif
|
||||||
94
main.cpp
94
main.cpp
@@ -1,6 +1,7 @@
|
|||||||
#include <iostream>
|
#include <iostream>
|
||||||
#include <vector>
|
#include <vector>
|
||||||
|
#include "histogram.h"
|
||||||
|
#include "text.h"
|
||||||
using namespace std;
|
using namespace std;
|
||||||
|
|
||||||
|
|
||||||
@@ -25,97 +26,6 @@ Input input_data(){
|
|||||||
return in;
|
return in;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
void find_minmax(const vector<double> &numbers, double &min, double &max)
|
|
||||||
{
|
|
||||||
min = numbers[1];
|
|
||||||
max = numbers[0];
|
|
||||||
for (double x: numbers)
|
|
||||||
{
|
|
||||||
if (x < min)
|
|
||||||
{
|
|
||||||
min = x;
|
|
||||||
}
|
|
||||||
else if (x > max)
|
|
||||||
{
|
|
||||||
|
|
||||||
max = x;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
return;
|
|
||||||
}
|
|
||||||
|
|
||||||
|
|
||||||
vector<size_t> make_histogram (vector<double> numbers, size_t bin_count)
|
|
||||||
{
|
|
||||||
double min, max;
|
|
||||||
find_minmax (numbers, min, max);
|
|
||||||
double bin_size = (max - min) / bin_count;
|
|
||||||
vector<size_t> bins(bin_count);
|
|
||||||
for (size_t i = 0; i < numbers.size(); i++)
|
|
||||||
{
|
|
||||||
bool found = false;
|
|
||||||
for (size_t j = 0; (j < bin_count - 1) && !found; j++)
|
|
||||||
{
|
|
||||||
auto lo = min + j * bin_size;
|
|
||||||
auto hi = min + (j + 1) * bin_size;
|
|
||||||
if ((lo <= numbers[i]) && (numbers[i] < hi))
|
|
||||||
{
|
|
||||||
bins[j]++;
|
|
||||||
found = true;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
if (!found)
|
|
||||||
{
|
|
||||||
bins[bin_count - 1]++;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
return bins;
|
|
||||||
}
|
|
||||||
void show_histogram_text(vector<size_t> bins, size_t bin_count)
|
|
||||||
{
|
|
||||||
const size_t SCREEN_WIDTH = 80;
|
|
||||||
const size_t MAX_ASTERISK = SCREEN_WIDTH - 3 - 1;
|
|
||||||
double max_bin;
|
|
||||||
size_t height;
|
|
||||||
max_bin = bins[0];
|
|
||||||
height = bins[0];
|
|
||||||
for (size_t i = 0; i < bin_count; i++)
|
|
||||||
{
|
|
||||||
if (bins[i] > max_bin){
|
|
||||||
max_bin = bins[i];
|
|
||||||
}
|
|
||||||
}
|
|
||||||
bool flag = false;
|
|
||||||
if ( max_bin > 80)
|
|
||||||
{
|
|
||||||
flag = true;
|
|
||||||
}
|
|
||||||
for (size_t i = 0; i < bin_count; i++)
|
|
||||||
{
|
|
||||||
if (bins[i] < 100)
|
|
||||||
cout << " ";
|
|
||||||
if (bins[i] < 10)
|
|
||||||
cout << " ";
|
|
||||||
cout << bins[i] << "|";
|
|
||||||
if( flag == true)
|
|
||||||
{
|
|
||||||
cout << ' ';
|
|
||||||
height = MAX_ASTERISK * (static_cast<double>(bins[i]) / max_bin);
|
|
||||||
}
|
|
||||||
else
|
|
||||||
{
|
|
||||||
height = bins[i];
|
|
||||||
}
|
|
||||||
for (size_t j = 0; j < height; j++)
|
|
||||||
{
|
|
||||||
cout << "*";
|
|
||||||
}
|
|
||||||
cout << endl;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
int main()
|
int main()
|
||||||
{
|
{
|
||||||
auto in = input_data();
|
auto in = input_data();
|
||||||
|
|||||||
37
text.cpp
Обычный файл
37
text.cpp
Обычный файл
@@ -0,0 +1,37 @@
|
|||||||
|
#include "text.h"
|
||||||
|
#include <iostream>
|
||||||
|
using namespace std;
|
||||||
|
|
||||||
|
void show_histogram_text(vector<size_t> bins, size_t bin_count) {
|
||||||
|
const size_t SCREEN_WIDTH = 80;
|
||||||
|
const size_t MAX_ASTERISK = SCREEN_WIDTH - 3 - 1;
|
||||||
|
double max_bin;
|
||||||
|
size_t height;
|
||||||
|
max_bin = bins[0];
|
||||||
|
height = bins[0];
|
||||||
|
for (size_t i = 0; i < bin_count; i++) {
|
||||||
|
if (bins[i] > max_bin) {
|
||||||
|
max_bin = bins[i];
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
bool flag = false;
|
||||||
|
if (max_bin > 80) {
|
||||||
|
flag = true;
|
||||||
|
}
|
||||||
|
for (size_t i = 0; i < bin_count; i++) {
|
||||||
|
if (bins[i] < 100) cout << " ";
|
||||||
|
if (bins[i] < 10) cout << " ";
|
||||||
|
cout << bins[i] << "|";
|
||||||
|
if (flag == true) {
|
||||||
|
height = MAX_ASTERISK * (static_cast<double>(bins[i]) / max_bin);
|
||||||
|
}
|
||||||
|
else {
|
||||||
|
height = bins[i];
|
||||||
|
}
|
||||||
|
for (size_t j = 0; j < height; j++) {
|
||||||
|
cout << "*";
|
||||||
|
}
|
||||||
|
cout << endl;
|
||||||
|
}
|
||||||
|
}
|
||||||
9
text.h
Обычный файл
9
text.h
Обычный файл
@@ -0,0 +1,9 @@
|
|||||||
|
#ifndef TEXT_H_INCLUDED
|
||||||
|
#define TEXT_H_INCLUDED
|
||||||
|
|
||||||
|
#include <vector>
|
||||||
|
using namespace std;
|
||||||
|
|
||||||
|
void show_histogram_text(vector<size_t> bins, size_t bin_count);
|
||||||
|
|
||||||
|
#endif
|
||||||
Ссылка в новой задаче
Block a user