Сравнить коммиты
Ничего общего в коммитах. 'b8ad5f3fa4cfc047d9c0764db88a6dbf7ed9472e' и 'ff46d5dc4c80f724bf0e6dcd0bb7793cf8f2c545' имеют совершенно разные истории.
b8ad5f3fa4
...
ff46d5dc4c
Разница между файлами не показана из-за своего большого размера
Загрузить разницу
@ -1,43 +0,0 @@
|
|||||||
#include <vector>
|
|
||||||
#include "histogram.h"
|
|
||||||
#include "histogram_internal.h"
|
|
||||||
using namespace std;
|
|
||||||
|
|
||||||
|
|
||||||
void find_minmax(const vector<double>& numbers, double& min, double& max) {
|
|
||||||
max = 0;
|
|
||||||
if (numbers.size() != 0) {
|
|
||||||
min = numbers[0];
|
|
||||||
max = numbers[0];
|
|
||||||
for (auto el : numbers) {
|
|
||||||
if (el > max) {
|
|
||||||
max = el;
|
|
||||||
}
|
|
||||||
if (el < min) {
|
|
||||||
min = el;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
} else {min = 0;}
|
|
||||||
}
|
|
||||||
|
|
||||||
vector<size_t> make_histogram(const vector<double>& numbers, size_t bin_count) {
|
|
||||||
vector<size_t> bins(bin_count);
|
|
||||||
double max, min;
|
|
||||||
find_minmax(numbers, min, max);
|
|
||||||
double binSize = (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 * binSize;
|
|
||||||
auto hi = min + (j + 1) * binSize;
|
|
||||||
if ((lo <= numbers[i]) && (numbers[i] < hi)) {
|
|
||||||
bins[j]++;
|
|
||||||
found = true;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
if (!found) {
|
|
||||||
bins[bin_count - 1]++;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
return bins;
|
|
||||||
}
|
|
@ -1,4 +0,0 @@
|
|||||||
#pragma once
|
|
||||||
#include <vector>
|
|
||||||
|
|
||||||
std::vector<size_t> make_histogram(const std::vector<double>& numbers, size_t bin_count);
|
|
@ -1,4 +0,0 @@
|
|||||||
#pragma once
|
|
||||||
#include <vector>
|
|
||||||
|
|
||||||
void find_minmax(const std::vector<double>& numbers, double& min, double& max);
|
|
@ -1,32 +1,67 @@
|
|||||||
|
|
||||||
#include <iostream>
|
#include <iostream>
|
||||||
#include <vector>
|
#include <vector>
|
||||||
#include "histogram.h"
|
|
||||||
#include "text.h"
|
|
||||||
#include "svg.h"
|
|
||||||
|
|
||||||
|
|
||||||
using namespace std;
|
using namespace std;
|
||||||
|
|
||||||
struct Input {
|
const size_t SCREEN_WIDTH = 80;
|
||||||
vector<double> numbers;
|
const size_t MAX_ASTERISK = SCREEN_WIDTH - 3 - 1;
|
||||||
size_t bin_count{};
|
|
||||||
};
|
|
||||||
|
|
||||||
Input input_data() {
|
|
||||||
Input in;
|
|
||||||
size_t number_count;
|
|
||||||
cin >> number_count;
|
|
||||||
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() {
|
int main() {
|
||||||
Input in = input_data();
|
size_t numberCount, binCount, maxCount;
|
||||||
auto bins = make_histogram(in.numbers, in.bin_count);
|
double maxx, minn;
|
||||||
show_histogram_svg(bins);
|
cerr << "Enter number count: ";
|
||||||
|
cin >> numberCount;
|
||||||
|
vector<double> numbers(numberCount);
|
||||||
|
cerr << "Fill array \"numbers\"\n";
|
||||||
|
for (int i = 0; i < numberCount; i++) {
|
||||||
|
cin >> numbers[i];
|
||||||
|
}
|
||||||
|
maxx = numbers[0];
|
||||||
|
minn = numbers[0];
|
||||||
|
for (auto el: numbers) {
|
||||||
|
if (el > maxx) {
|
||||||
|
maxx = el;
|
||||||
|
}
|
||||||
|
if (el < minn) {
|
||||||
|
minn = el;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
cerr << "Enter bin count: ";
|
||||||
|
cin >> binCount;
|
||||||
|
double binSize = (maxx - minn) / binCount;
|
||||||
|
vector<size_t> bins(binCount);
|
||||||
|
maxCount = 0;
|
||||||
|
for (size_t i = 0; i < numberCount; i++) {
|
||||||
|
bool found = false;
|
||||||
|
for (size_t j = 0; (j < binCount - 1) && !found; j++) {
|
||||||
|
auto lo = minn + j * binSize;
|
||||||
|
auto hi = minn + (j + 1) * binSize;
|
||||||
|
if ((lo <= numbers[i]) && (numbers[i] < hi)) {
|
||||||
|
bins[j]++;
|
||||||
|
found = true;
|
||||||
|
if (bins[j] > maxCount) {
|
||||||
|
maxCount = bins[j];
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
if (!found) {
|
||||||
|
bins[binCount - 1]++;
|
||||||
|
if (bins[binCount - 1] > maxCount) {
|
||||||
|
maxCount = bins[binCount - 1];
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
for (auto bin: bins) {
|
||||||
|
if (bin < 100) {
|
||||||
|
cout << " ";
|
||||||
|
}
|
||||||
|
if (bin < 10) {
|
||||||
|
cout << " ";
|
||||||
|
}
|
||||||
|
size_t height = maxCount < MAX_ASTERISK? bin: MAX_ASTERISK * (static_cast<double>(bin) / maxCount);
|
||||||
|
cout << bin << "|" << string(height, '*');
|
||||||
|
cout << "\n";
|
||||||
|
}
|
||||||
return 0;
|
return 0;
|
||||||
}
|
}
|
@ -1,61 +0,0 @@
|
|||||||
#include <iostream>
|
|
||||||
#include "svg.h"
|
|
||||||
|
|
||||||
using namespace std;
|
|
||||||
|
|
||||||
const auto IMAGE_WIDTH = 400;
|
|
||||||
const auto IMAGE_HEIGHT = 310;
|
|
||||||
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;
|
|
||||||
|
|
||||||
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";
|
|
||||||
cout << "<line x1='10' y1='2' x2='" << width + 10 <<"' y2='2' stroke-dasharray = '10 10' stroke='black'/>\n";
|
|
||||||
|
|
||||||
}
|
|
||||||
|
|
||||||
void svg_end(double top) {
|
|
||||||
cout << "<line x1='10' y1='" << top << "' x2='" << IMAGE_WIDTH <<"' y2='" << top << "' stroke-dasharray = '10 10' stroke='black'/>\n";
|
|
||||||
cout << "</svg>\n";
|
|
||||||
}
|
|
||||||
|
|
||||||
void svg_text(double left, double baseline, string text) {
|
|
||||||
cout << "<line x1='10' y1='" << baseline - TEXT_BASELINE << "' x2='10' y2='" << baseline <<"' stroke-dasharray = '10 10' stroke='black'/>\n";
|
|
||||||
cout << "<text x='" << left << "' y='" << baseline << "'>" << text <<"</text>\n";
|
|
||||||
}
|
|
||||||
|
|
||||||
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 << "' stroke='" << stroke << "' height='" << height << "' fill='" << fill << "' />\n";
|
|
||||||
cout << "<line x1='" << IMAGE_WIDTH <<"' y1='" << y << "' x2='" << IMAGE_WIDTH <<"' y2='" << y + height <<"' stroke-dasharray = '10 10' stroke='black'/>\n";
|
|
||||||
|
|
||||||
}
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
void show_histogram_svg(const vector<size_t>& bins) {
|
|
||||||
double max = 0, scale = 1;
|
|
||||||
int maxlen = IMAGE_WIDTH - TEXT_WIDTH;
|
|
||||||
for (auto el: bins) {
|
|
||||||
if (max < el) {
|
|
||||||
max = el;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
svg_begin(IMAGE_WIDTH, IMAGE_HEIGHT);
|
|
||||||
double top = 5;
|
|
||||||
scale = max * BLOCK_WIDTH / maxlen;
|
|
||||||
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 / scale, BIN_HEIGHT);
|
|
||||||
top += BIN_HEIGHT;
|
|
||||||
}
|
|
||||||
svg_end(top);
|
|
||||||
}
|
|
@ -1,3 +0,0 @@
|
|||||||
#include <vector>
|
|
||||||
|
|
||||||
void show_histogram_svg(const std::vector<size_t>& bins);
|
|
@ -1,30 +0,0 @@
|
|||||||
#include <iostream>
|
|
||||||
#include <vector>
|
|
||||||
|
|
||||||
using namespace std;
|
|
||||||
|
|
||||||
|
|
||||||
const size_t SCREEN_WIDTH = 80;
|
|
||||||
const size_t MAX_ASTERISK = SCREEN_WIDTH - 3 - 1;
|
|
||||||
|
|
||||||
void show_histogram_text(const vector<size_t>& bins) {
|
|
||||||
size_t maxCount = bins[0];
|
|
||||||
for (auto bin : bins) {
|
|
||||||
if (maxCount < bin) {
|
|
||||||
maxCount = bin;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
for (auto bin : bins) {
|
|
||||||
if (bin < 100)
|
|
||||||
{
|
|
||||||
cout << " ";
|
|
||||||
}
|
|
||||||
if (bin < 10)
|
|
||||||
{
|
|
||||||
cout << " ";
|
|
||||||
}
|
|
||||||
size_t height = maxCount < MAX_ASTERISK ? bin : MAX_ASTERISK * (static_cast<double>(bin) / maxCount);
|
|
||||||
cout << bin << "|" << string(height, '*');
|
|
||||||
cout << "\n";
|
|
||||||
}
|
|
||||||
}
|
|
@ -1,4 +0,0 @@
|
|||||||
#pragma once
|
|
||||||
#include <vector>
|
|
||||||
|
|
||||||
void show_histogram_text(const std::vector<size_t>& bins);
|
|
@ -1,36 +0,0 @@
|
|||||||
#define DOCTEST_CONFIG_NO_MULTITHREADING
|
|
||||||
#define DOCTEST_CONFIG_IMPLEMENT_WITH_MAIN
|
|
||||||
#include "doctest.h"
|
|
||||||
#include "histogram_internal.h"
|
|
||||||
|
|
||||||
TEST_CASE("distinct positive numbers") {
|
|
||||||
double min = 0;
|
|
||||||
double max = 0;
|
|
||||||
find_minmax({1, 2}, min, max);
|
|
||||||
CHECK(min == 1);
|
|
||||||
CHECK(max == 2);
|
|
||||||
}
|
|
||||||
|
|
||||||
TEST_CASE("empty vector") {
|
|
||||||
double min = 0;
|
|
||||||
double max = 0;
|
|
||||||
find_minmax({ }, min, max);
|
|
||||||
CHECK(min != 0);
|
|
||||||
CHECK(max != 0);
|
|
||||||
}
|
|
||||||
|
|
||||||
TEST_CASE("one element") {
|
|
||||||
double min = 0;
|
|
||||||
double max = 0;
|
|
||||||
find_minmax({ 1 }, min, max);
|
|
||||||
CHECK(min == 1);
|
|
||||||
CHECK(max == 1);
|
|
||||||
}
|
|
||||||
|
|
||||||
TEST_CASE("negative elements") {
|
|
||||||
double min = 0;
|
|
||||||
double max = 0;
|
|
||||||
find_minmax({ -81, 2 }, min, max);
|
|
||||||
CHECK(min == -81);
|
|
||||||
CHECK(max == 2);
|
|
||||||
}
|
|
Загрузка…
Ссылка в новой задаче