SolovyovaED 12 месяцев назад
Родитель fde5de5ddc
Сommit 07f4ed22db

@ -7,8 +7,9 @@ using namespace std;
void void
find_minmax(const vector<double>& numbers, double& min, double& max){ find_minmax(const vector<double>& numbers, double& min, double& max){
if (numbers.size() >=1){
min = numbers[0]; min = numbers[0];
min = numbers[0]; max = numbers[0];
for(size_t i=0; i < numbers.size(); i++){ for(size_t i=0; i < numbers.size(); i++){
if (numbers[i] < min) if (numbers[i] < min)
{ {
@ -18,7 +19,7 @@ find_minmax(const vector<double>& numbers, double& min, double& max){
{ {
max = numbers[i]; max = numbers[i];
} }
} }}
} }

@ -6,3 +6,4 @@ void
find_minmax(const std::vector<double>& numbers, double& min, double& max); find_minmax(const std::vector<double>& numbers, double& min, double& max);
#endif // HISTOGRAM_INTERNAL_H_INCLUDED #endif // HISTOGRAM_INTERNAL_H_INCLUDED

@ -32,7 +32,15 @@
<Add option="-Wall" /> <Add option="-Wall" />
<Add option="-fexceptions" /> <Add option="-fexceptions" />
</Compiler> </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="main.cpp" />
<Unit filename="svg.cpp" />
<Unit filename="svg.h" />
<Unit filename="text.cpp" />
<Unit filename="text.h" />
<Extensions /> <Extensions />
</Project> </Project>
</CodeBlocks_project_file> </CodeBlocks_project_file>

@ -11,8 +11,8 @@ using namespace std;
struct Input { struct Input {
vector<double> numbers; vector<double> numbers;
size_t bin_count{}; size_t bin_count{};
string stroke; vector<string> stroke;
string fill; vector<string> fill;
}; };
Input Input
@ -34,20 +34,25 @@ input_data(){
cerr << "Enter bin count: "; cerr << "Enter bin count: ";
cin >> in.bin_count; cin >> in.bin_count;
cerr << "Enter color: "; in.stroke.resize(in.bin_count);
cin >> in.stroke; in.fill.resize(in.bin_count);
for (size_t i=0;i<in.bin_count;i++){
cerr << "Enter color to fill: "; cerr <<"Enter stroke in format RGB:";
cin >> in.fill; cin >>in.stroke[i];
cerr <<"Enter fill in format RGB:";
cin >>in.fill[i];
}
return in; return in;
} }
int main() int main()
{ {
auto in = input_data(); auto in = input_data();
std::vector<size_t> bins = make_histogram(in.numbers, in.bin_count); 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; return 0;
} }

@ -29,28 +29,9 @@ void
svg_rect(double x, double y, double width, double height, string stroke = "chartreuse", string fill = "plum"){ 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 << "'/>"; cout << "<rect x='" << x << "' y='" << y << "' width='" << width << "' height='" << height << "' stroke='" << stroke << "' fill='" << fill << "'/>";
} }
void void
input_colors(string &colors_stroke, string &colors_fill){ show_histogram_svg(const vector<size_t>& bins, const vector<string>& stroke,const vector<string>& 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) {
const auto IMAGE_WIDTH = 400; const auto IMAGE_WIDTH = 400;
const auto IMAGE_HEIGHT = 300; const auto IMAGE_HEIGHT = 300;
const auto TEXT_LEFT = 20; const auto TEXT_LEFT = 20;
@ -67,10 +48,10 @@ show_histogram_svg(const vector<size_t>& bins, string stroke, string fill) {
maxel = bin; maxel = bin;
} }
} }
for (size_t bin : bins) { for (size_t i=0; i<bins.size(); i++) {
const double bin_width = (( IMAGE_WIDTH - TEXT_WIDTH ) / BLOCK_WIDTH ) * ( bin / maxel ); const double bin_width = (( IMAGE_WIDTH - TEXT_WIDTH ) / BLOCK_WIDTH ) * ( bins[i] / maxel );
svg_text(TEXT_LEFT, top + TEXT_BASELINE, to_string(bin)); svg_text(TEXT_LEFT, top + TEXT_BASELINE, to_string(bins[i]));
svg_rect(TEXT_WIDTH, top, bin_width, BIN_HEIGHT, stroke, fill); svg_rect(TEXT_WIDTH, top, bin_width, BIN_HEIGHT, stroke[i], fill[i]);
top += BIN_HEIGHT; top += BIN_HEIGHT;
} }
svg_end(); svg_end();

@ -3,5 +3,5 @@
#include <vector> #include <vector>
void 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);

@ -31,6 +31,12 @@
<Compiler> <Compiler>
<Add option="-Wall" /> <Add option="-Wall" />
</Compiler> </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> <Extensions>
<lib_finder disable_auto="1" /> <lib_finder disable_auto="1" />
</Extensions> </Extensions>

@ -2,23 +2,6 @@
#define DOCTEST_CONFIG_IMPLEMENT_WITH_MAIN #define DOCTEST_CONFIG_IMPLEMENT_WITH_MAIN
#include "doctest.h" #include "doctest.h"
#include "histogram_internal.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") { TEST_CASE("distinct positive numbers") {
double min = 0; double min = 0;
@ -28,18 +11,32 @@ TEST_CASE("distinct positive numbers") {
CHECK(max == 2); CHECK(max == 2);
} }
TEST_CASE("empty vector"){ TEST_CASE("same elements"){
double min = 0; double min = 0;
double max = 0; double max = 0;
find_minmax({ 0, 0 }, min, max); find_minmax({ 1, 1 }, min, max);
CHECK(min == 0); CHECK(min == max);
CHECK(max == 0);
} }
TEST_CASE("same elements"){ TEST_CASE("only one number"){
double min = 0; double min = 0;
double max = 0; double max = 0;
find_minmax({ 1, 1 }, min, max); find_minmax({1}, min, max);
CHECK(min == max); CHECK(min == 1);
CHECK(max == min); 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);
} }

Загрузка…
Отмена
Сохранить