code: реализовано задание под номером 6

master
Zakhar 7 месяцев назад
Родитель bc5eb6695c
Сommit 7f4768047e

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

@ -1,5 +1,6 @@
cs-lab34.cbp cs-lab34.cbp
cs-lab34.depend cs-lab34.depend
unittest.cbp unittest.cbp
/unittest.layout
/bin /bin
/obj /obj

@ -1,5 +1,6 @@
#include <iostream> #include <iostream>
#include <vector> #include <vector>
#include <string>
#include "histogram.h" #include "histogram.h"
#include "text.h" #include "text.h"
#include "svg.h" #include "svg.h"
@ -9,6 +10,7 @@ using namespace std;
struct Input { struct Input {
vector<double> numbers; vector<double> numbers;
vector<string> colours;
size_t bin_count{}; size_t bin_count{};
}; };
@ -25,6 +27,13 @@ Input input_data() {
cin >> in.bin_count; cin >> in.bin_count;
in.numbers.resize(number_count); in.numbers.resize(number_count);
in.colours.resize(in.bin_count);
cerr << "Enter colours: (press A for choose default colour) ";
for (size_t i = 0; i < in.bin_count; i++)
{
cin >> in.colours[i];
}
cerr << "Enter numbers: "; cerr << "Enter numbers: ";
for (size_t i = 0; i < number_count; i++) for (size_t i = 0; i < number_count; i++)
@ -46,7 +55,7 @@ int main(){
auto bins = make_histogram(in.numbers, in.bin_count); auto bins = make_histogram(in.numbers, in.bin_count);
show_histogram_svg(bins); show_histogram_svg(bins, in.colours);
} }

@ -1,5 +1,6 @@
#include <iostream> #include <iostream>
#include <vector> #include <vector>
#include <string>
#include "svg.h" #include "svg.h"
void void
@ -22,12 +23,12 @@ svg_text(double left, double baseline, std::string text) {
std::cout << "<text x='" << left << "' y='"<< baseline << "' > " << text << " </text>"; std::cout << "<text x='" << left << "' y='"<< baseline << "' > " << text << " </text>";
} }
void svg_rect(double x, double y, double width, double height){ void svg_rect(double x, double y, double width, double height, std::string colour){
std::cout << "<rect x='" << x << "' y='" << y << "' width='" << width << "' height='" << height << " ' stroke='black' fill='#33A220' ></rect>"; std::cout << "<rect x='" << x << "' y='" << y << "' width='" << width << "' height='" << height << " ' stroke='black' fill='"<< colour <<"' ></rect>";
} }
void void
show_histogram_svg(const std::vector<std::size_t>& bins) { show_histogram_svg(const std::vector<std::size_t>& bins, const std::vector<std::string>& colours) {
const auto IMAGE_WIDTH = 400; const auto IMAGE_WIDTH = 400;
const auto IMAGE_HEIGHT = 300; const auto IMAGE_HEIGHT = 300;
@ -51,7 +52,11 @@ show_histogram_svg(const std::vector<std::size_t>& bins) {
for (std::size_t i=0; i < bins.size(); i++) { for (std::size_t i=0; i < bins.size(); i++) {
double bin_width = BLOCK_WIDTH * bins[i]; double bin_width = BLOCK_WIDTH * bins[i];
svg_text(TEXT_LEFT, top + TEXT_BASELINE, std::to_string(bins[i])); svg_text(TEXT_LEFT, top + TEXT_BASELINE, std::to_string(bins[i]));
svg_rect(TEXT_WIDTH, top, bin_width, BIN_HEIGHT); if (colours[i] != "A") {
svg_rect(TEXT_WIDTH, top, bin_width, BIN_HEIGHT, colours[i]);
} else {
svg_rect(TEXT_WIDTH, top, bin_width, BIN_HEIGHT, "GREEN");
}
top += BIN_HEIGHT; top += BIN_HEIGHT;
} }
svg_end(); svg_end();
@ -59,7 +64,11 @@ show_histogram_svg(const std::vector<std::size_t>& bins) {
for (std::size_t i=0; i < bins.size(); i++) { for (std::size_t i=0; i < bins.size(); i++) {
double bin_width= MAX_ASTERISK * (static_cast<double>(bins[i]) / maxbin); double bin_width= MAX_ASTERISK * (static_cast<double>(bins[i]) / maxbin);
svg_text(TEXT_LEFT, top + TEXT_BASELINE, std::to_string(bins[i])); svg_text(TEXT_LEFT, top + TEXT_BASELINE, std::to_string(bins[i]));
svg_rect(TEXT_WIDTH, top, bin_width, BIN_HEIGHT); if (colours[i] != "A") {
svg_rect(TEXT_WIDTH, top, bin_width, BIN_HEIGHT, colours[i]);
} else {
svg_rect(TEXT_WIDTH, top, bin_width, BIN_HEIGHT, "GREEN");
}
top += BIN_HEIGHT; top += BIN_HEIGHT;
} }
svg_end(); svg_end();

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

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