Сравнить коммиты
7 Коммитов
1bff5bee6c
...
325d6f8fc4
Автор | SHA1 | Дата |
---|---|---|
|
325d6f8fc4 | 1 неделю назад |
|
e459274d5b | 1 неделю назад |
|
9b0338ed6b | 1 неделю назад |
|
0db02a9e37 | 1 неделю назад |
|
b8dd4fcb90 | 1 неделю назад |
|
a18be23a81 | 1 неделю назад |
|
f874a1a962 | 1 неделю назад |
Разница между файлами не показана из-за своего большого размера
Загрузить разницу
@ -0,0 +1,79 @@
|
||||
#include <iostream>
|
||||
#include "histogram.h"
|
||||
#include "histogram_internal.h"
|
||||
#include <algorithm>
|
||||
#include <cmath>
|
||||
|
||||
|
||||
void find_minmax(const std::vector<double>& numbers, double& min, double& max) {
|
||||
if (numbers.empty()) return;
|
||||
|
||||
min = numbers[0];
|
||||
max = numbers[0];
|
||||
for (double num : numbers) {
|
||||
if (num < min) min = num;
|
||||
if (num > max) max = num;
|
||||
}
|
||||
}
|
||||
|
||||
std::vector<size_t> make_histogram(const std::vector<double>& numbers, size_t bin_count) {
|
||||
if (numbers.empty() || bin_count == 0) return {};
|
||||
|
||||
double min, max;
|
||||
find_minmax(numbers, min, max);
|
||||
|
||||
std::vector<size_t> bins(bin_count);
|
||||
double bin_size = (max - min) / bin_count;
|
||||
|
||||
for (double num : numbers) {
|
||||
bool found = false;
|
||||
for (size_t j = 0; j < bin_count - 1 && !found; ++j) {
|
||||
double lo = min + j * bin_size;
|
||||
double hi = min + (j + 1) * bin_size;
|
||||
if (lo <= num && num < hi) {
|
||||
bins[j]++;
|
||||
found = true;
|
||||
}
|
||||
}
|
||||
if (!found) {
|
||||
bins[bin_count - 1]++;
|
||||
}
|
||||
}
|
||||
|
||||
return bins;
|
||||
}
|
||||
|
||||
Input input_data() {
|
||||
Input in;
|
||||
std::cin >> in.number_count;
|
||||
|
||||
in.numbers.resize(in.number_count);
|
||||
for (size_t i = 0; i < in.number_count; i++) {
|
||||
std::cin >> in.numbers[i];
|
||||
}
|
||||
|
||||
std::cin >> in.bin_count;
|
||||
return in;
|
||||
}
|
||||
|
||||
void show_histogram_text(const std::vector<size_t>& bins, size_t max_width) {
|
||||
if (bins.empty()) return;
|
||||
|
||||
size_t max_count = *std::max_element(bins.begin(), bins.end());
|
||||
if (max_count == 0) return;
|
||||
|
||||
for (size_t count : bins) {
|
||||
|
||||
if (count < 100) std::cout << " ";
|
||||
if (count < 10) std::cout << " ";
|
||||
|
||||
std::cout << count << "|";
|
||||
|
||||
|
||||
size_t bar_length = (count * max_width) / max_count;
|
||||
for (size_t i = 0; i < bar_length; ++i) {
|
||||
std::cout << "*";
|
||||
}
|
||||
std::cout << std::endl;
|
||||
}
|
||||
}
|
@ -0,0 +1,17 @@
|
||||
#ifndef HISTOGRAM_H
|
||||
#define HISTOGRAM_H
|
||||
|
||||
#include <vector>
|
||||
|
||||
struct Input {
|
||||
std::vector<double> numbers;
|
||||
size_t bin_count{};
|
||||
size_t number_count;
|
||||
};
|
||||
|
||||
|
||||
Input input_data();
|
||||
std::vector<size_t> make_histogram(const std::vector<double>& numbers, size_t bin_count);
|
||||
void show_histogram_text(const std::vector<size_t>& bins, size_t max_width = 80);
|
||||
|
||||
#endif
|
@ -0,0 +1,8 @@
|
||||
#ifndef HISTOGRAM_INTERNAL_H
|
||||
#define HISTOGRAM_INTERNAL_H
|
||||
|
||||
#include <vector>
|
||||
|
||||
void find_minmax(const std::vector<double>& numbers, double& min, double& max);
|
||||
|
||||
#endif
|
@ -0,0 +1,56 @@
|
||||
#include "svg.h"
|
||||
#include <algorithm>
|
||||
#include <string>
|
||||
|
||||
using namespace std;
|
||||
|
||||
namespace svg {
|
||||
void begin(ostream& out, double width, double height) {
|
||||
out << "<?xml version='1.0' encoding='UTF-8'?>\n";
|
||||
out << "<svg width='" << width << "' height='" << height << "' "
|
||||
<< "viewBox='0 0 " << width << " " << height << "' "
|
||||
<< "xmlns='http://www.w3.org/2000/svg'>\n";
|
||||
}
|
||||
|
||||
void end(ostream& out) {
|
||||
out << "</svg>\n";
|
||||
}
|
||||
|
||||
void text(ostream& out, double left, double baseline, const string& text) {
|
||||
out << "<text x='" << left << "' y='" << baseline << "'>" << text << "</text>\n";
|
||||
}
|
||||
|
||||
void rect(ostream& out, double x, double y, double width, double height,
|
||||
const string& stroke, const string& fill) {
|
||||
out << "<rect x='" << x << "' y='" << y << "' width='" << width
|
||||
<< "' height='" << height << "' stroke='" << stroke
|
||||
<< "' fill='" << fill << "' />\n";
|
||||
}
|
||||
|
||||
void show_histogram_svg(ostream& out, const vector<size_t>& bins) {
|
||||
begin(out, IMAGE_WIDTH, IMAGE_HEIGHT);
|
||||
|
||||
if (bins.empty()) {
|
||||
end(out);
|
||||
return;
|
||||
}
|
||||
|
||||
|
||||
size_t max_count = *max_element(bins.begin(), bins.end());
|
||||
double scale = (max_count > 0) ? (IMAGE_WIDTH - TEXT_WIDTH) / static_cast<double>(max_count) : 1.0;
|
||||
|
||||
double top = 0;
|
||||
for (size_t bin : bins) {
|
||||
const double bin_width = bin * scale;
|
||||
text(out, TEXT_LEFT, top + TEXT_BASELINE, to_string(bin));
|
||||
|
||||
|
||||
string fill_color = (static_cast<int>(top / BIN_HEIGHT) % 2 == 0) ? "#aaffaa" : "#aaaaff";
|
||||
rect(out, TEXT_WIDTH, top, bin_width, BIN_HEIGHT, "black", fill_color);
|
||||
|
||||
top += BIN_HEIGHT;
|
||||
}
|
||||
|
||||
end(out);
|
||||
}
|
||||
}
|
@ -0,0 +1,27 @@
|
||||
#ifndef SVG_H
|
||||
#define SVG_H
|
||||
|
||||
#include <vector>
|
||||
#include <string>
|
||||
#include <iostream>
|
||||
|
||||
|
||||
const size_t IMAGE_WIDTH = 400;
|
||||
const size_t IMAGE_HEIGHT = 300;
|
||||
const size_t TEXT_LEFT = 20;
|
||||
const size_t TEXT_BASELINE = 20;
|
||||
const size_t TEXT_WIDTH = 50;
|
||||
const size_t BIN_HEIGHT = 30;
|
||||
const size_t BLOCK_WIDTH = 10;
|
||||
|
||||
namespace svg {
|
||||
void begin(std::ostream& out, double width, double height);
|
||||
void end(std::ostream& out);
|
||||
void text(std::ostream& out, double left, double baseline, const std::string& text);
|
||||
void rect(std::ostream& out, double x, double y, double width, double height,
|
||||
const std::string& stroke = "black", const std::string& fill = "#dddddd");
|
||||
|
||||
void show_histogram_svg(std::ostream& out, const std::vector<size_t>& bins);
|
||||
}
|
||||
|
||||
#endif
|
@ -0,0 +1,38 @@
|
||||
<?xml version="1.0" encoding="UTF-8" standalone="yes" ?>
|
||||
<CodeBlocks_project_file>
|
||||
<FileVersion major="1" minor="6" />
|
||||
<Project>
|
||||
<Option title="unittest" />
|
||||
<Option pch_mode="2" />
|
||||
<Option compiler="gcc" />
|
||||
<Build>
|
||||
<Target title="Debug">
|
||||
<Option output="bin/Debug/unittest" prefix_auto="1" extension_auto="1" />
|
||||
<Option object_output="obj/Debug/" />
|
||||
<Option type="1" />
|
||||
<Option compiler="gcc" />
|
||||
<Compiler>
|
||||
<Add option="-g" />
|
||||
</Compiler>
|
||||
</Target>
|
||||
<Target title="Release">
|
||||
<Option output="bin/Release/unittest" prefix_auto="1" extension_auto="1" />
|
||||
<Option object_output="obj/Release/" />
|
||||
<Option type="1" />
|
||||
<Option compiler="gcc" />
|
||||
<Compiler>
|
||||
<Add option="-O2" />
|
||||
</Compiler>
|
||||
<Linker>
|
||||
<Add option="-s" />
|
||||
</Linker>
|
||||
</Target>
|
||||
</Build>
|
||||
<Compiler>
|
||||
<Add option="-Wall" />
|
||||
</Compiler>
|
||||
<Extensions>
|
||||
<lib_finder disable_auto="1" />
|
||||
</Extensions>
|
||||
</Project>
|
||||
</CodeBlocks_project_file>
|
Загрузка…
Ссылка в новой задаче