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)
#define DOCTEST_CREATE_AND_REGISTER_FUNCTION(f, decorators); \
#define DOCTEST_CREATE_AND_REGISTER_FUNCTION(f, decorators) \
static void f(); \
DOCTEST_REGISTER_FUNCTION(DOCTEST_EMPTY, f, decorators) \
static void f()

@ -3,16 +3,17 @@
using std::vector;
// Ïîèñê ìèíèìóìà è ìàêñèìóìà
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];
for (double number : numbers) {
if (number < min) {
min = number;
}
if (number > max) {
max = number;
}
min = numbers[0];
for (double x : numbers) {
if (x < min) min = x;
else if (x > max) max = x;
}
}

@ -2,6 +2,6 @@
#define HISTOGRAM_INTERNAL_H_INCLUDED
#include <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

@ -32,7 +32,15 @@
<Add option="-Wall" />
<Add option="-fexceptions" />
</Compiler>
<Unit filename="doctest.h" />
<Unit filename="histogram.cpp" />
<Unit filename="histogram.h" />
<Unit filename="histogram_internal.h" />
<Unit filename="main.cpp" />
<Unit filename="svg.cpp" />
<Unit filename="svg.h" />
<Unit filename="text.cpp" />
<Unit filename="text.h" />
<Extensions>
<lib_finder disable_auto="1" />
</Extensions>

@ -2,22 +2,22 @@
#include <vector>
#include "histogram.h"
#include "text.h"
#include "svg.h"
using namespace std;
struct Input {
vector<double> numbers;
size_t bin_count{};
size_t bin_count;
size_t image_width;
};
// Ââîä äàííûõ
Input input_data() {
Input in;
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++) {
cin >> in.numbers[i];
@ -26,18 +26,32 @@ Input input_data() {
cerr << "Enter 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;
}
// Âûâîä ãèñòîãðàììû
// Îñíîâíàÿ ôóíêöèÿ
int main() {
Input in = input_data();
vector<size_t> bins = make_histogram(in.numbers, in.bin_count);
show_histogram_text(bins);
auto in = input_data();
auto bins = make_histogram(in.numbers, in.bin_count);
show_histogram_svg(bins, in.image_width);
return 0;
}

@ -27,21 +27,20 @@ void svg_rect(double x, double y, double width, double height, string stroke = "
<< "' 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()) {
cerr << "Error: Empty bins vector\n";
return;
}
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 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;
size_t max_count = bins[0];

@ -2,8 +2,8 @@
#define SVG_H_INCLUDED
#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") {
double min = 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"){
double min = 0;
double max = 0;
find_minmax({3}, min, max);
CHECK(min == max);
find_minmax({7}, min, max);
CHECK(min == 7);
CHECK(max == 7);
}

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