Сравнить коммиты

..

4 Коммитов

@ -40,3 +40,21 @@ std::vector<size_t> make_histogram(const std::vector<double>& numbers, size_t bi
return bins;
};
void show_histogram_svg(const std::vector<size_t>& bins) {
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 BLOCK_WIDTH = 10;
svg_begin(IMAGE_WIDTH, IMAGE_HEIGHT);
for (size_t i = 0; i < bins.size(); ++i) {
const double bin_width = BLOCK_WIDTH * bins[i];
const double top = i * BIN_HEIGHT;
svg_text(TEXT_LEFT, top + TEXT_BASELINE, std::to_string(bins[i]));
svg_rect(TEXT_WIDTH, top, bin_width, BIN_HEIGHT, "black", "#aaffaa"); // Ïðèìåð öâåòà
}
svg_end();
}

@ -2,3 +2,4 @@
#include <vector>
size_t find_minmax(const std::vector<double>& numbers, double& min, double& max);
std::vector<size_t> make_histogram(const std::vector<double>& numbers, size_t bin_count);
void show_histogram_svg(const std::vector<size_t>& bins);

@ -1,6 +1,7 @@
#include <iostream>
#include <vector>
#include "histogram.h"
#include "text.h"
struct Input{
std::vector<double> numbers;
size_t bin_count{};
@ -24,86 +25,11 @@ Input input_data() {
return in;
};
using namespace std;
int main()
{
const size_t SCREEN_WIDTH = 80;
const size_t MAX_ASTERISK = SCREEN_WIDTH - 3 - 1;
int number_count, bucket;
cerr << "Enter number count: "; cin >> number_count;
std::cerr << "Enter numbers: \n";
vector <double> numbers(number_count);
for (int i = 0; i < number_count; i++) cin >> numbers[i];
float min = numbers[0];
float max = numbers[0];
for (float x : numbers)
{
if (x < min) min = x;
else if (x > max) max = x;
}
if (max == min) {// ïðîâåðêà íà àíîìàëèþ
std::cout << " Unable to create a histogram." << std::endl;
return 1;
}
cerr << "Enter bucket: "; cin >> bucket;
float k = (max - min) / bucket;
if (numbers.empty()) {//áûëè ëè ââåäåíû ÷èñëà
std::cout << "Empty. unable to create histogram." << std::endl;
return 1;
}
vector <int> stolb(bucket);
vector <int> proz(stolb);
for (int j = 0; j < bucket; j++) stolb[j] = 0;
for (int i = 0; i < number_count; i++)
{
bool flag = false;
for (int j = 0; (j < bucket && !flag); j++)
{
if (numbers[i] >= (min + k * j) && numbers[i] < (min + k * (1 + j)))
{
stolb[j]++;
flag = true;
}
}
if (!flag) stolb[bucket - 1]++;
}
int maxlen = 0;
for (int j = 0; j < bucket; j++)
{
if (maxlen < stolb[j]) maxlen = stolb[j];
}
for (int j = 0; j < bucket; j++)
{
if (stolb[j] < 100) cout << " ";
if (stolb[j] < 10) cout << "";
cout << stolb[j] << " |";
size_t height = stolb[j];
if (maxlen > MAX_ASTERISK)
{
if (maxlen != stolb[j]) height = MAX_ASTERISK * (static_cast <float>(stolb[j]) / maxlen);
else if (maxlen == stolb[j]) height = MAX_ASTERISK;
}
for (int i = 0; i < height; i++) cout << "*";
cout << "\n";
}
int main() {
Input in = input_data();
auto bins = make_histogram(in.numbers, in.bin_count);
show_histogram_svg(bins);
return 0;
}

@ -0,0 +1,24 @@
#include "svg.h"
#include <iostream>
#include <string>
void svg_begin(double width, double height) {
std::cout << "<?xml version='1.0' encoding='UTF-8'?>\n";
std::cout << "<svg ";
std::cout << "width='" << width << "' ";
std::cout << "height='" << height << "' ";
std::cout << "viewBox='0 0 " << width << " " << height << "' ";
std::cout << "xmlns='http://www.w3.org/2000/svg'>\n";
}
void svg_end() {
std::cout << "</svg>\n";
}
void svg_rect(double x, double y, double width, double height, std::string stroke, std::string fill) {
std::cout << "<rect x='" << x << "' y='" << y << "' width='" << width << "' height='" << height << "' stroke='" << stroke << "' fill='" << fill << "' />\n";
}
void svg_text(double left, double baseline, std::string text) {
std::cout << "<text x='" << left << "' y='" << baseline << "'>" << text << "</text>\n";
}

@ -0,0 +1,9 @@
#pragma once
#include <iostream>
#include <string>
#include <vector>
void svg_begin(double width, double height);
void svg_end();
void svg_rect(double x, double y, double width, double height, std::string stroke = "black", std::string fill = "black");
void svg_text(double left, double baseline, std::string text);

@ -31,6 +31,9 @@
<Compiler>
<Add option="-Wall" />
</Compiler>
<Unit filename="Histogram/histogram.cpp" />
<Unit filename="Histogram/histogram_internal.h" />
<Unit filename="unittest.cpp" />
<Extensions>
<lib_finder disable_auto="1" />
</Extensions>

@ -2,4 +2,9 @@
<CodeBlocks_layout_file>
<FileVersion major="1" minor="0" />
<ActiveTarget name="Debug" />
<File name="unittest.cpp" open="1" top="1" tabpos="1" split="0" active="1" splitpos="0" zoom_1="0" zoom_2="0">
<Cursor>
<Cursor1 position="271" topLine="0" />
</Cursor>
</File>
</CodeBlocks_layout_file>

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