code: итоговый вариант кода
Этот коммит содержится в:
101
lab01.cpp
101
lab01.cpp
@@ -2,6 +2,10 @@
|
||||
#include <iostream>
|
||||
#include <conio.h>
|
||||
#include <vector>
|
||||
#include "histogram.h"
|
||||
#include "text.h"
|
||||
#include "svg.h"
|
||||
|
||||
using namespace std;
|
||||
|
||||
|
||||
@@ -16,99 +20,6 @@ input_numbers(size_t count) {
|
||||
}
|
||||
|
||||
|
||||
|
||||
void
|
||||
find_minmax(const vector<double>& numbers, double& min, double& max) {
|
||||
min = numbers[0];
|
||||
max = numbers[0];
|
||||
for (double number : numbers)
|
||||
{
|
||||
if (number < min)
|
||||
{
|
||||
min = number;
|
||||
}
|
||||
|
||||
if (number > max)
|
||||
{
|
||||
max = number;
|
||||
}
|
||||
}
|
||||
return;
|
||||
}
|
||||
|
||||
vector<size_t> make_histogram(const vector<double>& numbers, const 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, 0);
|
||||
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(const vector<size_t>& bins)
|
||||
{
|
||||
const size_t SCREEN_WIDTH = 80;
|
||||
const size_t MAX_ASTERISK = SCREEN_WIDTH - 3 - 1;
|
||||
|
||||
size_t max_bin = bins[0];
|
||||
for (double bin : bins)
|
||||
{
|
||||
if (bin > max_bin)
|
||||
{
|
||||
max_bin = bin;
|
||||
}
|
||||
}
|
||||
|
||||
for (size_t bin : bins)
|
||||
{
|
||||
size_t height = bin;
|
||||
// проверить масштабирование(пересчитать height)
|
||||
|
||||
if (max_bin > MAX_ASTERISK)
|
||||
{
|
||||
height = MAX_ASTERISK * (static_cast<double> (bin) / max_bin);
|
||||
}
|
||||
|
||||
|
||||
if (bin < 100) {
|
||||
cout << " ";
|
||||
}
|
||||
|
||||
if (bin < 10) {
|
||||
cout << " ";
|
||||
}
|
||||
|
||||
cout << bin << "|";
|
||||
|
||||
|
||||
for (size_t i = 0; i < height; i++)
|
||||
{
|
||||
cout << "*";
|
||||
}
|
||||
cout << endl;
|
||||
|
||||
}
|
||||
}
|
||||
|
||||
int main()
|
||||
{
|
||||
|
||||
@@ -122,11 +33,11 @@ int main()
|
||||
size_t bin_count;
|
||||
cerr << "Enter bin count: ";
|
||||
cin >> bin_count;
|
||||
//расчет данных
|
||||
|
||||
const auto bins = make_histogram(numbers, bin_count);
|
||||
|
||||
show_histogram_text(bins);
|
||||
//show_histogram_text(bins,bin_count);//
|
||||
show_histogram_svg(bins, number_count, bin_count);
|
||||
|
||||
return 0;
|
||||
}
|
||||
|
||||
Ссылка в новой задаче
Block a user