Сравнить коммиты
Ничего общего в коммитах. '15a13d9bfcf3ec278cae97a144761ea09abf4d04' и '0b8d63da09cf99fc2de3777c406019019c27ed3e' имеют совершенно разные истории.
15a13d9bfc
...
0b8d63da09
@ -0,0 +1,4 @@
|
|||||||
|
bin/
|
||||||
|
obj/
|
||||||
|
*.exe
|
||||||
|
*.svg
|
@ -0,0 +1,81 @@
|
|||||||
|
# depslib dependency file v1.0
|
||||||
|
1745823834 source:c:\users\diman3000\desktop\cs-lab34\histogram.cpp
|
||||||
|
"histogram.h"
|
||||||
|
<vector>
|
||||||
|
|
||||||
|
1745823794 c:\users\diman3000\desktop\cs-lab34\histogram.h
|
||||||
|
<vector>
|
||||||
|
|
||||||
|
1745823869 source:c:\users\diman3000\desktop\cs-lab34\text.cpp
|
||||||
|
"text.h"
|
||||||
|
<iostream>
|
||||||
|
|
||||||
|
1745823848 c:\users\diman3000\desktop\cs-lab34\text.h
|
||||||
|
<vector>
|
||||||
|
|
||||||
|
1745824901 source:c:\users\diman3000\desktop\cs-lab34\main.cpp
|
||||||
|
<iostream>
|
||||||
|
<vector>
|
||||||
|
"histogram.h"
|
||||||
|
"svg.h"
|
||||||
|
|
||||||
|
1745824381 c:\users\diman3000\desktop\cs-lab34\svg.h
|
||||||
|
<vector>
|
||||||
|
<string>
|
||||||
|
|
||||||
|
1745824404 source:c:\users\diman3000\desktop\cs-lab34\svg.cpp
|
||||||
|
"svg.h"
|
||||||
|
<iostream>
|
||||||
|
|
||||||
|
1745825391 source:c:\users\diman3000\desktop\cs-lab34\unittest.cpp
|
||||||
|
"doctest.h"
|
||||||
|
"histogram_internal.h"
|
||||||
|
|
||||||
|
1745825250 c:\users\diman3000\desktop\cs-lab34\doctest.h
|
||||||
|
<signal.h>
|
||||||
|
<ciso646>
|
||||||
|
<cstddef>
|
||||||
|
<ostream>
|
||||||
|
<istream>
|
||||||
|
<type_traits>
|
||||||
|
"doctest_fwd.h"
|
||||||
|
<ctime>
|
||||||
|
<cmath>
|
||||||
|
<climits>
|
||||||
|
<math.h>
|
||||||
|
<new>
|
||||||
|
<cstdio>
|
||||||
|
<cstdlib>
|
||||||
|
<cstring>
|
||||||
|
<limits>
|
||||||
|
<utility>
|
||||||
|
<fstream>
|
||||||
|
<sstream>
|
||||||
|
<iostream>
|
||||||
|
<algorithm>
|
||||||
|
<iomanip>
|
||||||
|
<vector>
|
||||||
|
<atomic>
|
||||||
|
<mutex>
|
||||||
|
<set>
|
||||||
|
<map>
|
||||||
|
<unordered_set>
|
||||||
|
<exception>
|
||||||
|
<stdexcept>
|
||||||
|
<csignal>
|
||||||
|
<cfloat>
|
||||||
|
<cctype>
|
||||||
|
<cstdint>
|
||||||
|
<string>
|
||||||
|
<sys/types.h>
|
||||||
|
<unistd.h>
|
||||||
|
<sys/sysctl.h>
|
||||||
|
<AfxWin.h>
|
||||||
|
<windows.h>
|
||||||
|
<io.h>
|
||||||
|
<sys/time.h>
|
||||||
|
<unistd.h>
|
||||||
|
|
||||||
|
1745825157 c:\users\diman3000\desktop\cs-lab34\histogram_internal.h
|
||||||
|
<vector>
|
||||||
|
|
@ -0,0 +1,10 @@
|
|||||||
|
<?xml version="1.0" encoding="UTF-8" standalone="yes" ?>
|
||||||
|
<CodeBlocks_layout_file>
|
||||||
|
<FileVersion major="1" minor="0" />
|
||||||
|
<ActiveTarget name="Debug" />
|
||||||
|
<File name="main.cpp" open="1" top="1" tabpos="1" split="0" active="1" splitpos="0" zoom_1="0" zoom_2="0">
|
||||||
|
<Cursor>
|
||||||
|
<Cursor1 position="757" topLine="16" />
|
||||||
|
</Cursor>
|
||||||
|
</File>
|
||||||
|
</CodeBlocks_layout_file>
|
Разница между файлами не показана из-за своего большого размера
Загрузить разницу
@ -0,0 +1,34 @@
|
|||||||
|
#include "histogram.h"
|
||||||
|
#include <vector>
|
||||||
|
|
||||||
|
using namespace std;
|
||||||
|
|
||||||
|
void find_minmax(const vector<double>& numbers, double& min, double& max) {
|
||||||
|
min = numbers[0];
|
||||||
|
max = numbers[0];
|
||||||
|
for (float x : numbers) {
|
||||||
|
if (x < min) min = x;
|
||||||
|
else if (x > max) max = x;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
vector<size_t> make_histogram(const vector<double>& numbers, size_t bin_count) {
|
||||||
|
double min, max;
|
||||||
|
find_minmax(numbers, min, max);
|
||||||
|
|
||||||
|
float k = (max - min) / bin_count;
|
||||||
|
vector<size_t> bins(bin_count, 0);
|
||||||
|
|
||||||
|
for (double number : numbers) {
|
||||||
|
bool flag = false;
|
||||||
|
for (size_t j = 0; (j < bin_count && !flag); j++) {
|
||||||
|
if (number >= (min + k * j) && number < (min + k * (j + 1))) {
|
||||||
|
bins[j]++;
|
||||||
|
flag = true;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
if (!flag) bins[bin_count - 1]++;
|
||||||
|
}
|
||||||
|
|
||||||
|
return bins;
|
||||||
|
}
|
@ -0,0 +1,4 @@
|
|||||||
|
#pragma once
|
||||||
|
#include <vector>
|
||||||
|
|
||||||
|
std::vector<size_t> make_histogram(const std::vector<double>& numbers, size_t bin_count);
|
@ -0,0 +1,4 @@
|
|||||||
|
#pragma once
|
||||||
|
#include <vector>
|
||||||
|
|
||||||
|
void find_minmax(const std::vector<double>& numbers, double& min, double& max);
|
@ -1,77 +1,35 @@
|
|||||||
#include <iostream>
|
#include <iostream>
|
||||||
#include <vector>
|
#include <vector>
|
||||||
#include <limits>
|
#include "histogram.h"
|
||||||
|
#include "text.h"
|
||||||
|
#include "svg.h"
|
||||||
using namespace std;
|
using namespace std;
|
||||||
|
|
||||||
struct Input {
|
struct Input {
|
||||||
vector<double> numbers;
|
vector<double> numbers;
|
||||||
size_t bin_count{};
|
size_t bin_count;
|
||||||
};
|
};
|
||||||
|
|
||||||
Input input_data() {
|
Input input_data() {
|
||||||
|
Input in;
|
||||||
size_t number_count;
|
size_t number_count;
|
||||||
|
cerr << "Enter number count: ";
|
||||||
cin >> number_count;
|
cin >> number_count;
|
||||||
|
|
||||||
Input in;
|
|
||||||
in.numbers.resize(number_count);
|
in.numbers.resize(number_count);
|
||||||
|
cerr << "Enter numbers: ";
|
||||||
for (size_t i = 0; i < number_count; i++) {
|
for (size_t i = 0; i < number_count; i++) {
|
||||||
cin >> in.numbers[i];
|
cin >> in.numbers[i];
|
||||||
}
|
}
|
||||||
|
|
||||||
|
cerr << "Enter bin count: ";
|
||||||
cin >> in.bin_count;
|
cin >> in.bin_count;
|
||||||
|
|
||||||
return in;
|
return in;
|
||||||
}
|
}
|
||||||
|
|
||||||
void find_minmax(const vector<double>& numbers, double& min, double& max) {
|
|
||||||
if (numbers.empty()) return;
|
|
||||||
|
|
||||||
min = max = numbers[0];
|
|
||||||
|
|
||||||
for (const double& number : numbers) {
|
|
||||||
if (number < min) {
|
|
||||||
min = number;
|
|
||||||
}
|
|
||||||
if (number > max) {
|
|
||||||
max = number;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
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, 0);
|
|
||||||
double bin_width = (max - min) / bin_count;
|
|
||||||
|
|
||||||
for (const double& number : numbers) {
|
|
||||||
if (number == max) {
|
|
||||||
bins[bin_count - 1]++;
|
|
||||||
} else {
|
|
||||||
size_t bin_index = static_cast<size_t>((number - min) / bin_width);
|
|
||||||
if (bin_index >= bin_count) {
|
|
||||||
bin_index = bin_count - 1;
|
|
||||||
}
|
|
||||||
bins[bin_index]++;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
return bins;
|
|
||||||
}
|
|
||||||
|
|
||||||
void show_histogram_text(const vector<size_t>& bins) {
|
|
||||||
for (size_t i = 0; i < bins.size(); i++) {
|
|
||||||
cout << "Bin " << i << ": " << bins[i] << endl;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
int main() {
|
int main() {
|
||||||
auto in = input_data();
|
auto in = input_data();
|
||||||
auto bins = make_histogram(in.numbers, in.bin_count);
|
auto bins = make_histogram(in.numbers, in.bin_count);
|
||||||
show_histogram_text(bins);
|
show_histogram_svg(bins);
|
||||||
|
|
||||||
return 0;
|
return 0;
|
||||||
}
|
}
|
||||||
|
@ -0,0 +1,58 @@
|
|||||||
|
#include "svg.h"
|
||||||
|
#include <iostream>
|
||||||
|
|
||||||
|
using namespace std;
|
||||||
|
|
||||||
|
void svg_begin(double width, double height) {
|
||||||
|
cout << "<?xml version='1.0' encoding='UTF-8'?>\n";
|
||||||
|
cout << "<svg ";
|
||||||
|
cout << "width='" << width << "' 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) {
|
||||||
|
cout << "<text x='" << left << "' y='" << baseline << "'>" << text << "</text>\n";
|
||||||
|
}
|
||||||
|
|
||||||
|
void svg_rect(double x, double y, double width, double height,
|
||||||
|
const string& stroke, const string& fill) {
|
||||||
|
cout << "<rect x='" << x << "' y='" << y << "' width='" << width
|
||||||
|
<< "' height='" << height
|
||||||
|
<< "' stroke='" << stroke
|
||||||
|
<< "' fill='" << fill << "' />\n";
|
||||||
|
}
|
||||||
|
|
||||||
|
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;
|
||||||
|
|
||||||
|
size_t max_count = 0;
|
||||||
|
for (size_t count : bins) {
|
||||||
|
if (count > max_count) {
|
||||||
|
max_count = count;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
double BLOCK_WIDTH = (IMAGE_WIDTH - TEXT_WIDTH) / static_cast<double>(max_count);
|
||||||
|
|
||||||
|
svg_begin(IMAGE_WIDTH, IMAGE_HEIGHT);
|
||||||
|
|
||||||
|
double top = 0;
|
||||||
|
for (size_t bin : bins) {
|
||||||
|
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, "blue", "#aaaaff");
|
||||||
|
top += BIN_HEIGHT;
|
||||||
|
}
|
||||||
|
|
||||||
|
svg_end();
|
||||||
|
}
|
@ -0,0 +1,11 @@
|
|||||||
|
#pragma once
|
||||||
|
#include <vector>
|
||||||
|
#include <string>
|
||||||
|
|
||||||
|
void svg_begin(double width, double height);
|
||||||
|
void svg_end();
|
||||||
|
void svg_text(double left, double baseline, const std::string& text);
|
||||||
|
void svg_rect(double x, double y, double width, double height,
|
||||||
|
const std::string& stroke = "black",
|
||||||
|
const std::string& fill = "black");
|
||||||
|
void show_histogram_svg(const std::vector<size_t>& bins);
|
@ -0,0 +1,33 @@
|
|||||||
|
#include "text.h"
|
||||||
|
#include <iostream>
|
||||||
|
|
||||||
|
using namespace std;
|
||||||
|
|
||||||
|
void show_histogram_text(const vector<size_t>& bins) {
|
||||||
|
const size_t SCREEN_WIDTH = 80;
|
||||||
|
const size_t MAX_ASTERISK = SCREEN_WIDTH - 3 - 1;
|
||||||
|
|
||||||
|
size_t max_count = 0;
|
||||||
|
for (size_t count : bins) {
|
||||||
|
if (count > max_count) {
|
||||||
|
max_count = count;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
for (size_t bin : bins) {
|
||||||
|
if (bin < 100) cout << " ";
|
||||||
|
if (bin < 10) cout << " ";
|
||||||
|
cout << bin << "|";
|
||||||
|
|
||||||
|
size_t height = bin;
|
||||||
|
if (max_count > MAX_ASTERISK) {
|
||||||
|
if (max_count != bin)
|
||||||
|
height = MAX_ASTERISK * (static_cast<float>(bin) / max_count);
|
||||||
|
else
|
||||||
|
height = MAX_ASTERISK;
|
||||||
|
}
|
||||||
|
|
||||||
|
for (size_t i = 0; i < height; i++) cout << "*";
|
||||||
|
cout << "\n";
|
||||||
|
}
|
||||||
|
}
|
@ -0,0 +1,4 @@
|
|||||||
|
#pragma once
|
||||||
|
#include <vector>
|
||||||
|
|
||||||
|
void show_histogram_text(const std::vector<size_t>& bins);
|
@ -0,0 +1,43 @@
|
|||||||
|
#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("all numbers are same") {
|
||||||
|
double min = 0;
|
||||||
|
double max = 0;
|
||||||
|
find_minmax({5, 5, 5, 5}, min, max);
|
||||||
|
CHECK(min == 5);
|
||||||
|
CHECK(max == 5);
|
||||||
|
}
|
||||||
|
|
||||||
|
TEST_CASE("with negative numbers") {
|
||||||
|
double min = 0;
|
||||||
|
double max = 0;
|
||||||
|
find_minmax({-3, -7, -1}, min, max);
|
||||||
|
CHECK(min == -7);
|
||||||
|
CHECK(max == -1);
|
||||||
|
}
|
||||||
|
|
||||||
|
TEST_CASE("single number") {
|
||||||
|
double min = 0;
|
||||||
|
double max = 0;
|
||||||
|
find_minmax({42}, min, max);
|
||||||
|
CHECK(min == 42);
|
||||||
|
CHECK(max == 42);
|
||||||
|
}
|
||||||
|
|
||||||
|
TEST_CASE("empty vector") {
|
||||||
|
double min = 0;
|
||||||
|
double max = 0;
|
||||||
|
find_minmax({}, min, max);
|
||||||
|
CHECK(min == 0);
|
||||||
|
CHECK(max == 0);
|
||||||
|
}
|
Загрузка…
Ссылка в новой задаче