code:разделил программу на файлы
Этот коммит содержится в:
41
histogram.cpp
Обычный файл
41
histogram.cpp
Обычный файл
@@ -0,0 +1,41 @@
|
||||
#include "histogram.hpp"
|
||||
#include <vector>
|
||||
|
||||
void find_minmax(std::vector<double> &numbers, double& min, double& max)
|
||||
{
|
||||
min = numbers[0]; max = numbers[0];
|
||||
for (double x : numbers)
|
||||
{
|
||||
if (max < x)
|
||||
max = x;
|
||||
if (min > x)
|
||||
min = x;
|
||||
}
|
||||
}
|
||||
|
||||
std::vector<size_t> make_histogram(std::vector<double> numbers, size_t bin_count)
|
||||
{
|
||||
double min, max;
|
||||
find_minmax(numbers, min, max);
|
||||
double bin_size = (max - min) / bin_count;
|
||||
std::vector<size_t> bins(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 + 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;
|
||||
}
|
||||
3
histogram.hpp
Обычный файл
3
histogram.hpp
Обычный файл
@@ -0,0 +1,3 @@
|
||||
#include <vector>
|
||||
|
||||
std::vector<size_t> make_histogram(std::vector<double> numbers, size_t bin_count);
|
||||
@@ -8,6 +8,8 @@
|
||||
|
||||
/* Begin PBXBuildFile section */
|
||||
35EA3A982A2D54F400ED69F1 /* main.cpp in Sources */ = {isa = PBXBuildFile; fileRef = 35EA3A972A2D54F400ED69F1 /* main.cpp */; };
|
||||
35EA3AA02A2D58E700ED69F1 /* histogram.cpp in Sources */ = {isa = PBXBuildFile; fileRef = 35EA3A9E2A2D58E700ED69F1 /* histogram.cpp */; };
|
||||
35EA3AA32A2D591600ED69F1 /* text.cpp in Sources */ = {isa = PBXBuildFile; fileRef = 35EA3AA12A2D591600ED69F1 /* text.cpp */; };
|
||||
/* End PBXBuildFile section */
|
||||
|
||||
/* Begin PBXCopyFilesBuildPhase section */
|
||||
@@ -25,6 +27,10 @@
|
||||
/* Begin PBXFileReference section */
|
||||
35EA3A942A2D54F400ED69F1 /* lab_3 */ = {isa = PBXFileReference; explicitFileType = "compiled.mach-o.executable"; includeInIndex = 0; path = lab_3; sourceTree = BUILT_PRODUCTS_DIR; };
|
||||
35EA3A972A2D54F400ED69F1 /* main.cpp */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.cpp.cpp; path = main.cpp; sourceTree = "<group>"; };
|
||||
35EA3A9E2A2D58E700ED69F1 /* histogram.cpp */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.cpp.cpp; path = histogram.cpp; sourceTree = "<group>"; };
|
||||
35EA3A9F2A2D58E700ED69F1 /* histogram.hpp */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.cpp.h; path = histogram.hpp; sourceTree = "<group>"; };
|
||||
35EA3AA12A2D591600ED69F1 /* text.cpp */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.cpp.cpp; path = text.cpp; sourceTree = "<group>"; };
|
||||
35EA3AA22A2D591600ED69F1 /* text.hpp */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.cpp.h; path = text.hpp; sourceTree = "<group>"; };
|
||||
/* End PBXFileReference section */
|
||||
|
||||
/* Begin PBXFrameworksBuildPhase section */
|
||||
@@ -42,6 +48,10 @@
|
||||
isa = PBXGroup;
|
||||
children = (
|
||||
35EA3A972A2D54F400ED69F1 /* main.cpp */,
|
||||
35EA3AA12A2D591600ED69F1 /* text.cpp */,
|
||||
35EA3AA22A2D591600ED69F1 /* text.hpp */,
|
||||
35EA3A9E2A2D58E700ED69F1 /* histogram.cpp */,
|
||||
35EA3A9F2A2D58E700ED69F1 /* histogram.hpp */,
|
||||
35EA3A952A2D54F400ED69F1 /* Products */,
|
||||
);
|
||||
sourceTree = "<group>";
|
||||
@@ -111,7 +121,9 @@
|
||||
isa = PBXSourcesBuildPhase;
|
||||
buildActionMask = 2147483647;
|
||||
files = (
|
||||
35EA3AA02A2D58E700ED69F1 /* histogram.cpp in Sources */,
|
||||
35EA3A982A2D54F400ED69F1 /* main.cpp in Sources */,
|
||||
35EA3AA32A2D591600ED69F1 /* text.cpp in Sources */,
|
||||
);
|
||||
runOnlyForDeploymentPostprocessing = 0;
|
||||
};
|
||||
|
||||
Двоичные данные
lab_3.xcodeproj/project.xcworkspace/xcuserdata/lesaminov.xcuserdatad/UserInterfaceState.xcuserstate
сгенерированный
Двоичные данные
lab_3.xcodeproj/project.xcworkspace/xcuserdata/lesaminov.xcuserdatad/UserInterfaceState.xcuserstate
сгенерированный
Двоичный файл не отображается.
63
main.cpp
63
main.cpp
@@ -1,6 +1,8 @@
|
||||
#include <iostream>
|
||||
#include <vector>
|
||||
#include <ctime>
|
||||
#include "histogram.hpp"
|
||||
#include "text.hpp"
|
||||
|
||||
using namespace std;
|
||||
|
||||
@@ -26,67 +28,6 @@ Input input_data()
|
||||
return in;
|
||||
}
|
||||
|
||||
void find_minmax(vector<double> &numbers, double& min, double& max)
|
||||
{
|
||||
min = numbers[0]; max = numbers[0];
|
||||
for (double x : numbers)
|
||||
{
|
||||
if (max < x)
|
||||
max = x;
|
||||
if (min > x)
|
||||
min = x;
|
||||
}
|
||||
}
|
||||
|
||||
vector<size_t> make_histogram(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 (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;
|
||||
}
|
||||
|
||||
const size_t SCREEN_WIDTH = 80;
|
||||
const size_t MAX_ASTERISK = SCREEN_WIDTH - 3 - 1;
|
||||
|
||||
void show_histogram_text(std::vector<size_t> bins, size_t bin_count)
|
||||
{
|
||||
double max_bin = *std::max_element(bins.begin(), bins.end());
|
||||
for (size_t i = 0; i < bin_count; i++)
|
||||
{
|
||||
if (bins[i] < 100)
|
||||
cout << " ";
|
||||
if (bins[i] < 10)
|
||||
cout << " ";
|
||||
cout << bins[i] << "|";
|
||||
size_t height = MAX_ASTERISK * (static_cast<double>(bins[i]) / max_bin);
|
||||
for (size_t j = 0; j < height; j++)
|
||||
{
|
||||
cout << "*";
|
||||
}
|
||||
cout << endl;
|
||||
}
|
||||
}
|
||||
|
||||
int main()
|
||||
{
|
||||
auto in = input_data();
|
||||
|
||||
24
text.cpp
Обычный файл
24
text.cpp
Обычный файл
@@ -0,0 +1,24 @@
|
||||
#include "text.hpp"
|
||||
#include <iostream>
|
||||
|
||||
const size_t SCREEN_WIDTH = 80;
|
||||
const size_t MAX_ASTERISK = SCREEN_WIDTH - 3 - 1;
|
||||
|
||||
void show_histogram_text(std::vector<size_t> bins, size_t bin_count)
|
||||
{
|
||||
double max_bin = *std::max_element(bins.begin(), bins.end());
|
||||
for (size_t i = 0; i < bin_count; i++)
|
||||
{
|
||||
if (bins[i] < 100)
|
||||
std::cout << " ";
|
||||
if (bins[i] < 10)
|
||||
std::cout << " ";
|
||||
std::cout << bins[i] << "|";
|
||||
size_t height = MAX_ASTERISK * (static_cast<double>(bins[i]) / max_bin);
|
||||
for (size_t j = 0; j < height; j++)
|
||||
{
|
||||
std::cout << "*";
|
||||
}
|
||||
std::cout << std::endl;
|
||||
}
|
||||
}
|
||||
4
text.hpp
Обычный файл
4
text.hpp
Обычный файл
@@ -0,0 +1,4 @@
|
||||
#include <vector>
|
||||
using namespace std;
|
||||
|
||||
void show_histogram_text(vector<size_t> bins, size_t bin_count);
|
||||
Ссылка в новой задаче
Block a user