Hihoffff 2 недель назад
Сommit 5de8e3f61a

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

@ -0,0 +1,2 @@
/bin
/obj

@ -0,0 +1,54 @@
using namespace std;
#include "histogram.h"
#include "text.h"
#include "svg.h"
struct Input {
std::vector<double> numbers;
size_t bin_count{};
};
Input input_data(istream& in, bool promt);
int main()
{
Input in = input_data(cin);
std::vector<size_t> bins = make_histogram(in.numbers, in.bin_count);
show_histogram_svg(bins);
return 0;
}
Input input_data(istream& in, bool promt) {
Input input_struct;
size_t countOfNumbers;
if(promt){cerr << "Input your count of numbers:\n";}
cin >> countOfNumbers;
input_struct.numbers.resize(countOfNumbers);
if(promt){cerr << "Input numbers:\n";}
for (int i = 0; i < countOfNumbers; i++) {
if(promt){cerr << i << ":" << endl;}
cin >> input_struct.numbers[i];
}
if(promt){
cerr << endl;
cerr << "Input bin count:\n";
}
cin >> input_struct.bin_count;
return input_struct;
}

@ -0,0 +1,42 @@
#include "svg.h"
#include "text.h"
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 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 BIN_HEIGHT = 30;
const auto TEXT_WIDTH = 50;
svg_begin(400, 300);
size_t maxCount = maxBin(bins);
double top = 0;
for (size_t bin : bins) {
const double bin_width = (IMAGE_WIDTH - TEXT_WIDTH) * (bin / double(maxCount));
svg_text(TEXT_LEFT, top + TEXT_BASELINE, std::to_string(bin));
svg_rect(TEXT_WIDTH, top, bin_width, BIN_HEIGHT,"red","#006B3C");
top += BIN_HEIGHT;
}
svg_end();
}
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 fills="black"){
std::cout << "<rect x='"<<x<<"' y='"<<y<<"' width='"<<width<<"' height='"<<height<<"' stroke='"<<stroke<<"' fill='"<<fills<<"' />";
}

12
svg.h

@ -0,0 +1,12 @@
#ifndef SVG_H_INCLUDED
#define SVG_H_INCLUDED
#include <vector>
#include <iostream>
void svg_begin(double width, double height);
void svg_end();
void show_histogram_svg(const std::vector<size_t>& bins);
void svg_text(double left, double baseline, std::string text);
void svg_rect(double x, double y, double width, double height,std::string stroke, std::string fills);
#endif // SVG_H_INCLUDED

@ -0,0 +1,41 @@
#include "text.h"
void show_histogram_text(std::vector<size_t> bins , size_t bin_count) {
size_t maxCount = maxBin(bins);
size_t count_stars;
for (size_t i = 0; i < bin_count; i++) {
if (bins[i] < 100) {
std::cout << " ";
}
if (bins[i] < 10) {
std::cout << " ";
}
std::cout << bins[i];
std::cout << "|";
if (maxCount > MAX_ASTERISK) {
count_stars = MAX_ASTERISK * (static_cast<double>(bins[i]) / maxCount);
}
else {
count_stars = bins[i];
}
for (size_t i2 = 0; i2 < count_stars; i2++) {
std::cout << "*";
}
std::cout << std::endl;
}
}
const size_t maxBin(std::vector<size_t> bins) {
size_t max = bins[0];
for (int i = 1; i < bins.size(); i++) {
if (max < bins[i]) {
max = bins[i];
}
}
return max;
}

@ -0,0 +1,12 @@
#ifndef TEXT_H_INCLUDED
#define TEXT_H_INCLUDED
#include <iostream>
#include <vector>
const size_t SCREEN_WIDTH = 80;
const size_t MAX_ASTERISK = SCREEN_WIDTH - 3 - 1;
void show_histogram_text(std::vector<size_t> bins , size_t bin_count);
const size_t maxBin(std::vector<size_t> bins);
#endif // TEXT_H_INCLUDED
Загрузка…
Отмена
Сохранить