code: разделение программы на файлы
Этот коммит содержится в:
57
histogram.cpp
Обычный файл
57
histogram.cpp
Обычный файл
@@ -0,0 +1,57 @@
|
||||
#include "histogram.h"
|
||||
#include <vector>
|
||||
|
||||
static void find_minmax(const std::vector<double>& numbers, double& min, double& max)
|
||||
{
|
||||
min = numbers[0];
|
||||
for (double x : numbers)
|
||||
{
|
||||
if (x < min)
|
||||
{
|
||||
min = x;
|
||||
}
|
||||
else if (x > max)
|
||||
{
|
||||
max = x;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
std::vector<double> make_histogram(const std::vector<double> &numbers, std::size_t bin_count)
|
||||
{
|
||||
double min = numbers[0];
|
||||
double max = numbers[0];
|
||||
find_minmax(numbers, min, max);
|
||||
double bin_size = (max - min) / bin_count;
|
||||
std::vector<double>bins(bin_count);
|
||||
|
||||
for (std::size_t i = 0; i < numbers.size(); i++)
|
||||
{
|
||||
bool found = false;
|
||||
for (std::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]++;
|
||||
}
|
||||
}
|
||||
double b;
|
||||
for (std::size_t i = 0; i < bin_count-1; i++)
|
||||
{
|
||||
if (bins[i] > bins[i+1])
|
||||
{
|
||||
b = bins[i];
|
||||
bins[i] = bins[i + 1];
|
||||
bins[i + 1] = b;
|
||||
}
|
||||
}
|
||||
return bins;
|
||||
}
|
||||
8
histogram.h
Обычный файл
8
histogram.h
Обычный файл
@@ -0,0 +1,8 @@
|
||||
#ifndef HISTOGRAM_H_INCLUDED
|
||||
#define HISTOGRAM_H_INCLUDED
|
||||
#include <vector>
|
||||
|
||||
std::vector<double> make_histogram(const std::vector<double>& numbers, std::size_t bin_count);
|
||||
|
||||
|
||||
#endif // HISTOGRAM_H_INCLUDED
|
||||
107
main.cpp
107
main.cpp
@@ -1,5 +1,8 @@
|
||||
#include <iostream>
|
||||
#include <vector>
|
||||
#include "histogram.h"
|
||||
#include "text.h"
|
||||
|
||||
using namespace std;
|
||||
|
||||
struct Input
|
||||
@@ -25,110 +28,6 @@ Input input_data()
|
||||
return in;
|
||||
}
|
||||
|
||||
void find_minmax(const vector<double>& numbers, double& min, double& max)
|
||||
{
|
||||
min = numbers[0];
|
||||
for (double x : numbers)
|
||||
{
|
||||
if (x < min)
|
||||
{
|
||||
min = x;
|
||||
}
|
||||
else if (x > max)
|
||||
{
|
||||
max = x;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
vector<double> make_histogram(const vector<double> &numbers, size_t bin_count)
|
||||
{
|
||||
double min = numbers[0];
|
||||
double max = numbers[0];
|
||||
find_minmax(numbers, min, max);
|
||||
double bin_size = (max - min) / bin_count;
|
||||
vector<double>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]++;
|
||||
}
|
||||
}
|
||||
double b;
|
||||
for (size_t i = 0; i < bin_count-1; i++)
|
||||
{
|
||||
if (bins[i] > bins[i+1])
|
||||
{
|
||||
b = bins[i];
|
||||
bins[i] = bins[i + 1];
|
||||
bins[i + 1] = b;
|
||||
}
|
||||
}
|
||||
return bins;
|
||||
}
|
||||
|
||||
void show_histogram_text(const vector <double> &bins)
|
||||
{
|
||||
const size_t SCREEN_WIDTH = 80;
|
||||
const size_t MAX_STAR = SCREEN_WIDTH - 3 - 1;
|
||||
size_t max_star_search = bins[-1];
|
||||
|
||||
if (max_star_search > MAX_STAR)
|
||||
{
|
||||
for (size_t x : bins)
|
||||
{
|
||||
size_t height = MAX_STAR * (static_cast<double>(x) / max_star_search);
|
||||
if (x < 100)
|
||||
{
|
||||
cout << " ";
|
||||
if (x < 10)
|
||||
{
|
||||
cout << " ";
|
||||
}
|
||||
}
|
||||
cout << x << "|";
|
||||
for (size_t i = 0; i < height; i++)
|
||||
{
|
||||
cout << "*";
|
||||
}
|
||||
cout << endl;
|
||||
}
|
||||
}
|
||||
else
|
||||
{
|
||||
for (size_t x : bins)
|
||||
{
|
||||
if (x < 100)
|
||||
{
|
||||
cout << " ";
|
||||
if (x < 10)
|
||||
{
|
||||
cout << " ";
|
||||
}
|
||||
}
|
||||
cout << x << "|";
|
||||
for (size_t i = 0; i < x; i++)
|
||||
{
|
||||
cout << "*";
|
||||
}
|
||||
cout << endl;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
int main()
|
||||
{
|
||||
auto in = input_data();
|
||||
|
||||
52
text.cpp
Обычный файл
52
text.cpp
Обычный файл
@@ -0,0 +1,52 @@
|
||||
#include <iostream>
|
||||
#include <vector>
|
||||
#include "text.h"
|
||||
|
||||
void show_histogram_text(const std::vector <double> &bins)
|
||||
{
|
||||
const size_t SCREEN_WIDTH = 80;
|
||||
const size_t MAX_STAR = SCREEN_WIDTH - 3 - 1;
|
||||
size_t max_star_search = bins[-1];
|
||||
|
||||
if (max_star_search > MAX_STAR)
|
||||
{
|
||||
for (size_t x : bins)
|
||||
{
|
||||
size_t height = MAX_STAR * (static_cast<double>(x) / max_star_search);
|
||||
if (x < 100)
|
||||
{
|
||||
std::cout << " ";
|
||||
if (x < 10)
|
||||
{
|
||||
std::cout << " ";
|
||||
}
|
||||
}
|
||||
std::cout << x << "|";
|
||||
for (size_t i = 0; i < height; i++)
|
||||
{
|
||||
std::cout << "*";
|
||||
}
|
||||
std::cout << std::endl;
|
||||
}
|
||||
}
|
||||
else
|
||||
{
|
||||
for (size_t x : bins)
|
||||
{
|
||||
if (x < 100)
|
||||
{
|
||||
std::cout << " ";
|
||||
if (x < 10)
|
||||
{
|
||||
std::cout << " ";
|
||||
}
|
||||
}
|
||||
std::cout << x << "|";
|
||||
for (size_t i = 0; i < x; i++)
|
||||
{
|
||||
std::cout << "*";
|
||||
}
|
||||
std::cout << std::endl;
|
||||
}
|
||||
}
|
||||
}
|
||||
7
text.h
Обычный файл
7
text.h
Обычный файл
@@ -0,0 +1,7 @@
|
||||
#ifndef TEXT_H_INCLUDED
|
||||
#define TEXT_H_INCLUDED
|
||||
#include <vector>
|
||||
|
||||
void show_histogram_text(const std::vector<double> &bins);
|
||||
|
||||
#endif // TEXT_H_INCLUDED
|
||||
Ссылка в новой задаче
Block a user