diff --git a/bin/Debug/I.txt.txt b/bin/Debug/I.txt.txt new file mode 100644 index 0000000..69b5758 --- /dev/null +++ b/bin/Debug/I.txt.txt @@ -0,0 +1,6 @@ +142 +1 1 1 1 1 1 1 1 1 +2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 +3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 +3 +15 \ No newline at end of file diff --git a/bin/Debug/devlab1.exe b/bin/Debug/devlab1.exe index e8cbea5..bdc69c1 100644 Binary files a/bin/Debug/devlab1.exe and b/bin/Debug/devlab1.exe differ diff --git a/bin/Debug/input.txt b/bin/Debug/input.txt index 9e79f5d..8739895 100644 --- a/bin/Debug/input.txt +++ b/bin/Debug/input.txt @@ -1,4 +1,4 @@ 10 3 3 3 4 4 4 4 4 5 5 3 -12 \ No newline at end of file +16 \ No newline at end of file diff --git a/bin/Debug/test.svg b/bin/Debug/test.svg new file mode 100644 index 0000000..7d85b15 --- /dev/null +++ b/bin/Debug/test.svg @@ -0,0 +1,6 @@ + + +9 +33 +100 + diff --git a/bin/Debug/unittest.exe b/bin/Debug/unittest.exe index 9c487f1..8d3a796 100644 Binary files a/bin/Debug/unittest.exe and b/bin/Debug/unittest.exe differ diff --git a/devlab1.depend b/devlab1.depend index f77c49c..826bae6 100644 --- a/devlab1.depend +++ b/devlab1.depend @@ -1,5 +1,5 @@ # depslib dependency file v1.0 -1681739567 source:c:\program files\codeblocks\devlab1\main.cpp +1682334548 source:c:\program files\codeblocks\devlab1\main.cpp @@ -9,7 +9,7 @@ 1681733870 c:\program files\codeblocks\devlab1\histogram.h -1681738471 source:c:\program files\codeblocks\devlab1\histogram.cpp +1682342839 source:c:\program files\codeblocks\devlab1\histogram.cpp "histogram.h" @@ -21,10 +21,10 @@ 1680530410 source:c:\program files\codeblocks\devlab1\text.cpp "text.h" -1681734640 c:\program files\codeblocks\devlab1\svg.h +1682337558 c:\program files\codeblocks\devlab1\svg.h -1681739461 source:c:\program files\codeblocks\devlab1\svg.cpp +1682339123 source:c:\program files\codeblocks\devlab1\svg.cpp "svg.h" diff --git a/devlab1.layout b/devlab1.layout index 6b17b07..dd5b9bf 100644 --- a/devlab1.layout +++ b/devlab1.layout @@ -2,44 +2,49 @@ - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + + + + + + diff --git a/histogram.cpp b/histogram.cpp index 17c02b2..f587c5b 100644 --- a/histogram.cpp +++ b/histogram.cpp @@ -2,9 +2,12 @@ #include #include using namespace std; -void +bool find_minmax(const vector& numbers, double& min, double& max) { + if(numbers.empty()) + return false; + min = numbers[0]; max = numbers[0]; for ( size_t i=0; i < numbers.size(); i++) @@ -12,15 +15,16 @@ find_minmax(const vector& numbers, double& min, double& max) if (numbers[i] > max) max=numbers[i]; if (numbers[i] < min) min=numbers[i]; } -}; + return true; +}; std::vector make_histogram(const vector &numbers,size_t bin_count) { float lo,hi,dif; double min, max; - find_minmax(numbers, min, max); - //std::cout< bins(bin_count) ; dif=(max - min)/bin_count; for(int i=0; i < numbers.size(); i++) @@ -35,19 +39,13 @@ make_histogram(const vector &numbers,size_t bin_count) bins[j]++; found = true; } - // std::cout<& numbers, double& min, double& max); #endif // HISTOGRAM_INTERNAL_H_INCLUDED diff --git a/main.cpp b/main.cpp index d0acf30..5d48798 100644 --- a/main.cpp +++ b/main.cpp @@ -5,37 +5,19 @@ #include "svg.h" using namespace std; -int inbl() -{ - int BLOCK_WIDTH; - cerr << "block width is"; - cin>>BLOCK_WIDTH; - if (BLOCK_WIDTH>30) - { - cerr<< "too big, please make it smaller"; - inbl; - } - else if (BLOCK_WIDTH<3) - { - cerr<< "too small, please make it bigger"; - inbl; - } - else return BLOCK_WIDTH; -} struct Input { vector numbers; int bin_count; - int BLOCK_WIDTH; }; Input input_data() { size_t number_count=0; - cerr<<"amount of numbers is"; + cerr<<"amount of numbers is "; cin >> number_count; -Input in; + Input in; in.numbers.resize(number_count); cerr<<"let's intro numbers "; for (size_t i = 0; i < number_count; i++) @@ -43,9 +25,8 @@ Input in; cin >> in.numbers[i]; } size_t bin_count=0; - cerr<<"amount of bins is"; + cerr<<"amount of bins is "; cin >> in.bin_count; - int BLOCK_WIDTH = inbl(); return in; } //4199705 @@ -62,7 +43,7 @@ int main() auto bins = make_histogram(in.numbers, in.bin_count); - show_histogram_svg(bins,in.BLOCK_WIDTH); + show_histogram_svg(bins); return 0; diff --git a/obj/Debug/histogram.o b/obj/Debug/histogram.o index 6724c9e..abc95a4 100644 Binary files a/obj/Debug/histogram.o and b/obj/Debug/histogram.o differ diff --git a/obj/Debug/main.o b/obj/Debug/main.o index 3e14e73..09cd2b8 100644 Binary files a/obj/Debug/main.o and b/obj/Debug/main.o differ diff --git a/obj/Debug/svg.o b/obj/Debug/svg.o index f88a306..c9e3f38 100644 Binary files a/obj/Debug/svg.o and b/obj/Debug/svg.o differ diff --git a/obj/Debug/unittest.o b/obj/Debug/unittest.o index f1f17ab..bcc0027 100644 Binary files a/obj/Debug/unittest.o and b/obj/Debug/unittest.o differ diff --git a/svg.cpp b/svg.cpp index 5d4f64b..4a6f9a4 100644 --- a/svg.cpp +++ b/svg.cpp @@ -4,6 +4,22 @@ using namespace std; +bool inbl(const int& BLOCK_WIDTH) +{ + if (BLOCK_WIDTH>30) + { + cerr<< "too big, please make it smaller\n"; + return false; + } + if (BLOCK_WIDTH<3) + { + cerr<< "too small, please make it bigger\n"; + return false; + } + return true; + +} + void svg_begin(double width, double height) { @@ -33,19 +49,17 @@ void svg_rect(double x, double y, double width, double height, string stroke = " } void -show_histogram_svg(vector & bins, int BLOCK_WIDTH) +show_histogram_svg(vector & bins) { const auto IMAGE_WIDTH = 400; const auto IMAGE_HEIGHT = 300; const auto TEXT_LEFT = 20; const auto TEXT_BASELINE = 20; + const auto TEXT_WIDTH = 50; const auto BIN_HEIGHT = 30; - - -BLOCK_WIDTH=BLOCK_WIDTH+12; - svg_begin(400, 300); + const auto MAX_ASTERISK1 = 30; double top = 0; const size_t MAX_ASTERISK = IMAGE_WIDTH - TEXT_WIDTH; @@ -56,10 +70,22 @@ BLOCK_WIDTH=BLOCK_WIDTH+12; if (bins[j] > maxb) maxb = bins[j]; } - auto maxb1 = maxb * BLOCK_WIDTH; int i = 0; + int BLOCK_WIDTH; + + while (true){ + cerr << "block width: "; + cin >> BLOCK_WIDTH; + if(inbl(BLOCK_WIDTH)) + break; + } + + auto maxb1 = maxb * BLOCK_WIDTH; + + svg_begin(IMAGE_WIDTH, IMAGE_HEIGHT); + for (size_t bin : bins) { @@ -69,9 +95,13 @@ BLOCK_WIDTH=BLOCK_WIDTH+12; if (maxb1 > MAX_ASTERISK) { - bin_width = BLOCK_WIDTH * MAX_ASTERISK * (bin / maxb1); + bin_width = MAX_ASTERISK * (static_cast(bin) / maxb); } - else bin_width = BLOCK_WIDTH * bin; + else + bin_width = BLOCK_WIDTH * bin; + + cerr << "\n" << bin_width << "\n"; + svg_text(TEXT_LEFT, top + TEXT_BASELINE, to_string(bin)); svg_rect(TEXT_WIDTH, top, bin_width, BIN_HEIGHT, "violet", to_string(color)); diff --git a/svg.h b/svg.h index a2d58c6..985c546 100644 --- a/svg.h +++ b/svg.h @@ -2,4 +2,5 @@ #include void -show_histogram_svg(std::vector& bins, int BLOCK_WIDTH); +show_histogram_svg(std::vector& bins); +bool inbl(const int& BLOCK_WIDTH); diff --git a/unittest.cbp b/unittest.cbp index 379d0a2..65bd1be 100644 --- a/unittest.cbp +++ b/unittest.cbp @@ -34,6 +34,8 @@ + + diff --git a/unittest.cpp b/unittest.cpp index 981aa6f..54b1fa6 100644 --- a/unittest.cpp +++ b/unittest.cpp @@ -2,7 +2,20 @@ #define DOCTEST_CONFIG_IMPLEMENT_WITH_MAIN #include "doctest.h" #include "histogram_internal.h" +#include "svg.h" +TEST_CASE("Too big") { + int BLOCKWIDTH =40; + CHECK(inbl(BLOCKWIDTH) == false); +} +TEST_CASE("Too small") { + int BLOCKWIDTH =1; + CHECK(inbl(BLOCKWIDTH) == false); +} +TEST_CASE("ok ") { + int BLOCKWIDTH =12; + CHECK(inbl(BLOCKWIDTH) == true); +} TEST_CASE("distinct positive numbers") { double min = 0; double max = 0; @@ -34,3 +47,13 @@ TEST_CASE("distinct negative numbers") { CHECK(min == -3); CHECK(max == -1); } +TEST_CASE("empty arr") { + double min = 0; + double max = 0; + + CHECK(find_minmax({}, min, max) == false); + CHECK(find_minmax({3,4}, min, max) == true); + +; +} + diff --git a/unittest.depend b/unittest.depend index 9a50203..2102d2c 100644 --- a/unittest.depend +++ b/unittest.depend @@ -1,14 +1,16 @@ # depslib dependency file v1.0 -1680530029 source:c:\program files\codeblocks\devlab1\histogram.cpp +1682343064 source:c:\program files\codeblocks\devlab1\histogram.cpp "histogram.h" + -1680530141 c:\program files\codeblocks\devlab1\histogram.h +1681733870 c:\program files\codeblocks\devlab1\histogram.h -1681719645 source:c:\program files\codeblocks\devlab1\unittest.cpp +1682343068 source:c:\program files\codeblocks\devlab1\unittest.cpp "doctest.h" "histogram_internal.h" + "svg.h" 1681707623 c:\program files\codeblocks\devlab1\doctest.h @@ -55,5 +57,13 @@ -1681730258 c:\program files\codeblocks\devlab1\histogram_internal.h +1682343113 c:\program files\codeblocks\devlab1\histogram_internal.h + +1682339123 source:c:\program files\codeblocks\devlab1\svg.cpp + "svg.h" + + + +1682337558 c:\program files\codeblocks\devlab1\svg.h + diff --git a/unittest.layout b/unittest.layout index 547206b..a86c868 100644 --- a/unittest.layout +++ b/unittest.layout @@ -2,19 +2,14 @@ - - - - - - + - + - + @@ -22,4 +17,9 @@ + + + + +