diff --git a/.DS_Store b/.DS_Store index 810bd47..66f072b 100644 Binary files a/.DS_Store and b/.DS_Store differ diff --git a/Gistogramma.xcodeproj/project.pbxproj b/Gistogramma.xcodeproj/project.pbxproj index 7edc636..c01d126 100644 --- a/Gistogramma.xcodeproj/project.pbxproj +++ b/Gistogramma.xcodeproj/project.pbxproj @@ -11,6 +11,7 @@ 52D010C52DC397980091060B /* histogram.cpp in Sources */ = {isa = PBXBuildFile; fileRef = 52D010C42DC397980091060B /* histogram.cpp */; }; 52D010C62DC39F250091060B /* histogram.hpp in Sources */ = {isa = PBXBuildFile; fileRef = 52D010C32DC397980091060B /* histogram.hpp */; }; 52FD91E52DC3AA580037B82C /* text.cpp in Sources */ = {isa = PBXBuildFile; fileRef = 52FD91E42DC3AA580037B82C /* text.cpp */; }; + 52FD92082DC3CAAB0037B82C /* svg.cpp in Sources */ = {isa = PBXBuildFile; fileRef = 52FD92072DC3CAAB0037B82C /* svg.cpp */; }; /* End PBXBuildFile section */ /* Begin PBXCopyFilesBuildPhase section */ @@ -32,6 +33,10 @@ 52E197562D5F35A1004F8E3E /* Gistogramma */ = {isa = PBXFileReference; explicitFileType = "compiled.mach-o.executable"; includeInIndex = 0; path = Gistogramma; sourceTree = BUILT_PRODUCTS_DIR; }; 52FD91E32DC3AA580037B82C /* text.hpp */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.cpp.h; path = text.hpp; sourceTree = ""; }; 52FD91E42DC3AA580037B82C /* text.cpp */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.cpp.cpp; path = text.cpp; sourceTree = ""; }; + 52FD91F72DC3BE4E0037B82C /* .gitignore */ = {isa = PBXFileReference; lastKnownFileType = text; path = .gitignore; sourceTree = ""; }; + 52FD91FA2DC3C02E0037B82C /* histogram_internal.hpp */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.cpp.h; path = histogram_internal.hpp; sourceTree = ""; }; + 52FD92062DC3CAAB0037B82C /* svg.hpp */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.cpp.h; path = svg.hpp; sourceTree = ""; }; + 52FD92072DC3CAAB0037B82C /* svg.cpp */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.cpp.cpp; path = svg.cpp; sourceTree = ""; }; /* End PBXFileReference section */ /* Begin PBXFrameworksBuildPhase section */ @@ -48,11 +53,15 @@ 52E1974D2D5F35A1004F8E3E = { isa = PBXGroup; children = ( + 52FD91FA2DC3C02E0037B82C /* histogram_internal.hpp */, 52FD91E32DC3AA580037B82C /* text.hpp */, 52FD91E42DC3AA580037B82C /* text.cpp */, 52D010C32DC397980091060B /* histogram.hpp */, 52D010C42DC397980091060B /* histogram.cpp */, + 52FD92062DC3CAAB0037B82C /* svg.hpp */, + 52FD92072DC3CAAB0037B82C /* svg.cpp */, 523FBF7D2DA909BA0044A741 /* main.cpp */, + 52FD91F72DC3BE4E0037B82C /* .gitignore */, 52E197572D5F35A1004F8E3E /* Products */, ); sourceTree = ""; @@ -126,6 +135,7 @@ buildActionMask = 2147483647; files = ( 523FBF7E2DA909BA0044A741 /* main.cpp in Sources */, + 52FD92082DC3CAAB0037B82C /* svg.cpp in Sources */, 52D010C52DC397980091060B /* histogram.cpp in Sources */, 52FD91E52DC3AA580037B82C /* text.cpp in Sources */, 52D010C62DC39F250091060B /* histogram.hpp in Sources */, diff --git a/Gistogramma.xcodeproj/project.xcworkspace/xcuserdata/macbookpro.xcuserdatad/UserInterfaceState.xcuserstate b/Gistogramma.xcodeproj/project.xcworkspace/xcuserdata/macbookpro.xcuserdatad/UserInterfaceState.xcuserstate index 06df49d..309b219 100644 Binary files a/Gistogramma.xcodeproj/project.xcworkspace/xcuserdata/macbookpro.xcuserdatad/UserInterfaceState.xcuserstate and b/Gistogramma.xcodeproj/project.xcworkspace/xcuserdata/macbookpro.xcuserdatad/UserInterfaceState.xcuserstate differ diff --git a/Gistogramma.xcodeproj/xcuserdata/macbookpro.xcuserdatad/xcdebugger/Breakpoints_v2.xcbkptlist b/Gistogramma.xcodeproj/xcuserdata/macbookpro.xcuserdatad/xcdebugger/Breakpoints_v2.xcbkptlist index 67fde3e..e5b0f72 100644 --- a/Gistogramma.xcodeproj/xcuserdata/macbookpro.xcuserdatad/xcdebugger/Breakpoints_v2.xcbkptlist +++ b/Gistogramma.xcodeproj/xcuserdata/macbookpro.xcuserdatad/xcdebugger/Breakpoints_v2.xcbkptlist @@ -110,11 +110,123 @@ filePath = "main.cpp" startingColumnNumber = "9223372036854775807" endingColumnNumber = "9223372036854775807" - startingLineNumber = "86" - endingLineNumber = "86" + startingLineNumber = "34" + endingLineNumber = "34" landmarkName = "main()" landmarkType = "9"> + + + + + + + + + + + + + + + + + + + + + + + + + + + + diff --git a/Gistogramma.xcodeproj/xcuserdata/macbookpro.xcuserdatad/xcschemes/xcschememanagement.plist b/Gistogramma.xcodeproj/xcuserdata/macbookpro.xcuserdatad/xcschemes/xcschememanagement.plist index f0752e7..5d26295 100644 --- a/Gistogramma.xcodeproj/xcuserdata/macbookpro.xcuserdatad/xcschemes/xcschememanagement.plist +++ b/Gistogramma.xcodeproj/xcuserdata/macbookpro.xcuserdatad/xcschemes/xcschememanagement.plist @@ -10,5 +10,13 @@ 0 + SuppressBuildableAutocreation + + 52E197552D5F35A1004F8E3E + + primary + + + diff --git a/histogram.cpp b/histogram.cpp index 4cda481..c337730 100644 --- a/histogram.cpp +++ b/histogram.cpp @@ -3,14 +3,15 @@ using namespace std; -void find_minmax(const vector& numbers, double& minimum, double& maximum){ - if (numbers.size() == 0) return; +bool find_minmax(const vector& numbers, double& minimum, double& maximum){ + if (numbers.size() == 0) return 1; minimum = numbers[0]; maximum = numbers[0]; for (auto i : numbers) { if (i < minimum) minimum = i; if (i > maximum) maximum = i; } + return 0; } vector make_histogram(const vector& numbers, size_t& bin_count) { diff --git a/histogram_internal.hpp b/histogram_internal.hpp index 77a847d..4cfb67a 100644 --- a/histogram_internal.hpp +++ b/histogram_internal.hpp @@ -2,6 +2,6 @@ #define histogram_internal_h #include -void find_minmax(const std::vector& numbers, double& minimum, double& maximum); +bool find_minmax(const std::vector& numbers, double& minimum, double& maximum); #endif /* histogram_internal_h */ diff --git a/svg.cpp b/svg.cpp index ceb521f..5b148ed 100644 --- a/svg.cpp +++ b/svg.cpp @@ -29,9 +29,12 @@ void show_histogram_svg(const vector& bins) { const auto TEXT_WIDTH = 50; const auto BIN_HEIGHT = 30; const auto BLOCK_WIDTH = 10; - const auto MAX_BIN_WIDTH = IMAGE_WIDTH - TEXT_WIDTH; + const auto MAX_BIN_WIDTH = IMAGE_WIDTH - 2.3 * TEXT_WIDTH; auto max_bins = bins[0]; - + size_t sum_bins = 0; + + for (size_t bin : bins) sum_bins += bin; + for (size_t bin : bins) { if (bin > max_bins) max_bins = bin; } @@ -42,8 +45,10 @@ void show_histogram_svg(const vector& bins) { double top = 0; for (size_t bin : bins) { const double bin_width = BLOCK_WIDTH * bin * koef; + size_t percent = bin * 100 / sum_bins; svg_text(TEXT_LEFT, top + TEXT_BASELINE, to_string(bin)); svg_rect(TEXT_WIDTH, top, bin_width, BIN_HEIGHT); + svg_text((IMAGE_WIDTH - TEXT_WIDTH), top + TEXT_BASELINE, to_string(percent)); top += BIN_HEIGHT; } svg_end(); diff --git a/text.cpp b/text.cpp index 929422d..66b0b8d 100644 --- a/text.cpp +++ b/text.cpp @@ -7,7 +7,7 @@ using namespace std; void show_histogram_text(const vector& baskets) { const size_t screen_width = 80; const size_t max_asterisk = screen_width - 3 - 1; - cout.precision(4); + cout.precision(0); size_t baskets_max_count; baskets_max_count = baskets[0]; diff --git a/unittest.cpp b/unittest.cpp index cbad44d..6b2e4bf 100644 --- a/unittest.cpp +++ b/unittest.cpp @@ -14,11 +14,10 @@ TEST_CASE("distinct positive numbers") { } TEST_CASE("empty vector"){ - double min = NULL; - double max = NULL; - find_minmax({}, min, max); - CHECK(min == NULL); - CHECK(max == NULL); + double min = 0; + double max = 0; + bool check = find_minmax({}, min, max); + CHECK(check == 1); } TEST_CASE("single vector"){