code: выполнено задание для варианта №16
Этот коммит содержится в:
18
main.cpp
18
main.cpp
@@ -1,5 +1,6 @@
|
||||
#include <iostream>
|
||||
#include <vector>
|
||||
#include <string>
|
||||
#include "histogram.h"
|
||||
//#include "text.h"
|
||||
#include "svg.h"
|
||||
@@ -8,6 +9,7 @@ using namespace std;
|
||||
|
||||
struct Input {
|
||||
vector<double> numbers;
|
||||
vector<string> colors;
|
||||
size_t bin_count{};
|
||||
};
|
||||
|
||||
@@ -26,8 +28,20 @@ input_data() {
|
||||
cin >> in.numbers[i];
|
||||
}
|
||||
|
||||
cerr << "Enter bins count: ";
|
||||
cerr << "Enter bins count and colors: ";
|
||||
cin >> in.bin_count;
|
||||
vector<string> colors(in.bin_count);
|
||||
in.colors.resize(in.bin_count);
|
||||
|
||||
for (size_t i = 0; i < in.bin_count; i++) {
|
||||
cin >> in.colors[i];
|
||||
bool check = check_color(in.colors[i]);
|
||||
while (!check) {
|
||||
cerr << "Incorrect input. Color doesn't contain # or has spaces." << endl;
|
||||
cin >> in.colors[i];
|
||||
check = check_color(in.colors[i]);
|
||||
}
|
||||
}
|
||||
return in;
|
||||
}
|
||||
|
||||
@@ -36,5 +50,5 @@ int main()
|
||||
Input in = input_data();
|
||||
vector<size_t> bins = make_histogram(in.numbers, in.bin_count);
|
||||
//show_histogram_text(bins, in.bin_count);
|
||||
show_histogram_svg(bins);
|
||||
show_histogram_svg(bins, in.colors);
|
||||
}
|
||||
|
||||
12
svg.cpp
12
svg.cpp
@@ -5,6 +5,14 @@
|
||||
|
||||
using namespace std;
|
||||
|
||||
bool
|
||||
check_color(string color) {
|
||||
if (color[0]=='#'||color.find(' ')==(-1))
|
||||
return true;
|
||||
else
|
||||
return false;
|
||||
}
|
||||
|
||||
void
|
||||
svg_text(double left, double baseline, string text) {
|
||||
cout << "<text x='" << left
|
||||
@@ -40,7 +48,7 @@ svg_end() {
|
||||
}
|
||||
|
||||
void
|
||||
show_histogram_svg(const vector<size_t>& bins) {
|
||||
show_histogram_svg(const vector<size_t>& bins, const vector<string>& colors) {
|
||||
const auto IMAGE_WIDTH = 400;
|
||||
const auto IMAGE_HEIGHT = 300;
|
||||
const auto TEXT_LEFT = 20;
|
||||
@@ -67,7 +75,7 @@ show_histogram_svg(const vector<size_t>& bins) {
|
||||
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);
|
||||
svg_rect(TEXT_WIDTH, top, bin_width, BIN_HEIGHT, "black",colors[top/BIN_HEIGHT]);
|
||||
top += BIN_HEIGHT;
|
||||
}
|
||||
svg_end();
|
||||
|
||||
3
svg.h
3
svg.h
@@ -2,8 +2,9 @@
|
||||
#define SVG_H_INCLUDED
|
||||
|
||||
#include <vector>
|
||||
#include <string>
|
||||
|
||||
void
|
||||
show_histogram_svg(const std::vector<size_t>& bins);
|
||||
show_histogram_svg(const std::vector<size_t>& bins, const std::vector<std::string>& colors);
|
||||
|
||||
#endif // SVG_H_INCLUDED
|
||||
|
||||
9
svg_internal.h
Обычный файл
9
svg_internal.h
Обычный файл
@@ -0,0 +1,9 @@
|
||||
#ifndef SVG_INTERNAL_H_INCLUDED
|
||||
#define SVG_INTERNAL_H_INCLUDED
|
||||
|
||||
#include <string>
|
||||
|
||||
bool
|
||||
check_color(std::string color);
|
||||
|
||||
#endif // SVG_INTERNAL_H_INCLUDED
|
||||
@@ -31,6 +31,9 @@
|
||||
<Compiler>
|
||||
<Add option="-Wall" />
|
||||
</Compiler>
|
||||
<Unit filename="histogram.cpp" />
|
||||
<Unit filename="histogram_internal.h" />
|
||||
<Unit filename="unittest.cpp" />
|
||||
<Extensions />
|
||||
</Project>
|
||||
</CodeBlocks_project_file>
|
||||
|
||||
16
unittest.cpp
16
unittest.cpp
@@ -2,6 +2,8 @@
|
||||
#define DOCTEST_CONFIG_IMPLEMENT_WITH_MAIN
|
||||
#include "doctest.h"
|
||||
#include "histogram_internal.h"
|
||||
#include "svg_internal.h"
|
||||
#include <string>
|
||||
|
||||
TEST_CASE("distinct positive numbers") {
|
||||
double min = 0;
|
||||
@@ -52,3 +54,17 @@ TEST_CASE("empty vector") {
|
||||
CHECK(min == 0);
|
||||
CHECK(max == 0);
|
||||
}
|
||||
|
||||
TEST_CASE("correct color") {
|
||||
bool check = check_color("red");
|
||||
CHECK(check == true);
|
||||
check = check_color("#FF FFF");
|
||||
CHECK(check == true);
|
||||
}
|
||||
|
||||
TEST_CASE("incorrect color") {
|
||||
bool check = check_color("re d");
|
||||
CHECK(check == false);
|
||||
check = check_color("red green blue");
|
||||
CHECK(check == false);
|
||||
}
|
||||
|
||||
Ссылка в новой задаче
Block a user