Родитель
3cd21db461
Сommit
15dba8705d
Разница между файлами не показана из-за своего большого размера
Загрузить разницу
@ -0,0 +1,52 @@
|
|||||||
|
#include "histogram.h"
|
||||||
|
#include <vector>
|
||||||
|
#include <iostream>
|
||||||
|
using namespace std;
|
||||||
|
|
||||||
|
void
|
||||||
|
find_minmax(const vector<double>& numbers, double& min, double& max)
|
||||||
|
{
|
||||||
|
min = numbers[0];
|
||||||
|
max = numbers[0];
|
||||||
|
for (double x : numbers)
|
||||||
|
{
|
||||||
|
if (x > max )
|
||||||
|
{
|
||||||
|
max = x;
|
||||||
|
}
|
||||||
|
if (x < min)
|
||||||
|
{
|
||||||
|
min = x;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
vector<size_t>
|
||||||
|
make_histogram(const vector<double>& numbers, size_t& bin_count)
|
||||||
|
{
|
||||||
|
double min = 0;
|
||||||
|
double max = 0;
|
||||||
|
find_minmax(numbers, min, max);
|
||||||
|
auto bin_size = (max - min)/bin_count;
|
||||||
|
vector<size_t> bins(bin_count);
|
||||||
|
for (double number : numbers)
|
||||||
|
{
|
||||||
|
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 <= number) && (number < hi))
|
||||||
|
{
|
||||||
|
bins[j]++;
|
||||||
|
found = true;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
if (!found)
|
||||||
|
{
|
||||||
|
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
|
@ -0,0 +1,9 @@
|
|||||||
|
#ifndef HISTOGRAM_INTERNAL_H_INCLUDED
|
||||||
|
#define HISTOGRAM_INTERNAL_H_INCLUDED
|
||||||
|
|
||||||
|
#include <vector>
|
||||||
|
|
||||||
|
void
|
||||||
|
find_minmax(const std::vector<double>& numbers, double& min, double& max);
|
||||||
|
|
||||||
|
#endif // HISTOGRAM_INTERNAL_H_INCLUDED
|
@ -0,0 +1,18 @@
|
|||||||
|
# depslib dependency file v1.0
|
||||||
|
1716798554 source:c:\users\lenov\desktop\lab03\histogram.cpp
|
||||||
|
"histogram.h"
|
||||||
|
<vector>
|
||||||
|
<iostream>
|
||||||
|
|
||||||
|
1716797551 c:\users\lenov\desktop\lab03\histogram.h
|
||||||
|
<vector>
|
||||||
|
|
||||||
|
1716799012 source:c:\users\lenov\desktop\lab03\main.cpp
|
||||||
|
<iostream>
|
||||||
|
<vector>
|
||||||
|
"histogram.h"
|
||||||
|
"text.h"
|
||||||
|
|
||||||
|
1716798974 c:\users\lenov\desktop\lab03\text.h
|
||||||
|
<vector>
|
||||||
|
|
@ -0,0 +1,43 @@
|
|||||||
|
#include <iostream>
|
||||||
|
#include "text.h"
|
||||||
|
#include <vector>
|
||||||
|
|
||||||
|
|
||||||
|
using namespace std;
|
||||||
|
|
||||||
|
void
|
||||||
|
show_histogram_text(const vector<size_t>& bins, size_t& bin_count) {
|
||||||
|
const size_t SCREEN_WIDTH = 80;
|
||||||
|
const size_t MAX_ASTERISK = SCREEN_WIDTH - 3 - 1;
|
||||||
|
size_t max_count = 0;
|
||||||
|
for (size_t s = 0; s < bin_count; s++)
|
||||||
|
{
|
||||||
|
if (bins[s] > max_count)
|
||||||
|
{
|
||||||
|
max_count = bins[s];
|
||||||
|
}
|
||||||
|
}
|
||||||
|
auto kof = static_cast<double>(MAX_ASTERISK) / max_count;
|
||||||
|
if (kof > 1)
|
||||||
|
{
|
||||||
|
kof = 1;
|
||||||
|
}
|
||||||
|
for (size_t bin : bins)
|
||||||
|
{
|
||||||
|
if (bin <100)
|
||||||
|
{
|
||||||
|
cout << " ";
|
||||||
|
}
|
||||||
|
if (bin <10)
|
||||||
|
{
|
||||||
|
cout << " ";
|
||||||
|
}
|
||||||
|
cout << bin << "|";
|
||||||
|
size_t weight = bin * kof;
|
||||||
|
for (size_t t = 0; t < weight; t++)
|
||||||
|
{
|
||||||
|
cout << "*";
|
||||||
|
}
|
||||||
|
cout << endl;
|
||||||
|
}
|
||||||
|
}
|
@ -0,0 +1,9 @@
|
|||||||
|
#ifndef TEXT_H_INCLUDED
|
||||||
|
#define TEXT_H_INCLUDED
|
||||||
|
|
||||||
|
#include <vector>
|
||||||
|
|
||||||
|
void
|
||||||
|
show_histogram_text(const std::vector<size_t>& bins, size_t& bin_count);
|
||||||
|
|
||||||
|
#endif // TEXT_H_INCLUDED
|
@ -0,0 +1,38 @@
|
|||||||
|
<?xml version="1.0" encoding="UTF-8" standalone="yes" ?>
|
||||||
|
<CodeBlocks_project_file>
|
||||||
|
<FileVersion major="1" minor="6" />
|
||||||
|
<Project>
|
||||||
|
<Option title="unittest" />
|
||||||
|
<Option pch_mode="2" />
|
||||||
|
<Option compiler="gcc" />
|
||||||
|
<Build>
|
||||||
|
<Target title="Debug">
|
||||||
|
<Option output="bin/Debug/unittest" 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/unittest" 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" />
|
||||||
|
</Compiler>
|
||||||
|
<Extensions>
|
||||||
|
<lib_finder disable_auto="1" />
|
||||||
|
</Extensions>
|
||||||
|
</Project>
|
||||||
|
</CodeBlocks_project_file>
|
Загрузка…
Ссылка в новой задаче