Родитель
0aa341dc28
Сommit
48471c5828
@ -0,0 +1,86 @@
|
||||
/*#include "histogram.h"
|
||||
|
||||
void
|
||||
find_minmax(const std::vector<double>& numbers, double& min, double& max) {
|
||||
min = numbers[0];
|
||||
max = numbers[0];
|
||||
for (double x : numbers){
|
||||
if (x < min){
|
||||
min = x;
|
||||
}
|
||||
else if (x > max){
|
||||
max = x;
|
||||
}
|
||||
|
||||
}
|
||||
}
|
||||
|
||||
std::vector<size_t> make_histogram(const std::vector<double>& numbers, size_t& bin_count){
|
||||
std::vector<size_t> bins(bin_count);
|
||||
double max, min = 0;
|
||||
find_minmax(numbers, min, max);
|
||||
double bin_size = (max - min) / bin_count;
|
||||
|
||||
for (size_t i = 0; i < numbers.size(); i++) {
|
||||
bool found = false;
|
||||
for (size_t j = 0; (j < bin_count - 1) && !found; j++) {
|
||||
auto lo = min + j * bin_size;
|
||||
auto hi = min + (j + 1) * bin_size;
|
||||
if ((lo <= numbers[i]) && (numbers[i] < hi)) {
|
||||
bins[j]++;
|
||||
found = true;
|
||||
}
|
||||
}
|
||||
if (!found) {
|
||||
bins[bin_count - 1]++;
|
||||
|
||||
}
|
||||
}
|
||||
return bins;
|
||||
}
|
||||
*/
|
||||
#include "histogram.h"
|
||||
void
|
||||
find_minmax(const std::vector<double>& numbers, double& min, double& max) {
|
||||
min = numbers[0];
|
||||
max = numbers[0];
|
||||
for (double x : numbers)
|
||||
{
|
||||
if (x < min){
|
||||
min = x;
|
||||
}
|
||||
else if (x > max){
|
||||
max = x;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
std::vector <size_t>
|
||||
make_histogram(const std::vector<double>& numbers, size_t & bin_count, size_t & number_count, size_t & max_count) {
|
||||
double min, max;
|
||||
find_minmax(numbers, min, max);
|
||||
double bin_size = (max - min) / bin_count;
|
||||
std::vector<size_t> bins(bin_count);
|
||||
max_count = bins[0];
|
||||
for (size_t i = 0; i < number_count; i++){
|
||||
bool found = false;
|
||||
for (size_t j = 0; (j < bin_count - 1) && !found; j++){
|
||||
auto lo = min + j * bin_size;
|
||||
auto hi = min + (j + 1) * bin_size;
|
||||
if ((lo <= numbers[i]) && (numbers[i] < hi)){
|
||||
bins[j]++;
|
||||
found = true;
|
||||
}
|
||||
if (bins[j] > max_count){
|
||||
max_count = bins[j];
|
||||
}
|
||||
}
|
||||
if (!found){
|
||||
bins[bin_count - 1]++;
|
||||
}
|
||||
if (bins[bin_count - 1] > max_count){
|
||||
max_count = bins[bin_count - 1];
|
||||
}
|
||||
}
|
||||
return bins;
|
||||
}
|
@ -0,0 +1,12 @@
|
||||
/*#pragma once
|
||||
#include <iostream>
|
||||
#include <vector>
|
||||
|
||||
std::vector<size_t>
|
||||
make_histogram(const std::vector<double>& numbers, size_t & bin_count);*/
|
||||
#pragma once
|
||||
#include <iostream>
|
||||
#include <vector>
|
||||
|
||||
std::vector<size_t>
|
||||
make_histogram(const std::vector<double>& numbers, size_t & bin_count, size_t & number_count, size_t & max_count);
|
@ -0,0 +1,12 @@
|
||||
/*#pragma once
|
||||
#include <iostream>
|
||||
#include <vector>
|
||||
|
||||
void
|
||||
find_minmax(const std::vector<double>& numbers, double& min, double& max);*/
|
||||
#pragma once
|
||||
#include <iostream>
|
||||
#include <vector>
|
||||
|
||||
void
|
||||
find_minmax(const std::vector<double>& numbers, double& min, double& max);
|
@ -0,0 +1,49 @@
|
||||
#include "svg.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_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 fil = "black"){
|
||||
std::cout<< "<rect x='"<< x <<"' y='"<< y <<"' width='"<< width <<"' height='"<< height <<"' />";
|
||||
}
|
||||
|
||||
void
|
||||
show_histogram_svg(std::vector<size_t>& bins, size_t & max_count,size_t & bin_count) {
|
||||
|
||||
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 = (IMAGE_WIDTH - TEXT_WIDTH)/max_count;
|
||||
|
||||
svg_begin(IMAGE_WIDTH, IMAGE_HEIGHT);
|
||||
|
||||
double top = 0;
|
||||
for (size_t bin : bins) {
|
||||
const double bin_width = BLOCK_WIDTH * bin;
|
||||
svg_text(TEXT_LEFT, top + TEXT_BASELINE, std::to_string(bin));
|
||||
svg_rect(TEXT_WIDTH, top, bin_width, BIN_HEIGHT);
|
||||
top += BIN_HEIGHT;
|
||||
}
|
||||
svg_end();
|
||||
}
|
||||
|
||||
void
|
||||
svg_end() {
|
||||
std::cout << "</svg>\n";
|
||||
}
|
||||
|
@ -0,0 +1,29 @@
|
||||
/*#pragma once
|
||||
#include <iostream>
|
||||
#include <vector>
|
||||
|
||||
void
|
||||
show_histogram_svg(std::vector<size_t>& bins, size_t & max_count,size_t & bin_count);
|
||||
void
|
||||
svg_begin(double width, double height);
|
||||
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 fil);
|
||||
void
|
||||
svg_end();*/
|
||||
#pragma once
|
||||
#include <iostream>
|
||||
#include <vector>
|
||||
//#include <string>
|
||||
|
||||
void
|
||||
show_histogram_svg(std::vector<size_t>& bins, size_t & max_count,size_t & bin_count);
|
||||
void
|
||||
svg_begin(double width, double height);
|
||||
void
|
||||
svg_text(double left, double baseline, std::string text);
|
||||
void
|
||||
svg_rect(double x, double y, double width, double height, double fill_opacity, std::string stroke, std::string fil);
|
||||
void
|
||||
svg_end();
|
@ -0,0 +1,67 @@
|
||||
/*#include "histogram.h"
|
||||
|
||||
void
|
||||
show_histogram_text(std::vector<size_t>& bins, size_t & max_count,size_t & bin_count){
|
||||
const size_t SCREEN_WIDTH = 80;
|
||||
const size_t MAX_ASTERISK = SCREEN_WIDTH - 3 - 1;
|
||||
if (max_count > MAX_ASTERISK){
|
||||
std::vector<size_t> hights(bin_count);
|
||||
for (size_t i = 0; i < bin_count; i++){
|
||||
size_t height = MAX_ASTERISK * (static_cast<double>(bins[i]) / max_count);
|
||||
hights[i] = height;
|
||||
}
|
||||
|
||||
for (size_t i = 0; i < bin_count; i++){
|
||||
printf("%3d|", bins[i]);
|
||||
for (size_t j = 0; j < hights[i]; j++){
|
||||
std::cout<<"*";
|
||||
}
|
||||
std::cout << std::endl;
|
||||
}
|
||||
}
|
||||
else{
|
||||
for (size_t i = 0; i < bin_count; i++)
|
||||
{
|
||||
printf("%3d|", bins[i]);
|
||||
for (size_t j = 0; j < bins[i]; j++){
|
||||
std::cout<<"*";
|
||||
}
|
||||
std::cout << std::endl;
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
*/
|
||||
#include "histogram.h"
|
||||
|
||||
void
|
||||
show_histogram_text(std::vector<size_t>& bins, size_t & max_count,size_t & bin_count){
|
||||
const size_t SCREEN_WIDTH = 80;
|
||||
const size_t MAX_ASTERISK = SCREEN_WIDTH - 3 - 1;
|
||||
if (max_count > MAX_ASTERISK){
|
||||
std::vector<size_t> hights(bin_count);
|
||||
for (size_t i = 0; i < bin_count; i++){
|
||||
size_t height = MAX_ASTERISK * (static_cast<double>(bins[i]) / max_count);
|
||||
hights[i] = height;
|
||||
}
|
||||
|
||||
for (size_t i = 0; i < bin_count; i++){
|
||||
printf("%3d|", bins[i]);
|
||||
for (size_t j = 0; j < hights[i]; j++){
|
||||
std::cout<<"*";
|
||||
}
|
||||
std::cout << std::endl;
|
||||
}
|
||||
}
|
||||
else{
|
||||
for (size_t i = 0; i < bin_count; i++)
|
||||
{
|
||||
printf("%3d|", bins[i]);
|
||||
for (size_t j = 0; j < bins[i]; j++){
|
||||
std::cout<<"*";
|
||||
}
|
||||
std::cout << std::endl;
|
||||
}
|
||||
}
|
||||
|
||||
}
|
@ -0,0 +1,12 @@
|
||||
/*#pragma once
|
||||
#include <iostream>
|
||||
#include <vector>
|
||||
|
||||
void
|
||||
show_histogram_text(std::vector<size_t>& bins, size_t & max_count,size_t & bin_count);*/
|
||||
#pragma once
|
||||
#include <iostream>
|
||||
#include <vector>
|
||||
|
||||
void
|
||||
show_histogram_text(std::vector<size_t>& bins, size_t & max_count,size_t & bin_count);
|
Загрузка…
Ссылка в новой задаче