diff --git a/main.cpp b/main.cpp index af98169..cbfe033 100644 --- a/main.cpp +++ b/main.cpp @@ -6,20 +6,22 @@ using namespace std; -struct Input { +struct Input +{ vector numbers; size_t bin_count{}; }; Input -input_data() { +input_data() +{ size_t number_count; cerr << "Enter number count: "; cin >> number_count; Input in; in.numbers.resize(number_count); - cerr << "Enter numbers: "; - for (size_t i = 0; i < number_count; i++) { + for (size_t i = 0; i < number_count; i++) + { cin >> in.numbers[i]; } cerr << "Enter bin count: "; @@ -27,9 +29,9 @@ input_data() { return in; } -int -main() { - auto in = input_data(); - auto bins = make_histogram(in.numbers, in.bin_count); +int main() +{ + Input in = input_data(); + vector bins = make_histogram(in.numbers, in.bin_count); show_histogram_svg(bins); } diff --git a/svg.cpp b/svg.cpp index df13906..429158b 100644 --- a/svg.cpp +++ b/svg.cpp @@ -4,18 +4,15 @@ #include "svg.h" using namespace std; -void -svg_text(double left, double baseline, string text) { +void svg_text(double left, double baseline, string text) { cout << "" << text << ""; } -void -svg_rect(double x, double y, double width, double height) { +void svg_rect(double x, double y, double width, double height) { cout << "\n"; } -void -svg_begin(double width, double height) { +void svg_begin(double width, double height) { cout << "\n"; cout << "\n"; } -void -svg_end() { +void svg_end() { cout << "\n"; } -void -show_histogram_svg(const vector& bins) { +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; svg_begin(400, 300); double top = 0; + size_t max_bin = 0; + for (size_t bin : bins) { + if (bin > max_bin) { + max_bin = bin; + } + } for (size_t bin : bins) { const double bin_width = BLOCK_WIDTH * bin; - svg_text(TEXT_LEFT, top + TEXT_BASELINE, to_string(bin)); - svg_rect(TEXT_WIDTH, top, bin_width, BIN_HEIGHT); + const double text_left = BLOCK_WIDTH * max_bin; + const double rect_left = text_left - bin_width; + svg_rect(rect_left, top, bin_width, BIN_HEIGHT); + svg_text(text_left, top + TEXT_BASELINE, to_string(bin)); top += BIN_HEIGHT; } svg_end(); } - diff --git a/text.cpp b/text.cpp index 76b4785..dab9409 100644 --- a/text.cpp +++ b/text.cpp @@ -3,51 +3,56 @@ using namespace std; -void -show_histogram_text(const vector& bins){ +void show_histogram_text(const vector& bins) { const size_t SCREEN_WIDTH = 80; const size_t MAX_ASTERISK = SCREEN_WIDTH - 3 - 1; auto bin_count = bins.size(); size_t max_count = 0; - for (size_t i = 0; i < bin_count; i++){ + for (size_t i = 0; i < bin_count; i++) { size_t Count = bins[i]; - if (max_count < Count){ + if (max_count < Count) { max_count = Count; } } - if (max_count <= MAX_ASTERISK){ - for (size_t i = 0; i < bin_count; i++){ + if (max_count <= MAX_ASTERISK) { + for (size_t i = 0; i < bin_count; i++) { size_t Count = bins[i]; - if (Count < 100){ + for (size_t j = 0; j < max_count - Count; j++){ cout << " "; } - if (Count < 10){ - cout << " "; - } - cout << Count << "|"; for (size_t j = 0; j < Count; j++){ cout << "*"; } - cout << "\n"; + cout << "|"; + if (Count < 100) { + cout << " "; + } + if (Count < 10) { + cout << " "; + } + cout << Count << "\n"; } } - else{ - for (size_t i = 0; i < bin_count; i++){ + else { + for (size_t i = 0; i < bin_count; i++) { size_t Count = bins[i]; size_t height = 76 * (static_cast(Count) / max_count); - if (Count < 100){ + for (size_t j = 0; j < max_count - height; j++){ cout << " "; } - if (Count < 10){ - cout << " "; - } - cout << Count << "|"; for (size_t j = 0; j < height; j++){ cout << "*"; } - cout << "\n"; + cout << "|"; + if (Count < 100) { + cout << " "; + } + if (Count < 10) { + cout << " "; + } + cout << Count << "\n"; } } } diff --git a/unittest.cpp b/unittest.cpp index 1ee1609..0708330 100644 --- a/unittest.cpp +++ b/unittest.cpp @@ -35,3 +35,10 @@ TEST_CASE("one number") { CHECK(min == 2); CHECK(max == 2); } + + + + + + +