code: добавлен вывод шкалы в формате svg

main
NikitkinTY 2 лет назад
Родитель 554cdd5d11
Сommit b7fd7da9cb

7
.gitignore поставляемый

@ -2,4 +2,11 @@
/obj /obj
/cs-lab34.layout /cs-lab34.layout
/cs-lab34.depend /cs-lab34.depend
/a.out
/start_01.sh
/test
/unittest.depend
/NUL
/unittest.layout /unittest.layout
/start.sh
/cs-lab34

@ -32,7 +32,14 @@
<Add option="-Wall" /> <Add option="-Wall" />
<Add option="-fexceptions" /> <Add option="-fexceptions" />
</Compiler> </Compiler>
<Unit filename=".gitignore" />
<Unit filename="histogram.cpp" />
<Unit filename="histogram.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>

@ -1,17 +1,25 @@
#include "histogram.h" #include "histogram.h"
#include "histogram_internal.h"
void void
find_minmax (const std::vector<double>& numbers, double& min, double& max) { find_minmax (const std::vector<double>& numbers, double& min, double& max) {
if (numbers.size() == 0) { //empty array situation check
return;
} else {
min = numbers[0]; min = numbers[0];
for(double element : numbers) for (double element : numbers) {
{ if (element < min) {
if(element < min) {min = element;} min = element;
}
} }
max = numbers[0]; max = numbers[0];
for(double element : numbers) for (double element : numbers) {
{ if (element > max) {
if(element > max) {max = element;} max = element;
}
}
return;
} }
} }
@ -28,19 +36,18 @@ make_histogram(const std::vector<double>& numbers, std::size_t bin_count){
std::vector <std::size_t> bins(bin_count); std::vector <std::size_t> bins(bin_count);
for(std::size_t i = 0; i < numbers.size(); ++i) //Checking if a number is in a bin. for (std::size_t i = 0; i < numbers.size(); ++i) { //Checking if a number is in a bin.
{
bool found = false; bool found = false;
for(std::size_t j = 0; (j < bin_count - 1) && !found; ++j) for (std::size_t j = 0; (j < bin_count - 1) && !found; ++j) {
{
if ( (numbers[i] >= (min + j * bin_size)) && //Where (min + j * bin_size) is equal to the lower border if ( (numbers[i] >= (min + j * bin_size)) && //Where (min + j * bin_size) is equal to the lower border
(numbers[i] < (min + (j + 1) * bin_size)) ) //and (min + (j + 1) * bin_size) is equal to the higher border. (numbers[i] < (min + (j + 1) * bin_size)) ) { //and (min + (j + 1) * bin_size) is equal to the higher border.
{
++bins[j]; ++bins[j];
found = true; found = true;
} }
} }
if(!found) {++bins[bin_count - 1];} //A special case when current number is equal to the maximum. if (!found) {
++bins[bin_count - 1]; //A special case when current number is equal to the maximum.
}
} }
return bins; return bins;
} }

@ -5,13 +5,14 @@
#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 interval_task{}; size_t interval_task{};
}; };
@ -29,15 +30,22 @@ input_data() {
cin >> in.bin_count; cin >> in.bin_count;
//cin >> in.interval_task; cin >> in.interval_task;
if ((in.interval_task >= 2.0) && (in.interval_task <= 9.0)) {
return in; return in;
} else {
std::cout << "ERROR";
exit(1);
}
} }
int main() { int main() {
auto in = input_data(); auto in = input_data();
auto 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.interval_task);
return 0; return 0;
} }

@ -0,0 +1,3 @@
<?xml version='1.0' encoding='UTF-8'?>
<svg width='410' height='300' viewBox='0 0 410 300' xmlns='http://www.w3.org/2000/svg'>
<text x='20' y='20'>9</text><rect x='50' y='0' width='31.5' height='30' stroke='#483D8B' fill='#9370DB' /><text x='20' y='50'>33</text><rect x='50' y='30' width='115.5' height='30' stroke='#483D8B' fill='#9370DB' /><text x='20' y='80'>100</text><rect x='50' y='60' width='350' height='30' stroke='#483D8B' fill='#9370DB' /><rect x='50' y='100' width='60' height='8' stroke='chocolate' fill='tan' /><rect x='122.5' y='100' width='60' height='8' stroke='chocolate' fill='tan' /><rect x='195' y='100' width='60' height='8' stroke='chocolate' fill='tan' /><rect x='267.5' y='100' width='60' height='8' stroke='chocolate' fill='tan' /><rect x='340' y='100' width='60' height='8' stroke='chocolate' fill='tan' /><text x='20' y='130'>0</text><text x='112.25' y='130'>6</text><text x='392' y='130'>30</text></svg>

После

Ширина:  |  Высота:  |  Размер: 933 B

@ -0,0 +1,6 @@
142
1 1 1 1 1 1 1 1 1
2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2
3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3
3
6

@ -0,0 +1,100 @@
#include <iostream>
#include "svg.h"
#include <string.h>
#include <math.h>
static void
find_max (const std::vector<std::size_t>& numbers, std::size_t& max) {
max = numbers[0];
for (double element : numbers) {
if (element > max) {
max = element;
}
}
}
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_text(double left, double baseline, std::string text) {
std::cout << "<text x='" << left << "' y='" << baseline << "'>" << text << "</text>";
}
void
svg_rect (double x, double y, double width, double height, std::string stroke = "black", std::string fill = "black") {
std::cout << "<rect x='" << x << "' y='" << y << "' width='" << width << "' height='" << height << "' stroke='" << stroke << "' fill='" << fill << "' />";
}
void
show_histogram_svg (const std::vector<size_t>& bins, std::size_t& interval_task) {
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 = 50;
const auto MAX_WIDTH = IMAGE_WIDTH - TEXT_WIDTH;
//const auto SPACE_BETWEEN_INTERVALS = 10;
const auto INTERVAL_SCALED = interval_task * 10;
const auto SCALE_WIDTH = 8;
//const auto HALF_SPACE_BETWEEN_INTERVALS = static_cast<double>(SPACE_BETWEEN_INTERVALS) / (2);
const auto HALF_DEFAULT_FONT = 4;
const auto MINIMAL_FONT_SPACE = 4 * 4;
std::size_t max_bin;
find_max(bins, max_bin);
double modifier;
modifier = static_cast<double>(MAX_WIDTH) / (max_bin);
size_t times;
for (times = 0; (times * INTERVAL_SCALED) < (MAX_WIDTH - MINIMAL_FONT_SPACE); ++times);
double interval_space = static_cast<double>(MAX_WIDTH - INTERVAL_SCALED * (times - 1)) / (times - 2);
double half_interval_space = interval_space / 2;
svg_begin(410, 300);
double top = 0;
for (size_t bin : bins) {
const double bin_width = modifier * bin;
svg_text(TEXT_LEFT, top + TEXT_BASELINE, std::to_string(bin));
svg_rect(TEXT_WIDTH, top, bin_width, BIN_HEIGHT, "#483D8B", "#9370DB");
top += BIN_HEIGHT;
}
top += 10;
times -= 1;
for (size_t i = 0; i < times; ++i) {
svg_rect(TEXT_WIDTH + i * (INTERVAL_SCALED + interval_space), top, INTERVAL_SCALED, SCALE_WIDTH, "chocolate", "tan");
}
top += 10;
svg_text(TEXT_LEFT, top + TEXT_BASELINE, std::to_string(0));
svg_text(TEXT_WIDTH + INTERVAL_SCALED + half_interval_space - HALF_DEFAULT_FONT, top + TEXT_BASELINE, std::to_string(interval_task));
svg_text(TEXT_WIDTH + (INTERVAL_SCALED * times) + interval_space * (times - 1) - (HALF_DEFAULT_FONT * 2), top + TEXT_BASELINE, std::to_string(interval_task * times));
svg_end();
}

@ -0,0 +1,4 @@
#include <vector>
void
show_histogram_svg(const std::vector<std::size_t>& bins, std::size_t& interval_task);

@ -1,12 +1,16 @@
#include <iostream> #include <iostream>
#include "text.h" #include "text.h"
const size_t screen_width = 80;
const size_t max_asterisk = screen_width - 3 - 1;
static void static void
find_max (const std::vector<std::size_t>& numbers, std::size_t& max) { find_max (const std::vector<std::size_t>& numbers, std::size_t& max) {
max = numbers[0]; max = numbers[0];
for(double element : numbers) for (double element : numbers) {
{ if (element > max) {
if(element > max) {max = element;} max = element;
}
} }
} }
@ -23,22 +27,29 @@ show_histogram_text(const std::vector<std::size_t>& bins){
* In other case, histogram won't be scaled, i.e, asterisk count depends on the current bin number. * In other case, histogram won't be scaled, i.e, asterisk count depends on the current bin number.
*/ */
if(max_bin > 76) {modifier = static_cast<double>(74) / (max_bin);} if (max_bin > max_asterisk) {
else {modifier = 1;} modifier = static_cast<double>(max_asterisk) / (max_bin);
} else {
for(std::size_t i = 0; i < bins.size(); ++i) //Histogram output with alignment, if necessary. modifier = 1;
{
if(bins[i] >= 10)
{
if(bins[i] >= 100) {std::cout << bins[i] << '|';} //Output a three-digit number.
else {std::cout << ' ' << bins[i] << '|';} //Output a two-digit number with alignment.
} }
else {std::cout << " " << bins[i] << '|';} //Output a single-digit number with alignment.
for (std::size_t i = 0; i < bins.size(); ++i) { //Histogram output with alignment, if necessary.
if (bins[i] >= 10) {
if (bins[i] >= 100) {
std::cout << bins[i] << '|';
} //Output a three-digit number.
else {
std::cout << ' ' << bins[i] << '|';
} //Output a two-digit number with alignment.
} else {
std::cout << " " << bins[i] << '|';
} //Output a single-digit number with alignment.
size_t height = modifier * bins[i]; //Height stands for the number of output asterisks. size_t height = modifier * bins[i]; //Height stands for the number of output asterisks.
for(size_t k = 0; k < height; ++k) for (size_t k = 0; k < height; ++k) {
std::cout << '*'; std::cout << '*';
}
std::cout << "\n"; std::cout << "\n";
} }
} }

@ -1,8 +1,3 @@
#ifndef HISTOGRAM_H_INCLUDED
#define HISTOGRAM_H_INCLUDED
#endif // HISTOGRAM_H_INCLUDED
#include <vector> #include <vector>
void void

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