code: Физическая декомпозиция
Этот коммит содержится в:
52
histogram.cpp
Обычный файл
52
histogram.cpp
Обычный файл
@@ -0,0 +1,52 @@
|
||||
#include <iostream>
|
||||
#include <conio.h>
|
||||
#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 (double x : numbers)
|
||||
{
|
||||
if (x < min)
|
||||
{
|
||||
min = x;
|
||||
}
|
||||
if (x > max)
|
||||
{
|
||||
max = x;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
vector<size_t> make_histogram(const vector<double>& numbers, size_t bin_count)
|
||||
{
|
||||
double min_number, max_number;
|
||||
find_minmax(numbers, min_number, max_number);
|
||||
vector<size_t> bins(bin_count);
|
||||
for (int i = 0; i < bin_count; i++)
|
||||
{
|
||||
bins[i] = 0;
|
||||
}
|
||||
double bin_size = (max_number - min_number) / 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_number + j * bin_size;
|
||||
auto hi = min_number + (j + 1) * bin_size;
|
||||
if ((lo <= numbers[i]) && (numbers[i] < hi))
|
||||
{
|
||||
bins[j]++;
|
||||
found = true;
|
||||
}
|
||||
}
|
||||
if (!found)
|
||||
{
|
||||
bins[bin_count - 1]++;
|
||||
}
|
||||
}
|
||||
return bins;
|
||||
}
|
||||
4
histogram.h
Обычный файл
4
histogram.h
Обычный файл
@@ -0,0 +1,4 @@
|
||||
#pragma once
|
||||
#include <vector>
|
||||
|
||||
std::vector<size_t> make_histogram(const std::vector<double>& numbers, size_t bin_count);
|
||||
@@ -19,7 +19,15 @@
|
||||
</ProjectConfiguration>
|
||||
</ItemGroup>
|
||||
<ItemGroup>
|
||||
<ClCompile Include="histogram.cpp" />
|
||||
<ClCompile Include="lab1.cpp" />
|
||||
<ClCompile Include="text.cpp" />
|
||||
</ItemGroup>
|
||||
<ItemGroup>
|
||||
<ClInclude Include="histogram.h">
|
||||
<ExcludedFromBuild Condition="'$(Configuration)|$(Platform)'=='Release|x64'">false</ExcludedFromBuild>
|
||||
</ClInclude>
|
||||
<ClInclude Include="text.h" />
|
||||
</ItemGroup>
|
||||
<PropertyGroup Label="Globals">
|
||||
<VCProjectVersion>17.0</VCProjectVersion>
|
||||
|
||||
96
lab1.cpp
96
lab1.cpp
@@ -1,6 +1,8 @@
|
||||
#include <iostream>
|
||||
#include <conio.h>
|
||||
#include <vector>
|
||||
#include "histogram.h"
|
||||
#include "text.h"
|
||||
using namespace std;
|
||||
struct Input {
|
||||
vector<double> numbers;
|
||||
@@ -19,100 +21,6 @@ Input input_data() {
|
||||
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;
|
||||
}
|
||||
if (x > max)
|
||||
{
|
||||
max = x;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
vector<size_t> make_histogram(vector<double>& numbers, size_t bin_count)
|
||||
{
|
||||
double min_number, max_number;
|
||||
find_minmax(numbers, min_number, max_number);
|
||||
vector<size_t> bins(bin_count);
|
||||
for (int i = 0; i < bin_count; i++)
|
||||
{
|
||||
bins[i] = 0;
|
||||
}
|
||||
double bin_size = (max_number - min_number) / 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_number + j * bin_size;
|
||||
auto hi = min_number + (j + 1) * bin_size;
|
||||
if ((lo <= numbers[i]) && (numbers[i] < hi))
|
||||
{
|
||||
bins[j]++;
|
||||
found = true;
|
||||
}
|
||||
}
|
||||
if (!found)
|
||||
{
|
||||
bins[bin_count - 1]++;
|
||||
}
|
||||
}
|
||||
return bins;
|
||||
}
|
||||
|
||||
void find_max_capacity(vector<size_t>& bins, size_t& max_bin_capacity)
|
||||
{
|
||||
max_bin_capacity = bins[0];
|
||||
for (size_t x : bins)
|
||||
{
|
||||
if (x > max_bin_capacity)
|
||||
{
|
||||
max_bin_capacity = x;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
void show_histogram_text(vector<size_t>& bins, size_t max_asterisk)
|
||||
{
|
||||
size_t max_bin_capacity{};
|
||||
find_max_capacity(bins, max_bin_capacity);
|
||||
size_t height = 0;
|
||||
for (int i = 0; i < bins.size(); i++)
|
||||
{
|
||||
if (bins[i] < 10)
|
||||
{
|
||||
cout << " " << bins[i] << "|";
|
||||
}
|
||||
if ((bins[i] < 100) && (bins[i] > 9))
|
||||
{
|
||||
cout << " " << bins[i] << "|";
|
||||
}
|
||||
if (bins[i] >= 100)
|
||||
{
|
||||
cout << bins[i] << "|";
|
||||
}
|
||||
if (max_bin_capacity > max_asterisk)
|
||||
{
|
||||
height = max_asterisk * (static_cast<double>(bins[i]) / max_bin_capacity);
|
||||
}
|
||||
else
|
||||
{
|
||||
height = bins[i];
|
||||
}
|
||||
for (int j = 0; j < height; j++)
|
||||
{
|
||||
cout << "*";
|
||||
}
|
||||
cout << "\n";
|
||||
}
|
||||
}
|
||||
|
||||
int main()
|
||||
{
|
||||
const size_t SCREEN_WIDTH = 80;
|
||||
|
||||
52
text.cpp
Обычный файл
52
text.cpp
Обычный файл
@@ -0,0 +1,52 @@
|
||||
#include <iostream>
|
||||
#include <conio.h>
|
||||
#include <vector>
|
||||
#include "text.h"
|
||||
using namespace std;
|
||||
|
||||
static void find_max_capacity(const vector<size_t>& bins, size_t& max_bin_capacity)
|
||||
{
|
||||
max_bin_capacity = bins[0];
|
||||
for (size_t x : bins)
|
||||
{
|
||||
if (x > max_bin_capacity)
|
||||
{
|
||||
max_bin_capacity = x;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
void show_histogram_text(const vector<size_t>& bins, size_t max_asterisk)
|
||||
{
|
||||
size_t max_bin_capacity{};
|
||||
find_max_capacity(bins, max_bin_capacity);
|
||||
size_t height = 0;
|
||||
for (int i = 0; i < bins.size(); i++)
|
||||
{
|
||||
if (bins[i] < 10)
|
||||
{
|
||||
cout << " " << bins[i] << "|";
|
||||
}
|
||||
if ((bins[i] < 100) && (bins[i] > 9))
|
||||
{
|
||||
cout << " " << bins[i] << "|";
|
||||
}
|
||||
if (bins[i] >= 100)
|
||||
{
|
||||
cout << bins[i] << "|";
|
||||
}
|
||||
if (max_bin_capacity > max_asterisk)
|
||||
{
|
||||
height = max_asterisk * (static_cast<double>(bins[i]) / max_bin_capacity);
|
||||
}
|
||||
else
|
||||
{
|
||||
height = bins[i];
|
||||
}
|
||||
for (int j = 0; j < height; j++)
|
||||
{
|
||||
cout << "*";
|
||||
}
|
||||
cout << "\n";
|
||||
}
|
||||
}
|
||||
4
text.h
Обычный файл
4
text.h
Обычный файл
@@ -0,0 +1,4 @@
|
||||
#pragma once
|
||||
#include <vector>
|
||||
|
||||
void show_histogram_text(const std::vector<size_t>& bins, std::size_t max_asterisk);
|
||||
Ссылка в новой задаче
Block a user