Родитель
ac32efbbd3
Сommit
6d92425a8c
@ -0,0 +1,34 @@
|
||||
# Prerequisites
|
||||
*.d
|
||||
|
||||
# Compiled Object files
|
||||
*.slo
|
||||
*.lo
|
||||
*.o
|
||||
*.obj
|
||||
|
||||
# Precompiled Headers
|
||||
*.gch
|
||||
*.pch
|
||||
|
||||
# Compiled Dynamic libraries
|
||||
*.so
|
||||
*.dylib
|
||||
*.dll
|
||||
|
||||
# Fortran module files
|
||||
*.mod
|
||||
*.smod
|
||||
|
||||
# Compiled Static libraries
|
||||
*.lai
|
||||
*.la
|
||||
*.a
|
||||
*.lib
|
||||
|
||||
# Executables
|
||||
*.exe
|
||||
*.out
|
||||
*.app
|
||||
|
||||
/curl
|
@ -0,0 +1,73 @@
|
||||
#include "histogram.h"
|
||||
#include "vector"
|
||||
|
||||
using namespace std;
|
||||
|
||||
|
||||
vector<size_t> make_histogram(const vector<double>& numbers, size_t bin_count) {
|
||||
|
||||
vector<size_t> bins(bin_count);
|
||||
vector<size_t> binss(bin_count);
|
||||
|
||||
double max, min;
|
||||
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]++;
|
||||
}
|
||||
}
|
||||
|
||||
int max_count = bins[0];
|
||||
for (size_t i = 0; i < bin_count; i++) {
|
||||
if (bins[i] > max_count) {
|
||||
max_count = bins[i];
|
||||
}
|
||||
}
|
||||
|
||||
if (max_count > 76) {
|
||||
|
||||
for (size_t i = 0; i < bin_count; i++) {
|
||||
int count = bins[i];
|
||||
size_t height = 76 * (static_cast<double>(count) / max_count);
|
||||
bins[i] = height;
|
||||
}
|
||||
}
|
||||
return bins;
|
||||
}
|
||||
|
||||
bool find_minmax(const vector<double>& numbers, double& min, double& max) {
|
||||
if (numbers.size() == 0) {
|
||||
return false;
|
||||
}
|
||||
for (auto i = 0; i < numbers.size(); i++) {
|
||||
if (
|
||||
numbers[i] < 0
|
||||
) return false;
|
||||
}
|
||||
|
||||
min = numbers[0];
|
||||
for (auto i = 0; i < numbers.size(); i++) {
|
||||
if (numbers[i] < min) {
|
||||
min = numbers[i];
|
||||
}
|
||||
}
|
||||
|
||||
max = numbers[0];
|
||||
for (auto i = 0; i < numbers.size(); i++) {
|
||||
if (numbers[i] > max) {
|
||||
max = numbers[i];
|
||||
}
|
||||
}
|
||||
return true;
|
||||
}
|
@ -0,0 +1,13 @@
|
||||
#include <vector>
|
||||
#pragma once
|
||||
|
||||
#ifndef LAB4_HISTOGRAM_H
|
||||
#define LAB4_HISTOGRAM_H
|
||||
|
||||
#endif //LAB4_HISTOGRAM_H
|
||||
|
||||
#include <vector>
|
||||
#include <cstddef>
|
||||
|
||||
std::vector<size_t> make_histogram(const std::vector<double>& numbers, size_t bin_count);
|
||||
bool find_minmax(const std::vector<double>& numbers, double& min, double& max);
|
@ -0,0 +1,40 @@
|
||||
<?xml version="1.0" encoding="UTF-8" standalone="yes" ?>
|
||||
<CodeBlocks_project_file>
|
||||
<FileVersion major="1" minor="6" />
|
||||
<Project>
|
||||
<Option title="lab34" />
|
||||
<Option pch_mode="2" />
|
||||
<Option compiler="gcc" />
|
||||
<Build>
|
||||
<Target title="Debug">
|
||||
<Option output="bin/Debug/lab34" 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/lab34" 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="main.cpp" />
|
||||
<Extensions>
|
||||
<lib_finder disable_auto="1" />
|
||||
</Extensions>
|
||||
</Project>
|
||||
</CodeBlocks_project_file>
|
@ -0,0 +1,21 @@
|
||||
# depslib dependency file v1.0
|
||||
1684755117 source:c:\users\stepa\desktop\lab04\histogram.cpp
|
||||
"histogram.h"
|
||||
"vector"
|
||||
|
||||
1684755161 c:\users\stepa\desktop\lab04\histogram.h
|
||||
<vector>
|
||||
<vector>
|
||||
<cstddef>
|
||||
|
||||
1684755057 source:c:\users\stepa\desktop\lab04\svg.cpp
|
||||
"svg.h"
|
||||
<iostream>
|
||||
<string>
|
||||
<vector>
|
||||
"cmath"
|
||||
|
||||
1684757154 c:\users\stepa\desktop\lab04\svg.h
|
||||
<vector>
|
||||
<string>
|
||||
|
@ -0,0 +1,39 @@
|
||||
#include <iostream>
|
||||
#include "vector"
|
||||
#include "svg.h"
|
||||
#include "histogram.h"
|
||||
//#include "curl/curl.h"
|
||||
using namespace std;
|
||||
|
||||
|
||||
|
||||
|
||||
struct Input {
|
||||
vector<double> numbers;
|
||||
size_t bin_count{};
|
||||
};
|
||||
|
||||
|
||||
Input
|
||||
input_data(istream& inn, bool promt) {
|
||||
size_t number_count;
|
||||
cin >> number_count;
|
||||
Input in;
|
||||
in.numbers.resize(number_count);
|
||||
|
||||
for (size_t i = 0; i < number_count; i++) {
|
||||
cin >> in.numbers[i];
|
||||
}
|
||||
|
||||
cin >> in.bin_count;
|
||||
return in;
|
||||
}
|
||||
|
||||
int main() {
|
||||
// curl_global_init(CURL_GLOBAL_ALL);
|
||||
auto in = input_data(cin, true);
|
||||
auto bins = make_histogram(in.numbers, in.bin_count);
|
||||
show_histogram_text(bins, in.bin_count);
|
||||
//show_histogram_svg(bins);
|
||||
return 0;
|
||||
}
|
@ -0,0 +1,63 @@
|
||||
#include "svg.h"
|
||||
#include <iostream>
|
||||
#include <string>
|
||||
#include <vector>
|
||||
#include "cmath"
|
||||
|
||||
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";
|
||||
}
|
||||
|
||||
int color(int bin, int max_count) {
|
||||
int color;
|
||||
return color = round((10 - (bin * 9) / max_count));
|
||||
}
|
||||
|
||||
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, string fill) {
|
||||
cout << "<rect x='" << x << "' y='" << y << "' width='" << width << "' height='" << height << "' stroke='" << "#" << stroke << stroke << stroke << "' fill='" << "#" << fill << fill << fill << "'>" << "</rect>";
|
||||
}
|
||||
|
||||
void show_histogram_svg(const 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 TEXT_WIDTH = 50;
|
||||
const auto BIN_HEIGHT = 30;
|
||||
const auto BLOCK_WIDTH = 10;
|
||||
|
||||
|
||||
svg_begin(400, 300);
|
||||
double top = 0;
|
||||
double max_count = bins[0];
|
||||
for (size_t i = 0; i < bins.size(); i++) {
|
||||
if (bins[i] > max_count)
|
||||
max_count = bins[i];
|
||||
}
|
||||
for (size_t bin : bins) {
|
||||
int temp_color = color(bin, max_count);
|
||||
const double bin_width = (IMAGE_WIDTH - TEXT_WIDTH) * (bin / max_count);
|
||||
svg_text(TEXT_LEFT, top + TEXT_BASELINE, to_string(bin));
|
||||
svg_rect(TEXT_WIDTH, top, bin_width, BIN_HEIGHT, to_string(temp_color), to_string(temp_color));
|
||||
top += BIN_HEIGHT;
|
||||
}
|
||||
svg_end();
|
||||
}
|
@ -0,0 +1,15 @@
|
||||
|
||||
|
||||
#include <vector>
|
||||
#include <string>
|
||||
|
||||
#ifndef LAB4_SVG_H
|
||||
#define LAB4_SVG_H
|
||||
|
||||
#endif //LAB4_SVG_H
|
||||
|
||||
void svg_begin(double width, double height);
|
||||
void svg_end();
|
||||
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 fill);
|
||||
void show_histogram_svg(const std::vector<size_t>& bins);
|
@ -0,0 +1,13 @@
|
||||
//
|
||||
// Created by stepa on 22.05.2023.
|
||||
//
|
||||
|
||||
#include <vector>
|
||||
#include <cstddef>
|
||||
|
||||
#ifndef LAB4_TEXT_H
|
||||
#define LAB4_TEXT_H
|
||||
|
||||
#endif //LAB4_TEXT_H
|
||||
|
||||
void show_histogram_text(std::vector <size_t> bins, size_t bin_count);
|
Загрузка…
Ссылка в новой задаче