diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..f6f3fbd --- /dev/null +++ b/.gitignore @@ -0,0 +1,3 @@ +/bin +/obj +/1.layout diff --git a/1.depend b/1.depend new file mode 100644 index 0000000..f1c7256 --- /dev/null +++ b/1.depend @@ -0,0 +1,53 @@ +# depslib dependency file v1.0 +1677500178 source:l:\i курс\А-2-22\Коновалова\1\main.cpp + + + +1681133505 source:c:\users\u111-09\desktop\1\main.cpp + + + "histogram.h" + +1681133536 source:c:\users\u111-09\desktop\1\histogram.cpp + + + "histogram.h" + +1681131906 c:\users\u111-09\desktop\1\histogram.h + + +1681132321 source:c:\users\u111-09\desktop\1\text.cpp + + + +1684753111 source:c:\users\79156\desktop\3\main.cpp + + + + + "histogram.h" + "svg.h" + +1681131908 c:\users\79156\desktop\3\histogram.h + + +1682253694 c:\users\79156\desktop\3\svg.h + + +1682333182 source:c:\users\79156\desktop\3\histogram.cpp + + + + "histogram.h" + +1682263714 c:\users\79156\desktop\3\histogram_internal.h + + +1682340835 source:c:\users\79156\desktop\3\svg.cpp + + + + + + "svg.h" + diff --git a/histogram.cpp b/histogram.cpp new file mode 100644 index 0000000..ae85b54 --- /dev/null +++ b/histogram.cpp @@ -0,0 +1,62 @@ +#include +#include +#include +//#include "histogram_internal.h" +#include "histogram.h" + +using namespace std; + + +bool +find_minmax(vector numbers, double& min, double& max) +{ + if(numbers.size() == 0) + return false; + + min = numbers[0]; + max = numbers[0]; + size_t number_count = numbers.size(); + for (size_t i = 1; i < number_count; i++) + { + if (numbers[i] < min) + { + min = numbers[i]; + } + else if (numbers[i] > max) + { + max = numbers[i]; + } + } + return true; +} + +vector +make_histogram(const vector& numbers, size_t bin_count) +{ + + double min, max; + vector bins(bin_count); + find_minmax(numbers, min, max); + double bin_size = (max - min) / bin_count; + size_t number_count = numbers.size(); + + for (size_t i = 0; i < number_count; 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.h b/histogram.h new file mode 100644 index 0000000..9be7bb9 --- /dev/null +++ b/histogram.h @@ -0,0 +1,11 @@ +#ifndef HISTOGRAM_H_INCLUDED +#define HISTOGRAM_H_INCLUDED + +#include + +std::vector +make_histogram(const std::vector& numbers, size_t bin_count); + +#endif // HISTOGRAM_H_INCLUDED + + diff --git a/histogram_internal.h b/histogram_internal.h new file mode 100644 index 0000000..a3ac1b5 --- /dev/null +++ b/histogram_internal.h @@ -0,0 +1,9 @@ +#ifndef HISTOGRAM_INTERNAL_H_INCLUDED +#define HISTOGRAM_INTERNAL_H_INCLUDED + +#include + +bool +find_minmax(std::vector numbers, double& min, double& max); + +#endif // HISTOGRAM_INTERNAL_H_INCLUDED diff --git a/main.cpp b/main.cpp index 58b1f10..7604550 100644 --- a/main.cpp +++ b/main.cpp @@ -15,10 +15,10 @@ struct Input Input -input_data() +input_data(istream& tin) { size_t number_count; - cerr << "Enter number_count "; cin >> number_count; + cerr << "Enter number_count "; tin >> number_count; vector numbers(number_count); @@ -32,7 +32,7 @@ input_data() size_t bin_count; cerr << "Enter bin_count "; - cin >> in.bin_count; + tin >> in.bin_count; return in; } @@ -44,7 +44,7 @@ input_data() int main() { - auto in = input_data(); + auto in = input_data(cin); auto bins = make_histogram(in.numbers, in.bin_count); show_histogram_svg(bins); return 0; diff --git a/main.exe b/main.exe new file mode 100644 index 0000000..25dd4ab Binary files /dev/null and b/main.exe differ diff --git a/svg.cbp b/svg.cbp new file mode 100644 index 0000000..ceb7a55 --- /dev/null +++ b/svg.cbp @@ -0,0 +1,40 @@ + + + + + + diff --git a/svg.cpp b/svg.cpp new file mode 100644 index 0000000..36f687b --- /dev/null +++ b/svg.cpp @@ -0,0 +1,73 @@ +#include +#include +#include +#include +#include +#include "svg.h" +using namespace std; + +void +svg_begin(double width, double height) { + cout << "\n"; + cout << " \n"; +} + +void +svg_end() { + cout << "\n"; +} + +void +svg_text(double left, double baseline, string text) { + cout << "" << text << ""; +} + +void +svg_rect(double x, double y, double width, double height, string stroke, string fill) { +cout << ""; + +} + + + +void +show_histogram_svg(const 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; + const auto BLOCK_WIDTH = 10; + const auto MAX_WIDTH = IMAGE_WIDTH-TEXT_WIDTH; + + + svg_begin(IMAGE_WIDTH,IMAGE_HEIGHT); + + double top = 0; + double max_count = bins[0]; + for (size_t i = 0; i < bins.size(); i++) { + if (bins[i] > max_count) + max_count = bins[i]; + } + + for (size_t bin : bins) { + const double bin_width = (IMAGE_WIDTH - TEXT_WIDTH)*(bin/max_count); + svg_text(TEXT_LEFT, top + TEXT_BASELINE, to_string(bin)); + svg_rect(TEXT_WIDTH, top, bin_width, BIN_HEIGHT, "black", "#fde910"); + + top += BIN_HEIGHT; + + } + cout << "\n"; + cout << "\n"; + cout << "\n"; + cout << "\n"; + svg_end(); +} + + diff --git a/svg.depend b/svg.depend new file mode 100644 index 0000000..e63c9b7 --- /dev/null +++ b/svg.depend @@ -0,0 +1,19 @@ +# depslib dependency file v1.0 +1682280278 source:c:\users\79156\desktop\3\svg.cpp + + + + + + "svg.h" + +1682253694 c:\users\79156\desktop\3\svg.h + + +1682252851 c:\users\79156\desktop\3\svg.cpp + + + + + "svg.h" + diff --git a/svg.h b/svg.h new file mode 100644 index 0000000..bc76ceb --- /dev/null +++ b/svg.h @@ -0,0 +1,9 @@ +#ifndef SVG_H_INCLUDED +#define SVG_H_INCLUDED +//#include "svg.cpp" +#include + +void +show_histogram_svg(const std::vector& bins); + +#endif // SVG_H_INCLUDED diff --git a/svg.layout b/svg.layout new file mode 100644 index 0000000..acac677 --- /dev/null +++ b/svg.layout @@ -0,0 +1,15 @@ + + + + + + + + + + + + + + + diff --git a/test.cbp b/test.cbp new file mode 100644 index 0000000..7dd4057 --- /dev/null +++ b/test.cbp @@ -0,0 +1,44 @@ + + + + + + diff --git a/test.depend b/test.depend new file mode 100644 index 0000000..5a9c277 --- /dev/null +++ b/test.depend @@ -0,0 +1,130 @@ +# depslib dependency file v1.0 +1681135804 source:c:\users\u111-09\desktop\1\histogram.cpp + + + "histogram_internal.h" + +1681131906 c:\users\u111-09\desktop\1\histogram.h + + +1681135819 c:\users\u111-09\desktop\1\histogram_internal.h + + +1681135806 source:d:\3\histogram.cpp + + + "histogram_internal.h" + +1681135820 d:\3\histogram_internal.h + + +1682078902 source:d:\3\unittest.cpp + "doctest.h" + "histogram_internal.h" + +1681134424 d:\3\doctest.h + + + + + + + "doctest_fwd.h" + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + +1682333182 source:c:\users\79156\desktop\3\histogram.cpp + + + + "histogram.h" + +1682282091 c:\users\79156\desktop\3\histogram_internal.h + + +1682338695 source:c:\users\79156\desktop\3\unittest.cpp + "doctest.h" + "histogram_internal.h" + +1681134424 c:\users\79156\desktop\3\doctest.h + + + + + + + "doctest_fwd.h" + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + +1681131908 c:\users\79156\desktop\3\histogram.h + + diff --git a/test.layout b/test.layout new file mode 100644 index 0000000..fd00561 --- /dev/null +++ b/test.layout @@ -0,0 +1,25 @@ + + + + + + + + + + + + + + + + + + + + + + + + + diff --git a/text.cpp b/text.cpp new file mode 100644 index 0000000..75be88f --- /dev/null +++ b/text.cpp @@ -0,0 +1,23 @@ +#include +#include + +using namespace std; + +void show_histogram_text(vector bins, size_t bin_count){ + + for (size_t i = 0; i < bin_count; i++){ + if (bins[i]<100){ + cout << " "; + } + if (bins[i]<10){ + cout << " "; + } + cout << bins[i] << "|"; + for (size_t j = 0; j < bins[i]; j++){ + cout << "*"; + } + cout << "\n"; + } +} + + diff --git a/text.h b/text.h new file mode 100644 index 0000000..aa6bb49 --- /dev/null +++ b/text.h @@ -0,0 +1,9 @@ +#ifndef TEXT_H_INCLUDED +#define TEXT_H_INCLUDED + +#include + +void +show_histogram_text(std::vector bins, size_t bin_count); + +#endif // TEXT_H_INCLUDED diff --git a/unittest.cpp b/unittest.cpp new file mode 100644 index 0000000..38fafae --- /dev/null +++ b/unittest.cpp @@ -0,0 +1,61 @@ +#define DOCTEST_CONFIG_NO_MULTITHREADING +#define DOCTEST_CONFIG_IMPLEMENT_WITH_MAIN +#include "doctest.h" +#include "histogram_internal.h" + + + + TEST_CASE("null vector") { + double min = 0; + double max = 0; + find_minmax({}, min, max); + CHECK(find_minmax({}, min, max) == true); + CHECK(min == 1); + CHECK(max == 2); + + CHECK(find_minmax({}, min, max) == false); + } + + TEST_CASE("one number") { + double min = 0; + double max = 0; + find_minmax({1}, min, max); + CHECK(find_minmax({1}, min, max) == true); + CHECK(min == 1); + CHECK(max == 2); + + CHECK(find_minmax({}, min, max) == false); + } + + TEST_CASE("negative numbers") { + double min = 0; + double max = 0; + find_minmax({-1, -2}, min, max); + CHECK(find_minmax({-1, -2}, min, max) == true); + CHECK(min == 1); + CHECK(max == 2); + + CHECK(find_minmax({}, min, max) == false); + } + + TEST_CASE("same numbers") { + double min = 0; + double max = 0; + find_minmax({1, 1}, min, max); + CHECK(find_minmax({1, 1}, min, max) == true); + CHECK(min == 1); + CHECK(max == 2); + + CHECK(find_minmax({}, min, max) == false); + } + + TEST_CASE("distinct positive numbers") { + double min = 0; + double max = 0; + find_minmax({1, 2}, min, max); + CHECK(find_minmax({1, 2}, min, max) == true); + CHECK(min == 1); + CHECK(max == 2); + + CHECK(find_minmax({}, min, max) == false); + } diff --git a/unittest.depend b/unittest.depend new file mode 100644 index 0000000..a61bc11 --- /dev/null +++ b/unittest.depend @@ -0,0 +1,9 @@ +# depslib dependency file v1.0 +1681133536 source:c:\users\u111-09\desktop\1\histogram.cpp + + + "histogram.h" + +1681131906 c:\users\u111-09\desktop\1\histogram.h + +