diff --git a/.gitignore b/.gitignore index 1f0ed89..c659aea 100644 --- a/.gitignore +++ b/.gitignore @@ -4,4 +4,6 @@ /lab_1.layout /unittest.depend /unittest.layout -/curl \ No newline at end of file +/curl +/lab_4.depend +/lab_4.layout diff --git a/histogram.cpp b/histogram.cpp new file mode 100644 index 0000000..b261aeb --- /dev/null +++ b/histogram.cpp @@ -0,0 +1,59 @@ +#include "histogram.h" +#include +#include +using namespace std; + +bool find_minmax( vector numbers, double &min, double &max) +{ + if (numbers.empty()) + { + return false; + } + min = numbers[0]; + max = numbers[0]; + for (double x : numbers) + { + max = x; + if (x < min) + { + min = x; + } + else if (x > max) + { + max = x; + } + } + return true; +} + +vector make_histogram (vector numbers, size_t bin_count) +{ + + double min; + double 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; +} diff --git a/histogram.h b/histogram.h new file mode 100644 index 0000000..c750551 --- /dev/null +++ b/histogram.h @@ -0,0 +1,6 @@ +#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/lab_4.cbp b/lab_4.cbp new file mode 100644 index 0000000..473f2ba --- /dev/null +++ b/lab_4.cbp @@ -0,0 +1,40 @@ + + + + + + diff --git a/sr.cpp b/sr.cpp new file mode 100644 index 0000000..54c4856 --- /dev/null +++ b/sr.cpp @@ -0,0 +1,16 @@ +#include +#include +#include +#include +#include + +using namespace std; + +double sr(const vector& bins) { + size_t totalHeight = 0; + for (size_t bin : bins) { + totalHeight += bin; + } + + return static_cast(totalHeight) / bins.size(); +} diff --git a/sr.h b/sr.h new file mode 100644 index 0000000..fa59a19 --- /dev/null +++ b/sr.h @@ -0,0 +1,7 @@ +#ifndef SR_H_INCLUDED +#define SR_H_INCLUDED +#include + +double sr(const std::vector& bins); + +#endif // SR_H_INCLUDED diff --git a/svg.cpp b/svg.cpp new file mode 100644 index 0000000..4edb6d8 --- /dev/null +++ b/svg.cpp @@ -0,0 +1,98 @@ +#include +#include +#include +#include +#include +#include "svg.h" +//#include "sr.h" + +//using namespace std; +// +//double sr(const vector& bins) { +// size_t totalHeight = 0; +// for (size_t bin : bins) { +// totalHeight += bin; +// } +// +// return static_cast(totalHeight) / bins.size(); +//} + + + + +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 = "black", string fill = "black") +{ + 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 BLACK = "black"; + const auto RED = "red"; + const auto GREEN = "green"; + const auto MAX_WIDTH = IMAGE_WIDTH-TEXT_WIDTH; + + size_t x = sr(bins); + + svg_begin(IMAGE_WIDTH,IMAGE_HEIGHT); + + double top = 0; + double max_count = bins[0]; + for (size_t i = 0; i < bins.size(); i++) + { + if (max_count + +void +show_histogram_svg(const std::vector& bins); + +#endif // SVG_H_INCLUDED diff --git a/text.cpp b/text.cpp new file mode 100644 index 0000000..5ae4546 --- /dev/null +++ b/text.cpp @@ -0,0 +1,53 @@ +#include +#include +#include "text.h" +using namespace std; + +void show_histogram_text(vector bins, size_t bin_count) +{ + const size_t screen_width = 80; + const size_t max_asterisk = screen_width - 3 - 1; + size_t i,j; + double max_count; + max_count = bins[0]; + for (i=0; i< bin_count; i++) + { + if (max_countmax_asterisk) + { + flag=true; + } + for (j = 0; j < bin_count; j++) + { + if (bins[j] < 100) + { + cout << " "; + } + if (bins[j] < 10) + { + cout << " "; + } + cout << bins[j] << "|"; + + if (flag) + { + height = max_asterisk * (static_cast(bins[j]) / max_count); + } + else + { + height=bins[j]; + } + for (i = 0; i < height; i++) + { + cout << "*"; + } + cout << endl; + } +} diff --git a/text.h b/text.h new file mode 100644 index 0000000..bd0f200 --- /dev/null +++ b/text.h @@ -0,0 +1,7 @@ +#ifndef TEXT_H_INCLUDED +#define TEXT_H_INCLUDED + +void +show_histogram_text(std::vector bins, size_t bin_count); + +#endif // TEXT_H_INCLUDED