Сравнить коммиты

...

2 Коммитов

@ -2,9 +2,11 @@
#include <conio.h>
#include <vector>
#include <stdlib.h>
#include "histogram.h"
#include "text.h"
#include "svg01.h"
using namespace std;
const size_t MAX_ELEMENT = 76;
struct Input {
vector<double> numbers;
size_t bin_count{};
@ -31,113 +33,11 @@ input_data(){
return in;
}
void
find_minmax(const vector<double>& numbers, double& min, double& max){
min = numbers[0];
max = numbers[0];
for(size_t i=0; i < numbers.size(); i++){
if (numbers[i] < min)
{
min = numbers[i];
}
else if (numbers[i] > max)
{
max = numbers[i];
}
}
}
vector<size_t>
make_histogram(const vector<double>& numbers, size_t bin_count){
double min, max;
find_minmax(numbers, min, max);
vector<size_t> bins(bin_count);
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 lower_Bound = min + j * bin_size;
auto upper_Bound = min + (j + 1) * bin_size;
if ((lower_Bound <= numbers[i]) && (numbers[i] < upper_Bound))
{
bins[j]++;
found = true;
}
}
if (!found)
{
bins[bin_count - 1]++;
}
}
double max_count = bins[0];
for (size_t j=0; j < bin_count; j++){
if (bins[j] > max_count) {
max_count = bins[j];
}
}
return bins;
}
void
show_histogram_text(const vector<size_t> bins, size_t bin_count){
double max_count = bins[0];
for (size_t j=0; j < bin_count; j++){
if (bins[j] > max_count) {
max_count = bins[j];
}
}
vector<size_t> height(bin_count);
for (size_t j=0; j < bin_count; j++){
height[j] = MAX_ELEMENT * (bins[j]/max_count);
}
if (max_count > MAX_ELEMENT){
for (size_t j=0; j < bin_count; j++)
{
if (bins[j] < 100) {
cout << " ";
if (bins[j] < 10){
cout << " ";
}
}
cout << bins[j] << "|";
for (size_t i=0; i < height[j]; i++){
cout << "*";
}
cout << endl;
}
}
else {
for (size_t j=0; j < bin_count; j++)
{
if (bins[j] < 100) {
cout << " ";
if (bins[j] < 10){
cout << " ";
}
}
cout << bins[j] << "|";
for (size_t i=0; i < bins[j];i++){
cout << "*";
}
cout << endl;
}
}
}
int main()
{
auto in = input_data();
auto bins = make_histogram(in.numbers, in.bin_count);
show_histogram_text(bins, in.bin_count);
std::vector<size_t> bins = make_histogram(in.numbers, in.bin_count);
show_histogram_svg(bins);
return 0;
}

@ -19,9 +19,35 @@ svg_end() {
cout << "</svg>\n";
}
void
svg_text(double left, double baseline, string text) {
cout << "<text x='" << left << "' y='" << baseline << "'>" << text << "</text>";
}
void
svg_rect(double x, double y, double width, double height, string stroke = "midnightblue", string fill = "deeppink"){
cout << "<rect x='" << x << "' y='" << y << "' width='" << width << "' height='" << height << "' stroke='" << stroke << "' fill='" << fill << "'/>";
}
void
show_histogram_svg(const vector<size_t>& bins) {
svg_begin(400, 300);
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 = 10;
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, to_string(bin));
svg_rect(TEXT_WIDTH, top, bin_width, BIN_HEIGHT);
top += BIN_HEIGHT;
}
svg_end();
}

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