Arina (SokolAA) 3 недель назад
Родитель accea25b0b
Сommit e713fc3cc0

@ -2179,7 +2179,7 @@ int registerReporter(const char* name, int priority, bool isReporter) {
} \ } \
DOCTEST_INLINE_NOINLINE void der::f() // NOLINT(misc-definitions-in-headers) DOCTEST_INLINE_NOINLINE void der::f() // NOLINT(misc-definitions-in-headers)
#define DOCTEST_CREATE_AND_REGISTER_FUNCTION(f, decorators); \ #define DOCTEST_CREATE_AND_REGISTER_FUNCTION(f, decorators) \
static void f(); \ static void f(); \
DOCTEST_REGISTER_FUNCTION(DOCTEST_EMPTY, f, decorators) \ DOCTEST_REGISTER_FUNCTION(DOCTEST_EMPTY, f, decorators) \
static void f() static void f()

@ -3,16 +3,17 @@
using std::vector; using std::vector;
// Ïîèñê ìèíèìóìà è ìàêñèìóìà // Ïîèñê ìèíèìóìà è ìàêñèìóìà
void find_minmax(const std::vector<double>& numbers, double& min, double& max) { void find_minmax(const std::vector<double>& numbers, double& min, double& max) {
min = numbers[0]; if (numbers.empty())
{
min = 0;
max = 0;
return;
}
max = numbers[0]; max = numbers[0];
min = numbers[0];
for (double number : numbers) { for (double x : numbers) {
if (number < min) { if (x < min) min = x;
min = number; else if (x > max) max = x;
}
if (number > max) {
max = number;
}
} }
} }

@ -2,6 +2,6 @@
#define HISTOGRAM_INTERNAL_H_INCLUDED #define HISTOGRAM_INTERNAL_H_INCLUDED
#include <vector> #include <vector>
using std::vector; using std::vector;
void find_minmax(const std::vector<double>& numbers, double& min, double& max) void 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="doctest.h" />
<Unit filename="histogram.cpp" />
<Unit filename="histogram.h" />
<Unit filename="histogram_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>
<lib_finder disable_auto="1" /> <lib_finder disable_auto="1" />
</Extensions> </Extensions>

@ -2,22 +2,22 @@
#include <vector> #include <vector>
#include "histogram.h" #include "histogram.h"
#include "text.h" #include "text.h"
#include "svg.h"
using namespace std; using namespace std;
struct Input { struct Input {
vector<double> numbers; vector<double> numbers;
size_t bin_count{}; size_t bin_count;
size_t image_width;
}; };
// Ââîä äàííûõ
Input input_data() { Input input_data() {
Input in;
size_t number_count; size_t number_count;
cerr << "Enter number count: "; cerr << "Enter number count: ";
cin >> number_count; cin >> number_count;
Input in;
in.numbers.resize(number_count); in.numbers.resize(number_count);
cerr << "Enter numbers: "; 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]; cin >> in.numbers[i];
@ -26,18 +26,32 @@ Input input_data() {
cerr << "Enter bin count: "; cerr << "Enter bin count: ";
cin >> in.bin_count; cin >> in.bin_count;
const size_t BLOCK_WIDTH = 10;
const size_t MIN_WIDTH = 70;
const size_t MAX_WIDTH = 800;
size_t required_min_width = number_count * BLOCK_WIDTH / 3;
while (true) {
cerr << "Enter image width (" << MIN_WIDTH << "-" << MAX_WIDTH << "), minimum " << required_min_width << ": ";
cin >> in.image_width;
if (in.image_width < MIN_WIDTH) {
cerr << "Width is too small (minimum " << MIN_WIDTH << ")\n";
} else if (in.image_width > MAX_WIDTH) {
cerr << "Width is too large (maximum " << MAX_WIDTH << ")\n";
} else if (in.image_width < required_min_width) {
cerr << "Width is less than 1/3 of numbers count * block width (" << required_min_width << ")\n";
} else {
break;
}
}
return in; return in;
} }
// Âûâîä ãèñòîãðàììû
// Îñíîâíàÿ ôóíêöèÿ
int main() { int main() {
Input in = input_data(); auto in = input_data();
vector<size_t> bins = make_histogram(in.numbers, in.bin_count); auto bins = make_histogram(in.numbers, in.bin_count);
show_histogram_text(bins); show_histogram_svg(bins, in.image_width);
return 0; return 0;
} }

@ -27,21 +27,20 @@ void svg_rect(double x, double y, double width, double height, string stroke = "
<< "' stroke='" << stroke << "' fill='" << fill << "' />\n"; << "' stroke='" << stroke << "' fill='" << fill << "' />\n";
} }
void show_histogram_svg(const vector<size_t>& bins) { void show_histogram_svg(const vector<size_t>& bins, size_t image_width) {
if (bins.empty()) { if (bins.empty()) {
cerr << "Error: Empty bins vector\n"; cerr << "Error: Empty bins vector\n";
return; return;
} }
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;
const auto TEXT_BASELINE = 20; const auto TEXT_BASELINE = 20;
const auto TEXT_WIDTH = 50; const auto TEXT_WIDTH = 50;
const auto BIN_HEIGHT = 30; const auto BIN_HEIGHT = 30;
const auto MAX_WIDTH = IMAGE_WIDTH - TEXT_WIDTH; const auto MAX_WIDTH = image_width - TEXT_WIDTH;
svg_begin(IMAGE_WIDTH, IMAGE_HEIGHT); svg_begin(image_width, IMAGE_HEIGHT);
double top = 0; double top = 0;
size_t max_count = bins[0]; size_t max_count = bins[0];

@ -2,8 +2,8 @@
#define SVG_H_INCLUDED #define SVG_H_INCLUDED
#include <vector> #include <vector>
#include <cstddef> #include <cstddef> // Äëÿ size_t
void show_histogram_svg(const std::vector<size_t>& bins); void show_histogram_svg(const std::vector<size_t>& bins, size_t image_width);
#endif // SVG_H_INCLUDED #endif

@ -27,12 +27,15 @@ TEST_CASE("vector of the same elements"){
TEST_CASE("empty vector") { TEST_CASE("empty vector") {
double min = 0; double min = 0;
double max = 0; double max = 0;
CHECK_THROWS(find_minmax({}, min, max)); find_minmax({}, min, max);
CHECK(min == 0);
CHECK(max == 0);
} }
TEST_CASE("vector of one elements"){ TEST_CASE("vector of one elements"){
double min = 0; double min = 0;
double max = 0; double max = 0;
find_minmax({3}, min, max); find_minmax({7}, min, max);
CHECK(min == max); CHECK(min == 7);
CHECK(max == 7);
} }

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