the third stage
Этот коммит содержится в:
49
histogram.cpp
Обычный файл
49
histogram.cpp
Обычный файл
@@ -0,0 +1,49 @@
|
|||||||
|
#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 (double x : numbers)
|
||||||
|
{
|
||||||
|
if (x < min)
|
||||||
|
{
|
||||||
|
min = x;
|
||||||
|
}
|
||||||
|
else if (x > max)
|
||||||
|
{
|
||||||
|
max = x;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
vector<size_t> 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<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;
|
||||||
|
}
|
||||||
10
histogram.h
Обычный файл
10
histogram.h
Обычный файл
@@ -0,0 +1,10 @@
|
|||||||
|
#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
|
||||||
@@ -32,7 +32,15 @@
|
|||||||
<Add option="-Wall" />
|
<Add option="-Wall" />
|
||||||
<Add option="-fexceptions" />
|
<Add option="-fexceptions" />
|
||||||
</Compiler>
|
</Compiler>
|
||||||
|
<Unit filename="histogram.cpp" />
|
||||||
|
<Unit filename="histogram.h">
|
||||||
|
<Option target="<{~None~}>" />
|
||||||
|
</Unit>
|
||||||
<Unit filename="main.cpp" />
|
<Unit filename="main.cpp" />
|
||||||
|
<Unit filename="text.cpp" />
|
||||||
|
<Unit filename="text.h">
|
||||||
|
<Option target="<{~None~}>" />
|
||||||
|
</Unit>
|
||||||
<Extensions>
|
<Extensions>
|
||||||
<lib_finder disable_auto="1" />
|
<lib_finder disable_auto="1" />
|
||||||
</Extensions>
|
</Extensions>
|
||||||
|
|||||||
88
main.cpp
88
main.cpp
@@ -1,5 +1,7 @@
|
|||||||
#include <iostream>
|
#include <iostream>
|
||||||
#include <vector>
|
#include <vector>
|
||||||
|
#include "histogram.h"
|
||||||
|
#include "text.h"
|
||||||
|
|
||||||
using namespace std;
|
using namespace std;
|
||||||
|
|
||||||
@@ -28,93 +30,7 @@ Input input_data() {
|
|||||||
return in;
|
return in;
|
||||||
}
|
}
|
||||||
|
|
||||||
void find_minmax(const vector<double>& numbers, double& min, double& max) {
|
|
||||||
min = numbers[0];
|
|
||||||
max = numbers[0];
|
|
||||||
|
|
||||||
for (double x : numbers)
|
|
||||||
{
|
|
||||||
if (x < min)
|
|
||||||
{
|
|
||||||
min = x;
|
|
||||||
}
|
|
||||||
else if (x > max)
|
|
||||||
{
|
|
||||||
max = x;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
vector<size_t> 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<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;
|
|
||||||
}
|
|
||||||
|
|
||||||
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;
|
|
||||||
|
|
||||||
Input in;
|
|
||||||
|
|
||||||
size_t max_count = bins[0];
|
|
||||||
for (size_t i = 0; i < bin_count; i++)
|
|
||||||
{
|
|
||||||
if (bins[i] > max_count) {
|
|
||||||
max_count = bins[i];
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
for (size_t i = 0; i < bin_count; i++)
|
|
||||||
{
|
|
||||||
if (bins[i]<10) {
|
|
||||||
cout << " " << bins[i] << "|";
|
|
||||||
}
|
|
||||||
else if (bins[i]<100) {
|
|
||||||
cout << " " << bins[i] << "|";
|
|
||||||
}
|
|
||||||
else if (bins[i]<1000) {
|
|
||||||
cout << bins[i] << "|";
|
|
||||||
}
|
|
||||||
|
|
||||||
size_t height;
|
|
||||||
|
|
||||||
if (max_count<=MAX_ASTERISK) {
|
|
||||||
height = bins[i];
|
|
||||||
}
|
|
||||||
else {
|
|
||||||
height = MAX_ASTERISK * (static_cast<double>(bins[i]) / max_count);
|
|
||||||
}
|
|
||||||
|
|
||||||
for (size_t j = 0; j < height; j++) {
|
|
||||||
cout << "*";
|
|
||||||
}
|
|
||||||
cout << endl;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
int main()
|
int main()
|
||||||
{
|
{
|
||||||
|
|||||||
43
text.cpp
Обычный файл
43
text.cpp
Обычный файл
@@ -0,0 +1,43 @@
|
|||||||
|
#include "text.h"
|
||||||
|
|
||||||
|
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 = bins[0];
|
||||||
|
for (size_t i = 0; i < bin_count; i++)
|
||||||
|
{
|
||||||
|
if (bins[i] > max_count) {
|
||||||
|
max_count = bins[i];
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
for (size_t i = 0; i < bin_count; i++)
|
||||||
|
{
|
||||||
|
if (bins[i]<10) {
|
||||||
|
cout << " " << bins[i] << "|";
|
||||||
|
}
|
||||||
|
else if (bins[i]<100) {
|
||||||
|
cout << " " << bins[i] << "|";
|
||||||
|
}
|
||||||
|
else if (bins[i]<1000) {
|
||||||
|
cout << bins[i] << "|";
|
||||||
|
}
|
||||||
|
|
||||||
|
size_t height;
|
||||||
|
|
||||||
|
if (max_count<=MAX_ASTERISK) {
|
||||||
|
height = bins[i];
|
||||||
|
}
|
||||||
|
else {
|
||||||
|
height = MAX_ASTERISK * (static_cast<double>(bins[i]) / max_count);
|
||||||
|
}
|
||||||
|
|
||||||
|
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>
|
||||||
|
#include <iostream>
|
||||||
|
|
||||||
|
void show_histogram_text(const std::vector<size_t>& bins, size_t bin_count);
|
||||||
|
|
||||||
|
#endif // TEXT_H_INCLUDED
|
||||||
Ссылка в новой задаче
Block a user