Сommit
196a901350
@ -0,0 +1,4 @@
|
||||
/bin
|
||||
/obj
|
||||
/*.layout
|
||||
/*.depend
|
@ -0,0 +1,46 @@
|
||||
<?xml version="1.0" encoding="UTF-8" standalone="yes" ?>
|
||||
<CodeBlocks_project_file>
|
||||
<FileVersion major="1" minor="6" />
|
||||
<Project>
|
||||
<Option title="18" />
|
||||
<Option pch_mode="2" />
|
||||
<Option compiler="gcc" />
|
||||
<Build>
|
||||
<Target title="Debug">
|
||||
<Option output="bin/Debug/18" prefix_auto="1" extension_auto="1" />
|
||||
<Option object_output="obj/Debug/" />
|
||||
<Option type="1" />
|
||||
<Option compiler="gcc" />
|
||||
<Compiler>
|
||||
<Add option="-g" />
|
||||
</Compiler>
|
||||
</Target>
|
||||
<Target title="Release">
|
||||
<Option output="bin/Release/18" prefix_auto="1" extension_auto="1" />
|
||||
<Option object_output="obj/Release/" />
|
||||
<Option type="1" />
|
||||
<Option compiler="gcc" />
|
||||
<Compiler>
|
||||
<Add option="-O2" />
|
||||
</Compiler>
|
||||
<Linker>
|
||||
<Add option="-s" />
|
||||
</Linker>
|
||||
</Target>
|
||||
</Build>
|
||||
<Compiler>
|
||||
<Add option="-Wall" />
|
||||
<Add option="-fexceptions" />
|
||||
</Compiler>
|
||||
<Unit filename="histogram.cpp" />
|
||||
<Unit filename="histogram.h" />
|
||||
<Unit filename="main.cpp" />
|
||||
<Unit filename="svg.cpp" />
|
||||
<Unit filename="svg.h" />
|
||||
<Unit filename="text.cpp" />
|
||||
<Unit filename="text.h" />
|
||||
<Extensions>
|
||||
<lib_finder disable_auto="1" />
|
||||
</Extensions>
|
||||
</Project>
|
||||
</CodeBlocks_project_file>
|
@ -0,0 +1,43 @@
|
||||
#include "histogram.h"
|
||||
|
||||
bool find_minmax(vector<double> vec, double& min, double& max) {
|
||||
if (vec.size() == 0) {
|
||||
cerr << "Empty vec";
|
||||
return false;
|
||||
}
|
||||
min = vec[0];
|
||||
max = vec[0];
|
||||
for (double x : vec) {
|
||||
if (x < min) {
|
||||
min = x;
|
||||
}
|
||||
else if (x > max)
|
||||
{
|
||||
max = x;
|
||||
}
|
||||
}
|
||||
return true;
|
||||
}
|
||||
vector<size_t> make_histogram(size_t number, vector<double> vec) {
|
||||
vector<size_t> bins(number);
|
||||
double mn, mx;
|
||||
find_minmax(vec, mn, mx);
|
||||
float bin_size = (mx - mn) / number;
|
||||
for (size_t i = 0; i < vec.size(); i++) {
|
||||
bool fl = false;
|
||||
for (size_t j = 0; (j < number - 1) && !fl; j++) {
|
||||
auto lo = mn + j * bin_size;
|
||||
auto hi = mn + (j + 1) * bin_size;
|
||||
if ((lo <= vec[i]) && (vec[i] < hi)) {
|
||||
bins[j]++;
|
||||
fl = true;
|
||||
}
|
||||
}
|
||||
if (!fl) {
|
||||
bins[number - 1]++;
|
||||
|
||||
}
|
||||
}
|
||||
return bins;
|
||||
}
|
||||
|
@ -0,0 +1,5 @@
|
||||
#include <vector>
|
||||
#include <iostream>
|
||||
|
||||
using namespace std;
|
||||
vector<size_t> make_histogram(size_t number, vector<double> vec);
|
@ -0,0 +1,28 @@
|
||||
#include "histogram.h"
|
||||
#include "text.h"
|
||||
#include "svg.h"
|
||||
|
||||
struct Input {
|
||||
vector<double> vec;
|
||||
size_t korz{};
|
||||
};
|
||||
Input input_data() {
|
||||
Input in;
|
||||
size_t n, korz;
|
||||
|
||||
cerr << "Number of elements: ";
|
||||
cin >> n;
|
||||
in.vec.resize(n);
|
||||
cerr << "Elements: ";
|
||||
for (size_t i = 0; i < n; i++)
|
||||
cin >> in.vec[i];
|
||||
cerr << "Enter bin count: ";
|
||||
cin >> in.korz;
|
||||
return in;
|
||||
}
|
||||
|
||||
int main() {
|
||||
auto in = input_data();
|
||||
auto bins = make_histogram(in.korz, in.vec);
|
||||
show_histogram_svg(bins);
|
||||
}
|
@ -0,0 +1,80 @@
|
||||
#include "svg.h"
|
||||
|
||||
using namespace std;
|
||||
|
||||
void
|
||||
svg_begin(double width, double height)
|
||||
{
|
||||
cout << "<?xml version='1.0' encoding='UTF-8'?>\n";
|
||||
cout << "<svg ";
|
||||
cout << "width='" << width << "' ";
|
||||
cout << "height='" << height << "' ";
|
||||
cout << "viewBox='0 0 " << width << " " << height << "' ";
|
||||
cout << "xmlns='http://www.w3.org/2000/svg'>\n";
|
||||
}
|
||||
|
||||
void
|
||||
svg_end()
|
||||
{
|
||||
cout << "</svg>\n";
|
||||
}
|
||||
|
||||
void
|
||||
svg_text(double left, double baseline, const string& text, const string& text_decoration)
|
||||
{
|
||||
cout << "<text x='" << left << "' y='" << baseline << "' text-decoration='" << text_decoration << "'>" << text << "</text>";
|
||||
}
|
||||
|
||||
void
|
||||
svg_rect(double x, double y, double width, double height, string stroke = "black", string fill = "black")
|
||||
{
|
||||
cout << "<rect x='"<<x<<"' y='"<<y<<"' width='"<<width<<"' height='"<<height<<"' stroke='"<<stroke<<"' fill='"<<fill<<"' />";
|
||||
}
|
||||
|
||||
|
||||
void
|
||||
show_histogram_svg(const vector<size_t>& bins)
|
||||
{
|
||||
string decoration;
|
||||
cerr<<"Enter text decoration (none, underline, overline, line-through): ";
|
||||
cin>>decoration;
|
||||
while(!(decoration=="none"||decoration=="underline"||decoration=="overline"||decoration=="line-through"))
|
||||
{
|
||||
cerr<<"Invalid value. Please enter one of: none, underline, overline, line-through: ";
|
||||
cin>>decoration;
|
||||
}
|
||||
|
||||
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 BLACK = "black";
|
||||
const auto RED = "red";
|
||||
const auto MAX_WIDTH = IMAGE_WIDTH-TEXT_WIDTH;
|
||||
|
||||
|
||||
svg_begin(IMAGE_WIDTH,IMAGE_HEIGHT);
|
||||
|
||||
double top = 0;
|
||||
double max_count = bins[0];
|
||||
for (size_t i = 0; i < bins.size(); i++)
|
||||
{
|
||||
if (max_count<bins[i])
|
||||
{
|
||||
max_count=bins[i];
|
||||
}
|
||||
}
|
||||
|
||||
for (size_t bin : bins)
|
||||
{
|
||||
double bin_width = (MAX_WIDTH)*(bin/max_count);
|
||||
svg_text(TEXT_LEFT, top + TEXT_BASELINE, to_string(bin), decoration);
|
||||
svg_rect(TEXT_WIDTH, top, bin_width, BIN_HEIGHT, BLACK, RED);
|
||||
top += BIN_HEIGHT;
|
||||
}
|
||||
|
||||
svg_end();
|
||||
}
|
||||
|
@ -0,0 +1,9 @@
|
||||
#pragma once
|
||||
#include <iostream>
|
||||
#include <vector>
|
||||
#include <string>
|
||||
#include <math.h>
|
||||
|
||||
using namespace std;
|
||||
// Declaration for SVG histogram output
|
||||
void show_histogram_svg(const vector<size_t>& bins);
|
@ -0,0 +1,82 @@
|
||||
#include "text.h"
|
||||
|
||||
void show_histogram(std::vector<size_t> bins) {
|
||||
bool gigant = false;
|
||||
auto spaces = 0;
|
||||
size_t mx_count = 0;
|
||||
for (auto x : bins) {
|
||||
if (x > 76) {
|
||||
gigant = true;
|
||||
}
|
||||
if (x > mx_count) {
|
||||
mx_count = x;
|
||||
}
|
||||
auto len = 0;
|
||||
while (x > 0) {
|
||||
x /= 10;
|
||||
len++;
|
||||
}
|
||||
if (len > spaces) {
|
||||
spaces = len;
|
||||
}
|
||||
}
|
||||
if (spaces == 1) {
|
||||
for (size_t i = 0; i < bins.size(); i++) {
|
||||
std::cout << " " << bins[i] << "|";
|
||||
if (gigant) {
|
||||
if (bins[i] == mx_count) {
|
||||
for (size_t j = 0; j < 76; j++) {
|
||||
std::cout << "*";
|
||||
}
|
||||
}
|
||||
else
|
||||
{
|
||||
for (size_t j = 0; j < 76 * static_cast<double>(bins[i]) / mx_count; j++) {
|
||||
std::cout << "*";
|
||||
}
|
||||
}
|
||||
}
|
||||
else
|
||||
{
|
||||
for (size_t j = 0; j < bins[i]; j++) {
|
||||
std::cout << "*";
|
||||
}
|
||||
std::cout << std::endl;
|
||||
}
|
||||
}
|
||||
}
|
||||
else
|
||||
{
|
||||
for (size_t i = 0; i < bins.size(); i++) {
|
||||
int len = 1;
|
||||
int k = bins[i];
|
||||
for (; k /= 10; ++len);
|
||||
while (len < spaces) {
|
||||
std::cout << " ";
|
||||
len++;
|
||||
}
|
||||
std::cout << bins[i];
|
||||
std::cout << "|";
|
||||
if (gigant) {
|
||||
if (bins[i] == mx_count) {
|
||||
for (size_t j = 0; j < 76; j++) {
|
||||
std::cout << "*";
|
||||
}
|
||||
}
|
||||
else
|
||||
{
|
||||
for (size_t j = 0; j < (76 * static_cast<double>(bins[i]) / mx_count - 1); j++) {
|
||||
std::cout << "*";
|
||||
}
|
||||
}
|
||||
}
|
||||
else
|
||||
{
|
||||
for (size_t j = 0; j < bins[i]; j++) {
|
||||
std::cout << "*";
|
||||
}
|
||||
}
|
||||
std::cout << std::endl;
|
||||
}
|
||||
}
|
||||
}
|
@ -0,0 +1,4 @@
|
||||
#include <iostream>
|
||||
#include <vector>
|
||||
|
||||
void show_histogram(std::vector<size_t> bins);
|
Загрузка…
Ссылка в новой задаче