IvanonVG 4 недель назад
Родитель 1309008182
Сommit fa2e7974da

@ -1,47 +1,43 @@
#include "histogram.h"
#include <vector>
#include <numeric>
using namespace std;
void find_minmax(const vector<double>& numbers, double& min, double& max) {
if (numbers.empty())
{
min = 0;
max = 0;
return;
}
if (numbers.empty()) return;
max = numbers[0];
min = numbers[0];
for (double x : numbers) {
if (x < min) min = x;
else if (x > max) max = x;
if (x > max) max = x;
}
}
vector<size_t> make_histogram (vector<double> numbers, size_t bin_count)
{
vector<size_t> make_histogram(vector<double> numbers, size_t bin_count) {
double min, max;
find_minmax (numbers, min, max);
find_minmax(numbers, min, max);
double bin_size = (max - min) / bin_count;
vector<size_t> bins(bin_count);
for (size_t i = 0; i < numbers.size(); i++)
{
for (size_t i = 0; i < numbers.size(); i++) {
bool found = false;
for (size_t j = 0; (j < bin_count - 1) && !found; j++)
{
for (size_t j = 0; (j < bin_count - 1) && !found; j++) {
auto lo = min + j * bin_size;
auto hi = min + (j + 1) * bin_size;
if ((lo <= numbers[i]) && (numbers[i] < hi))
{
if ((lo <= numbers[i]) && (numbers[i] < hi)) {
bins[j]++;
found = true;
}
}
if (!found)
{
if (!found) {
bins[bin_count - 1]++;
}
}
return bins;
}
double calculate_average_height(const vector<size_t>& bins) {
if (bins.empty()) return 0.0;
double sum = accumulate(bins.begin(), bins.end(), 0.0);
return sum / bins.size();
}

@ -1,9 +1,8 @@
#ifndef HISTOGRAM_H_INCLUDED
#define HISTOGRAM_H_INCLUDED
#include <vector>
using namespace std;
vector<size_t> make_histogram(vector<double> numbers, size_t bin_count);
double calculate_average_height(const vector<size_t>& bins);
#endif

@ -37,9 +37,10 @@
<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" />
<Unit filename="unittest.cpp" />
<Extensions>
<lib_finder disable_auto="1" />
</Extensions>

@ -2,6 +2,8 @@
#include <vector>
#include "histogram.h"
#include "text.h"
#include "svg.h"
#include "histogram_internal.h"
using namespace std;
@ -28,12 +30,8 @@ Input input_data(){
int main()
{
if (in.numbers.empty()) {
cerr << "Error: The data set is empty." << endl;
return EXIT_FAILURE;
}
auto in = input_data();
auto bins = make_histogram(in.numbers, in.bin_count);
show_histogram_text(bins, in.bin_count);
show_histogram_svg(bins);
return 0;
}

@ -2,7 +2,7 @@
#include <vector>
#include <string>
#include "svg.h"
#include "histogram.h"
using namespace std;
void svg_begin(double width, double height) {
@ -22,7 +22,7 @@ void svg_text(double left, double baseline, string text) {
cout << "<text x='" << left << "' y='" << baseline << "'>" << text << "</text>\n";
}
void svg_rect(double x, double y, double width, double height, string stroke = "black", string fill = "black") {
void svg_rect(double x, double y, double width, double height, string stroke, string fill) {
cout << "<rect x='" << x << "' y='" << y << "' width='" << width << "' height='" << height
<< "' stroke='" << stroke << "' fill='" << fill << "' />\n";
}
@ -51,10 +51,13 @@ void show_histogram_svg(const vector<size_t>& bins) {
}
}
double avg_height = calculate_average_height(bins);
for (size_t bin : bins) {
const double bin_width = MAX_WIDTH * (static_cast<double>(bin) / max_count);
svg_text(TEXT_LEFT, top + TEXT_BASELINE, to_string(bin));
svg_rect(TEXT_WIDTH, top, bin_width, BIN_HEIGHT, "black", "red");
string fill_color = (bin > avg_height) ? "red" : "green";
svg_rect(TEXT_WIDTH, top, bin_width, BIN_HEIGHT, "black", fill_color);
top += BIN_HEIGHT;
}

@ -1,9 +1,8 @@
#ifndef SVG_H_INCLUDED
#define SVG_H_INCLUDED
#include <vector>
#include <cstddef>
void show_histogram_svg(const std::vector<size_t>& bins);
#endif // SVG_H_INCLUDED
#endif

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