diff --git a/histogram.cpp b/histogram.cpp new file mode 100644 index 0000000..95417a4 --- /dev/null +++ b/histogram.cpp @@ -0,0 +1,86 @@ +/*#include "histogram.h" + +void +find_minmax(const std::vector& numbers, double& min, double& max) { + min = numbers[0]; + max = numbers[0]; + for (double x : numbers){ + if (x < min){ + min = x; + } + else if (x > max){ + max = x; + } + + } +} + +std::vector make_histogram(const std::vector& numbers, size_t& bin_count){ + std::vector bins(bin_count); + double max, min = 0; + find_minmax(numbers, min, max); + double bin_size = (max - min) / 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; +} +*/ +#include "histogram.h" +void +find_minmax(const std::vector& numbers, double& min, double& max) { + min = numbers[0]; + max = numbers[0]; + for (double x : numbers) + { + if (x < min){ + min = x; + } + else if (x > max){ + max = x; + } + } +} + +std::vector +make_histogram(const std::vector& numbers, size_t & bin_count, size_t & number_count, size_t & max_count) { + double min, max; + find_minmax(numbers, min, max); + double bin_size = (max - min) / bin_count; + std::vector bins(bin_count); + max_count = bins[0]; + 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 (bins[j] > max_count){ + max_count = bins[j]; + } + } + if (!found){ + bins[bin_count - 1]++; + } + if (bins[bin_count - 1] > max_count){ + max_count = bins[bin_count - 1]; + } + } + return bins; +} diff --git a/histogram.h b/histogram.h new file mode 100644 index 0000000..ca901fe --- /dev/null +++ b/histogram.h @@ -0,0 +1,12 @@ +/*#pragma once +#include +#include + +std::vector +make_histogram(const std::vector& numbers, size_t & bin_count);*/ +#pragma once +#include +#include + +std::vector +make_histogram(const std::vector& numbers, size_t & bin_count, size_t & number_count, size_t & max_count); diff --git a/histogram_internal.h b/histogram_internal.h new file mode 100644 index 0000000..0c4f9ad --- /dev/null +++ b/histogram_internal.h @@ -0,0 +1,12 @@ +/*#pragma once +#include +#include + +void +find_minmax(const std::vector& numbers, double& min, double& max);*/ +#pragma once +#include +#include + +void +find_minmax(const std::vector& numbers, double& min, double& max); diff --git a/main.cpp b/main.cpp index c2883a7..d001a1f 100644 --- a/main.cpp +++ b/main.cpp @@ -1,5 +1,9 @@ -#include +/*#include #include +#include "histogram.h" +#include "text.h" +#include "histogram_internal.h" +#include "svg.h" using namespace std; @@ -14,7 +18,7 @@ Input input_data() { size_t number_count; cerr<<"Enter number count"; - cin >> number_count; + cin>>number_count; Input in; in.numbers.resize(number_count); vector numbers(number_count); @@ -27,79 +31,51 @@ cerr<<"Enter bin count"; cin >> in.bin_count; return in; } -void -find_minmax(const vector& numbers, double& min, double& max) { - min = numbers[0]; - max = numbers[0]; - for (double x : numbers){ - if (x < min){ - min = x; - } - else if (x > max){ - max = x; - } - } -} -vector make_histogram(const vector numbers, size_t bin_count){ - vector bins(bin_count); - double max, min = 0; - find_minmax(numbers, min, max); - double bin_size = (max - min) / bin_count; +int main() +{ + auto in = input_data(); + auto bins = make_histogram(in.numbers, in.bin_count); + show_histogram_svg(bins, in.max_count, in.bin_count); + return 0; +}*/ +#include +#include +#include "histogram.h" +#include "text.h" +#include "svg.h" +using namespace std; +struct Input { + vector numbers; + size_t bin_count{}; + size_t number_count{}; + size_t max_count{}; +}; +Input +input_data() { + Input in; + cerr << "Enter number count: "; + cin >> in.number_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]++; - } + vector numbers(in.number_count); + in.numbers.resize(in.number_count); + for (size_t i = 0; i < in.number_count; i++) { + cin >> in.numbers[i]; } - return bins; -} - -void -show_histogram_text(vector& bins, size_t & max_count,size_t & bin_count){ -const size_t SCREEN_WIDTH = 80; -const size_t MAX_ASTERISK = SCREEN_WIDTH - 3 - 1; -if (max_count > MAX_ASTERISK){ - vector hights(bin_count); - for (size_t i = 0; i < bin_count; i++){ - size_t height = MAX_ASTERISK * (static_cast(bins[i]) / max_count); - hights[i] = height; - } - for (size_t i = 0; i < bin_count; i++){ - printf("%3d|", bins[i]); - for (size_t j = 0; j < hights[i]; j++){ - cout<<"*"; - } - cout << endl; - } - } - else{ - for (size_t i = 0; i < bin_count; i++) - { - printf("%3d|", bins[i]); - for (size_t j = 0; j < bins[i]; j++){ - cout<<"*"; - } - cout << endl; - } - } + size_t bin_count; + cerr << "Enter bin count: "; + cin >> in.bin_count; + size_t max_count; + in.max_count = 0; + return in; } - int main() { auto in = input_data(); - auto bins = make_histogram(in.numbers, in.bin_count); - show_histogram_text(bins, in.max_count, in.bin_count); + auto bins = make_histogram(in.numbers, in.bin_count, in.number_count, in.max_count); + show_histogram_svg(bins, in.max_count, in.bin_count); + return 0; } diff --git a/svg.cpp b/svg.cpp new file mode 100644 index 0000000..058b41f --- /dev/null +++ b/svg.cpp @@ -0,0 +1,49 @@ +#include "svg.h" + +void +svg_begin(double width, double height) { + std::cout << "\n"; + std::cout << "\n"; +} + +void +svg_text(double left, double baseline, std::string text) { + std::cout << ""<< text <<""; +} + +void svg_rect(double x, double y, double width, double height,std::string stroke = "black", std::string fil = "black"){ + std::cout<< ""; +} + +void +show_histogram_svg(std::vector& bins, size_t & max_count,size_t & bin_count) { + + 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 = (IMAGE_WIDTH - TEXT_WIDTH)/max_count; + + svg_begin(IMAGE_WIDTH, IMAGE_HEIGHT); + + double top = 0; + for (size_t bin : bins) { + const double bin_width = BLOCK_WIDTH * bin; + svg_text(TEXT_LEFT, top + TEXT_BASELINE, std::to_string(bin)); + svg_rect(TEXT_WIDTH, top, bin_width, BIN_HEIGHT); + top += BIN_HEIGHT; + } + svg_end(); +} + +void +svg_end() { + std::cout << "\n"; +} + diff --git a/svg.h b/svg.h new file mode 100644 index 0000000..842eaf7 --- /dev/null +++ b/svg.h @@ -0,0 +1,29 @@ +/*#pragma once +#include +#include + +void +show_histogram_svg(std::vector& bins, size_t & max_count,size_t & bin_count); +void +svg_begin(double width, double height); +void +svg_text(double left, double baseline, std::string text); +void +svg_rect(double x, double y, double width, double height,std::string stroke, std::string fil); +void +svg_end();*/ +#pragma once +#include +#include +//#include + +void +show_histogram_svg(std::vector& bins, size_t & max_count,size_t & bin_count); +void +svg_begin(double width, double height); +void +svg_text(double left, double baseline, std::string text); +void +svg_rect(double x, double y, double width, double height, double fill_opacity, std::string stroke, std::string fil); +void +svg_end(); diff --git a/text.cpp b/text.cpp new file mode 100644 index 0000000..58c81bc --- /dev/null +++ b/text.cpp @@ -0,0 +1,67 @@ +/*#include "histogram.h" + +void +show_histogram_text(std::vector& bins, size_t & max_count,size_t & bin_count){ + const size_t SCREEN_WIDTH = 80; + const size_t MAX_ASTERISK = SCREEN_WIDTH - 3 - 1; + if (max_count > MAX_ASTERISK){ + std::vector hights(bin_count); + for (size_t i = 0; i < bin_count; i++){ + size_t height = MAX_ASTERISK * (static_cast(bins[i]) / max_count); + hights[i] = height; + } + + for (size_t i = 0; i < bin_count; i++){ + printf("%3d|", bins[i]); + for (size_t j = 0; j < hights[i]; j++){ + std::cout<<"*"; + } + std::cout << std::endl; + } + } + else{ + for (size_t i = 0; i < bin_count; i++) + { + printf("%3d|", bins[i]); + for (size_t j = 0; j < bins[i]; j++){ + std::cout<<"*"; + } + std::cout << std::endl; + } + } + +} +*/ +#include "histogram.h" + +void +show_histogram_text(std::vector& bins, size_t & max_count,size_t & bin_count){ + const size_t SCREEN_WIDTH = 80; + const size_t MAX_ASTERISK = SCREEN_WIDTH - 3 - 1; + if (max_count > MAX_ASTERISK){ + std::vector hights(bin_count); + for (size_t i = 0; i < bin_count; i++){ + size_t height = MAX_ASTERISK * (static_cast(bins[i]) / max_count); + hights[i] = height; + } + + for (size_t i = 0; i < bin_count; i++){ + printf("%3d|", bins[i]); + for (size_t j = 0; j < hights[i]; j++){ + std::cout<<"*"; + } + std::cout << std::endl; + } + } + else{ + for (size_t i = 0; i < bin_count; i++) + { + printf("%3d|", bins[i]); + for (size_t j = 0; j < bins[i]; j++){ + std::cout<<"*"; + } + std::cout << std::endl; + } + } + +} diff --git a/text.h b/text.h new file mode 100644 index 0000000..2a4e40e --- /dev/null +++ b/text.h @@ -0,0 +1,12 @@ +/*#pragma once +#include +#include + +void +show_histogram_text(std::vector& bins, size_t & max_count,size_t & bin_count);*/ +#pragma once +#include +#include + +void +show_histogram_text(std::vector& bins, size_t & max_count,size_t & bin_count);