Сравнить коммиты
2 Коммитов
bdd78f7c1e
...
782babbc6d
Автор | SHA1 | Дата |
---|---|---|
|
782babbc6d | 1 год назад |
|
736ee1a015 | 1 год назад |
@ -0,0 +1,59 @@
|
||||
#include <vector>
|
||||
#include "histogram.h"
|
||||
|
||||
using namespace std;
|
||||
|
||||
static
|
||||
void
|
||||
find_minmax(const vector<double>& numbers, double& min, double& max) {
|
||||
min = numbers[0];
|
||||
max = numbers[0];
|
||||
for (size_t i = 0; i < numbers.size(); i++){
|
||||
if (numbers[i] < min){
|
||||
min = numbers[i];
|
||||
}
|
||||
if (numbers[i] > max){
|
||||
max = numbers[i];
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
vector <double> make_histogram(const vector<double> numbers, size_t bin_count){
|
||||
|
||||
double min, max;
|
||||
find_minmax(numbers, min, max);
|
||||
double bin_size = (max - min) / bin_count;
|
||||
vector <double> bins;
|
||||
bins.resize(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 out;
|
||||
for (std::size_t i = 0; i < bin_count-1; i++)
|
||||
{
|
||||
if (bins[i] > bins[i+1])
|
||||
{
|
||||
out = bins[i];
|
||||
bins[i] = bins[i + 1];
|
||||
bins[i + 1] = out;
|
||||
}
|
||||
}
|
||||
return bins;
|
||||
}
|
||||
|
@ -0,0 +1,5 @@
|
||||
#ifndef HISTOGRAM_H_INCLUDED
|
||||
#define HISTOGRAM_H_INCLUDED
|
||||
std::vector<size_t>
|
||||
make_histogram(const std::vector<double>& numbers, const std::size_t bin_count);
|
||||
#endif // HISTOGRAM_H_INCLUDED
|
@ -0,0 +1,41 @@
|
||||
<?xml version="1.0" encoding="UTF-8" standalone="yes" ?>
|
||||
<CodeBlocks_project_file>
|
||||
<FileVersion major="1" minor="6" />
|
||||
<Project>
|
||||
<Option title="lab_03" />
|
||||
<Option pch_mode="2" />
|
||||
<Option compiler="gcc" />
|
||||
<Build>
|
||||
<Target title="Debug">
|
||||
<Option output="bin/Debug/lab_03" prefix_auto="1" extension_auto="1" />
|
||||
<Option object_output="obj/Debug/" />
|
||||
<Option type="1" />
|
||||
<Option compiler="gcc" />
|
||||
<Compiler>
|
||||
<Add option="-g" />
|
||||
</Compiler>
|
||||
</Target>
|
||||
<Target title="Release">
|
||||
<Option output="bin/Release/lab_03" prefix_auto="1" extension_auto="1" />
|
||||
<Option object_output="obj/Release/" />
|
||||
<Option type="1" />
|
||||
<Option compiler="gcc" />
|
||||
<Compiler>
|
||||
<Add option="-O2" />
|
||||
</Compiler>
|
||||
<Linker>
|
||||
<Add option="-s" />
|
||||
</Linker>
|
||||
</Target>
|
||||
</Build>
|
||||
<Compiler>
|
||||
<Add option="-Wall" />
|
||||
<Add option="-fexceptions" />
|
||||
</Compiler>
|
||||
<Unit filename=".gitignore" />
|
||||
<Unit filename="histogram.cpp" />
|
||||
<Unit filename="histogram.h" />
|
||||
<Unit filename="main.cpp" />
|
||||
<Extensions />
|
||||
</Project>
|
||||
</CodeBlocks_project_file>
|
@ -0,0 +1,43 @@
|
||||
#include <vector>
|
||||
#include "text.h"
|
||||
#include <iostream>
|
||||
|
||||
using namespace std;
|
||||
|
||||
const int max_length = 80; // ìàêñèìàëüíàÿ äëèíà
|
||||
const int indent = 4; // îòñòóï
|
||||
|
||||
|
||||
void show_histogram_text(const vector<double> bins, size_t bin_count){
|
||||
double max_bin = bins[0];
|
||||
int k;
|
||||
for (size_t i = 0; i < bin_count; i++) { // íàõîæäåíèå ìàêñ çíà÷åíèÿ bin[]
|
||||
if (bins[i] > max_bin) {
|
||||
max_bin = bins[i];
|
||||
}
|
||||
}
|
||||
if (max_bin <= (max_length - indent)) { // ñðàâíåíèå äëèíû ñòðîêè "*" ñ ìàêñèìàëüíîé äëèíîé âûâîäèìîé ñòðîêè
|
||||
k = 0;
|
||||
}
|
||||
else {
|
||||
k = 1;
|
||||
}
|
||||
|
||||
cerr << endl;
|
||||
int out;
|
||||
for (size_t i = 0; i < bin_count; i++) {
|
||||
cout.width(3); // âûðàâíèâàíèå
|
||||
cout.fill(' ');
|
||||
cout << bins[i] << "|";
|
||||
if (k == 0) { // ìàñøòàáèðîâàíèå
|
||||
out = bins[i]; // îáû÷íûé âûâîä
|
||||
}
|
||||
else {
|
||||
out = bins[i] * (max_length - indent) / max_bin; // ìàñøòàáèðîâàííûé âûâîä
|
||||
}
|
||||
for (int j = 0; j < out; j++) {
|
||||
cout << "*";
|
||||
}
|
||||
cout << endl;
|
||||
}
|
||||
}
|
@ -0,0 +1,4 @@
|
||||
#ifndef TEXT_H_INCLUDED
|
||||
#define TEXT_H_INCLUDED
|
||||
void show_histogram_text(const std::vector<double> bins, size_t bin_count);
|
||||
#endif // TEXT_H_INCLUDED
|
Загрузка…
Ссылка в новой задаче