diff --git a/histogram.cpp b/histogram.cpp new file mode 100644 index 0000000..f6d543d --- /dev/null +++ b/histogram.cpp @@ -0,0 +1,41 @@ +#include "histogram.hpp" +#include + +void find_minmax(std::vector &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 make_histogram(std::vector numbers, size_t bin_count) +{ + double min, max; + find_minmax(numbers, min, max); + double bin_size = (max - min) / bin_count; + std::vector 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; +} diff --git a/histogram.hpp b/histogram.hpp new file mode 100644 index 0000000..6b40c1b --- /dev/null +++ b/histogram.hpp @@ -0,0 +1,3 @@ +#include + +std::vector make_histogram(std::vector numbers, size_t bin_count); diff --git a/lab_3.xcodeproj/project.pbxproj b/lab_3.xcodeproj/project.pbxproj index 2719eb8..08e91ef 100644 --- a/lab_3.xcodeproj/project.pbxproj +++ b/lab_3.xcodeproj/project.pbxproj @@ -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 = ""; }; + 35EA3A9E2A2D58E700ED69F1 /* histogram.cpp */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.cpp.cpp; path = histogram.cpp; sourceTree = ""; }; + 35EA3A9F2A2D58E700ED69F1 /* histogram.hpp */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.cpp.h; path = histogram.hpp; sourceTree = ""; }; + 35EA3AA12A2D591600ED69F1 /* text.cpp */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.cpp.cpp; path = text.cpp; sourceTree = ""; }; + 35EA3AA22A2D591600ED69F1 /* text.hpp */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.cpp.h; path = text.hpp; sourceTree = ""; }; /* 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 = ""; @@ -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; }; diff --git a/lab_3.xcodeproj/project.xcworkspace/xcuserdata/lesaminov.xcuserdatad/UserInterfaceState.xcuserstate b/lab_3.xcodeproj/project.xcworkspace/xcuserdata/lesaminov.xcuserdatad/UserInterfaceState.xcuserstate index 7d29fed..a338503 100644 Binary files a/lab_3.xcodeproj/project.xcworkspace/xcuserdata/lesaminov.xcuserdatad/UserInterfaceState.xcuserstate and b/lab_3.xcodeproj/project.xcworkspace/xcuserdata/lesaminov.xcuserdatad/UserInterfaceState.xcuserstate differ diff --git a/main.cpp b/main.cpp index ecb814d..742aae6 100644 --- a/main.cpp +++ b/main.cpp @@ -1,6 +1,8 @@ #include #include #include +#include "histogram.hpp" +#include "text.hpp" using namespace std; @@ -26,67 +28,6 @@ Input input_data() return in; } -void find_minmax(vector &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 make_histogram(vector numbers, size_t bin_count) -{ - double min, max; - find_minmax(numbers, min, max); - double bin_size = (max - min) / bin_count; - vector 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 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(bins[i]) / max_bin); - for (size_t j = 0; j < height; j++) - { - cout << "*"; - } - cout << endl; - } -} - int main() { auto in = input_data(); diff --git a/text.cpp b/text.cpp new file mode 100644 index 0000000..5dc2dfc --- /dev/null +++ b/text.cpp @@ -0,0 +1,24 @@ +#include "text.hpp" +#include + +const size_t SCREEN_WIDTH = 80; +const size_t MAX_ASTERISK = SCREEN_WIDTH - 3 - 1; + +void show_histogram_text(std::vector 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(bins[i]) / max_bin); + for (size_t j = 0; j < height; j++) + { + std::cout << "*"; + } + std::cout << std::endl; + } +} diff --git a/text.hpp b/text.hpp new file mode 100644 index 0000000..b9acbfc --- /dev/null +++ b/text.hpp @@ -0,0 +1,4 @@ +#include +using namespace std; + +void show_histogram_text(vector bins, size_t bin_count);