From 07f4ed22db57a8fa2ecbc2f719669e86416fa4f5 Mon Sep 17 00:00:00 2001 From: SolovyovaED <SolovyovaYD@mpei.com> Date: Thu, 9 May 2024 22:10:04 +0300 Subject: [PATCH] =?UTF-8?q?build:6=20=D0=B2=D0=B0=D1=80=D0=B8=D0=BD=D0=B0?= =?UTF-8?q?=D1=82?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- histogram.cpp | 5 +++-- histogram_internal.h | 1 + laba3.cbp | 8 ++++++++ main.cpp | 21 ++++++++++++-------- svg.cpp | 31 ++++++----------------------- svg.h | 2 +- unittest.cbp | 6 ++++++ unittest.cpp | 47 +++++++++++++++++++++----------------------- 8 files changed, 60 insertions(+), 61 deletions(-) diff --git a/histogram.cpp b/histogram.cpp index 50cb037..820c885 100644 --- a/histogram.cpp +++ b/histogram.cpp @@ -7,8 +7,9 @@ using namespace std; void find_minmax(const vector<double>& numbers, double& min, double& max){ + if (numbers.size() >=1){ min = numbers[0]; - min = numbers[0]; + max = numbers[0]; for(size_t i=0; i < numbers.size(); i++){ if (numbers[i] < min) { @@ -18,7 +19,7 @@ find_minmax(const vector<double>& numbers, double& min, double& max){ { max = numbers[i]; } - } + }} } diff --git a/histogram_internal.h b/histogram_internal.h index 5c31312..654569d 100644 --- a/histogram_internal.h +++ b/histogram_internal.h @@ -6,3 +6,4 @@ void find_minmax(const std::vector<double>& numbers, double& min, double& max); #endif // HISTOGRAM_INTERNAL_H_INCLUDED + diff --git a/laba3.cbp b/laba3.cbp index 2a15d57..c1218af 100644 --- a/laba3.cbp +++ b/laba3.cbp @@ -32,7 +32,15 @@ <Add option="-Wall" /> <Add option="-fexceptions" /> </Compiler> + <Unit filename="histogram.cpp" /> + <Unit filename="histogram.h" /> + <Unit filename="histogram_internal.h" /> + <Unit filename="input_colors_internal.h" /> <Unit filename="main.cpp" /> + <Unit filename="svg.cpp" /> + <Unit filename="svg.h" /> + <Unit filename="text.cpp" /> + <Unit filename="text.h" /> <Extensions /> </Project> </CodeBlocks_project_file> diff --git a/main.cpp b/main.cpp index 3246b34..e63a609 100644 --- a/main.cpp +++ b/main.cpp @@ -11,8 +11,8 @@ using namespace std; struct Input { vector<double> numbers; size_t bin_count{}; - string stroke; - string fill; + vector<string> stroke; + vector<string> fill; }; Input @@ -34,20 +34,25 @@ input_data(){ cerr << "Enter bin count: "; cin >> in.bin_count; - cerr << "Enter color: "; - cin >> in.stroke; - - cerr << "Enter color to fill: "; - cin >> in.fill; + in.stroke.resize(in.bin_count); + in.fill.resize(in.bin_count); + for (size_t i=0;i<in.bin_count;i++){ + cerr <<"Enter stroke in format RGB:"; + cin >>in.stroke[i]; + cerr <<"Enter fill in format RGB:"; + cin >>in.fill[i]; + } return in; } + int main() { auto in = input_data(); std::vector<size_t> bins = make_histogram(in.numbers, in.bin_count); - show_histogram_svg(bins, in.stroke, in.fill); + show_histogram_svg(bins,in.stroke,in.fill); return 0; } + diff --git a/svg.cpp b/svg.cpp index 88bf76a..05a46af 100644 --- a/svg.cpp +++ b/svg.cpp @@ -29,28 +29,9 @@ void svg_rect(double x, double y, double width, double height, string stroke = "chartreuse", string fill = "plum"){ cout << "<rect x='" << x << "' y='" << y << "' width='" << width << "' height='" << height << "' stroke='" << stroke << "' fill='" << fill << "'/>"; } + void -input_colors(string &colors_stroke, string &colors_fill){ - string color_personal; - cout << "Do you want to change colors? Yes/No" << endl; - cin >> color_personal; - while ((color_personal != "No") && (color_personal != "Yes")){ - cout << "WRONG ANSWER! Try again!" << endl; - cin >> color_personal; - } - if (color_personal == "No"){ - return; - } - else { - cout << "What color for a stroke do you want?" << endl; - cin >> colors_stroke; - cout << "What color to fill do you want?" << endl; - cin >> colors_fill; - } - return; -} -void -show_histogram_svg(const vector<size_t>& bins, string stroke, string fill) { +show_histogram_svg(const vector<size_t>& bins, const vector<string>& stroke,const vector<string>& fill) { const auto IMAGE_WIDTH = 400; const auto IMAGE_HEIGHT = 300; const auto TEXT_LEFT = 20; @@ -67,10 +48,10 @@ show_histogram_svg(const vector<size_t>& bins, string stroke, string fill) { maxel = bin; } } - for (size_t bin : bins) { - const double bin_width = (( IMAGE_WIDTH - TEXT_WIDTH ) / BLOCK_WIDTH ) * ( bin / maxel ); - svg_text(TEXT_LEFT, top + TEXT_BASELINE, to_string(bin)); - svg_rect(TEXT_WIDTH, top, bin_width, BIN_HEIGHT, stroke, fill); + for (size_t i=0; i<bins.size(); i++) { + const double bin_width = (( IMAGE_WIDTH - TEXT_WIDTH ) / BLOCK_WIDTH ) * ( bins[i] / maxel ); + svg_text(TEXT_LEFT, top + TEXT_BASELINE, to_string(bins[i])); + svg_rect(TEXT_WIDTH, top, bin_width, BIN_HEIGHT, stroke[i], fill[i]); top += BIN_HEIGHT; } svg_end(); diff --git a/svg.h b/svg.h index 4859b3e..af3caa7 100644 --- a/svg.h +++ b/svg.h @@ -3,5 +3,5 @@ #include <vector> void -show_histogram_svg(const std::vector<size_t>& bins, std::string stroke, std::string fill); +show_histogram_svg(const std::vector<size_t>& bins, const std::vector<std::string>& stroke, const std::vector<std::string>& fill); diff --git a/unittest.cbp b/unittest.cbp index 7f9621b..cc815ba 100644 --- a/unittest.cbp +++ b/unittest.cbp @@ -31,6 +31,12 @@ <Compiler> <Add option="-Wall" /> </Compiler> + <Unit filename="doctest.h" /> + <Unit filename="histogram.cpp" /> + <Unit filename="histogram_internal.h" /> + <Unit filename="input_colors_internal.h" /> + <Unit filename="svg.cpp" /> + <Unit filename="unittest.cpp" /> <Extensions> <lib_finder disable_auto="1" /> </Extensions> diff --git a/unittest.cpp b/unittest.cpp index f4d7164..0025075 100644 --- a/unittest.cpp +++ b/unittest.cpp @@ -2,23 +2,6 @@ #define DOCTEST_CONFIG_IMPLEMENT_WITH_MAIN #include "doctest.h" #include "histogram_internal.h" -#include "input_colors_internal.h" - -TEST_CASE("Mistakes in spelling") { - std::string colors_stroke = "black"; - std::string colors_fill = "green"; - input_colors(colors_stroke, colors_fill); - CHECK(colors_stroke == "yellow"); - CHECK(colors_fill == "pink"); -} - -TEST_CASE("The answer is No") { - std::string colors_stroke = "black"; - std::string colors_fill = "green"; - input_colors(colors_stroke, colors_fill); - CHECK(colors_stroke == "black"); - CHECK(colors_fill == "green"); -} TEST_CASE("distinct positive numbers") { double min = 0; @@ -28,18 +11,32 @@ TEST_CASE("distinct positive numbers") { CHECK(max == 2); } -TEST_CASE("empty vector"){ +TEST_CASE("same elements"){ double min = 0; double max = 0; - find_minmax({ 0, 0 }, min, max); - CHECK(min == 0); - CHECK(max == 0); + find_minmax({ 1, 1 }, min, max); + CHECK(min == max); } -TEST_CASE("same elements"){ +TEST_CASE("only one number"){ double min = 0; double max = 0; - find_minmax({ 1, 1 }, min, max); - CHECK(min == max); - CHECK(max == min); + find_minmax({1}, min, max); + CHECK(min == 1); + CHECK(max == 1); +} + +TEST_CASE("distinct negative numbers") { + double min = 0; + double max = 0; + find_minmax({-1, -2}, min, max); + CHECK(min == -2); + CHECK(max == -1); +} +TEST_CASE("empty vector") { + double min = 0; + double max = 0; + find_minmax({}, min, max); + CHECK(min == 0); + CHECK(max == 0); }