Добавление потока
Этот коммит содержится в:
7
.gitignore
поставляемый
7
.gitignore
поставляемый
@@ -1,4 +1,7 @@
|
|||||||
|
|
||||||
bin/
|
bin/
|
||||||
obj/
|
obj/
|
||||||
*.exe
|
*.cbp
|
||||||
*.svg
|
*.layout
|
||||||
|
*.depend
|
||||||
|
*.user
|
||||||
|
|||||||
38
cs-lab34.cbp
38
cs-lab34.cbp
@@ -1,38 +0,0 @@
|
|||||||
<?xml version="1.0" encoding="UTF-8" standalone="yes" ?>
|
|
||||||
<CodeBlocks_project_file>
|
|
||||||
<FileVersion major="1" minor="6" />
|
|
||||||
<Project>
|
|
||||||
<Option title="cs-lab34" />
|
|
||||||
<Option pch_mode="2" />
|
|
||||||
<Option compiler="gcc" />
|
|
||||||
<Build>
|
|
||||||
<Target title="Debug">
|
|
||||||
<Option output="bin/Debug/cs-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/cs-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 />
|
|
||||||
</Project>
|
|
||||||
</CodeBlocks_project_file>
|
|
||||||
@@ -1,81 +0,0 @@
|
|||||||
# 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>
|
|
||||||
|
|
||||||
@@ -1,10 +0,0 @@
|
|||||||
<?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>
|
|
||||||
@@ -1,34 +1,41 @@
|
|||||||
#include "histogram.h"
|
#include "histogram.h"
|
||||||
#include <vector>
|
#include <vector>
|
||||||
|
|
||||||
using namespace std;
|
using namespace std;
|
||||||
|
|
||||||
void find_minmax(const vector<double>& numbers, double& min, double& max) {
|
void find_minmax(const vector<double>& numbers, double& min, double& max) {
|
||||||
min = numbers[0];
|
if (numbers.empty())
|
||||||
|
{
|
||||||
|
min = 0;
|
||||||
|
max = 0;
|
||||||
|
return;
|
||||||
|
}
|
||||||
max = numbers[0];
|
max = numbers[0];
|
||||||
for (float x : numbers) {
|
min = numbers[0];
|
||||||
|
for (double x : numbers) {
|
||||||
if (x < min) min = x;
|
if (x < min) min = x;
|
||||||
else if (x > max) max = x;
|
else if (x > max) max = x;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
vector<size_t> make_histogram(const vector<double>& numbers, size_t bin_count) {
|
vector<size_t> make_histogram(vector<double> numbers, size_t bin_count) {
|
||||||
double min, max;
|
double min, max;
|
||||||
find_minmax(numbers, min, max);
|
find_minmax(numbers, min, max);
|
||||||
|
double bin_size = (max - min) / bin_count;
|
||||||
|
vector<size_t> bins(bin_count);
|
||||||
|
|
||||||
float k = (max - min) / bin_count;
|
for (size_t i = 0; i < numbers.size(); i++) {
|
||||||
vector<size_t> bins(bin_count, 0);
|
bool found = false;
|
||||||
|
for (size_t j = 0; (j < bin_count - 1) && !found; j++) {
|
||||||
for (double number : numbers) {
|
auto lo = min + j * bin_size;
|
||||||
bool flag = false;
|
auto hi = min + (j + 1) * bin_size;
|
||||||
for (size_t j = 0; (j < bin_count && !flag); j++) {
|
if ((lo <= numbers[i]) && (numbers[i] < hi)) {
|
||||||
if (number >= (min + k * j) && number < (min + k * (j + 1))) {
|
|
||||||
bins[j]++;
|
bins[j]++;
|
||||||
flag = true;
|
found = true;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
if (!flag) bins[bin_count - 1]++;
|
if (!found) {
|
||||||
|
bins[bin_count - 1]++;
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
return bins;
|
return bins;
|
||||||
}
|
}
|
||||||
|
|||||||
11
histogram.h
11
histogram.h
@@ -1,4 +1,9 @@
|
|||||||
#pragma once
|
#ifndef HISTOGRAM_H_INCLUDED
|
||||||
#include <vector>
|
#define HISTOGRAM_H_INCLUDED
|
||||||
|
|
||||||
std::vector<size_t> make_histogram(const std::vector<double>& numbers, size_t bin_count);
|
#include <vector>
|
||||||
|
using namespace std;
|
||||||
|
|
||||||
|
vector<size_t> make_histogram(vector<double> numbers, size_t bin_count);
|
||||||
|
|
||||||
|
#endif
|
||||||
|
|||||||
@@ -1,4 +1,6 @@
|
|||||||
#pragma once
|
#ifndef HISTOGRAM_INTERNAL_H_INCLUDED
|
||||||
#include <vector>
|
#define HISTOGRAM_INTERNAL_H_INCLUDED
|
||||||
|
|
||||||
void find_minmax(const std::vector<double>& numbers, double& min, double& max);
|
void find_minmax(const std::vector<double>& numbers, double& min, double& max);
|
||||||
|
|
||||||
|
#endif
|
||||||
|
|||||||
50
svg.cpp
50
svg.cpp
@@ -1,56 +1,60 @@
|
|||||||
#include "svg.h"
|
|
||||||
#include <iostream>
|
#include <iostream>
|
||||||
|
#include <vector>
|
||||||
|
#include <string>
|
||||||
|
#include "svg.h"
|
||||||
|
|
||||||
using namespace std;
|
using namespace std;
|
||||||
|
|
||||||
void svg_begin(double width, double height) {
|
void svg_begin(double width, double height) {
|
||||||
cout << "<?xml version='1.0' encoding='UTF-8'?>\n";
|
cout << "<?xml version='1.0' encoding='UTF-8'?>\n";
|
||||||
cout << "<svg ";
|
cout << "<svg "
|
||||||
cout << "width='" << width << "' height='" << height << "' ";
|
<< "width='" << width << "' "
|
||||||
cout << "viewBox='0 0 " << width << " " << height << "' ";
|
<< "height='" << height << "' "
|
||||||
cout << "xmlns='http://www.w3.org/2000/svg'>\n";
|
<< "viewBox='0 0 " << width << " " << height << "' "
|
||||||
|
<< "xmlns='http://www.w3.org/2000/svg'>\n";
|
||||||
}
|
}
|
||||||
|
|
||||||
void svg_end() {
|
void svg_end() {
|
||||||
cout << "</svg>\n";
|
cout << "</svg>\n";
|
||||||
}
|
}
|
||||||
|
|
||||||
void svg_text(double left, double baseline, const string& text) {
|
void svg_text(double left, double baseline, string text) {
|
||||||
cout << "<text x='" << left << "' y='" << baseline << "'>" << text << "</text>\n";
|
cout << "<text x='" << left << "' y='" << baseline << "'>" << text << "</text>\n";
|
||||||
}
|
}
|
||||||
|
|
||||||
void svg_rect(double x, double y, double width, double height,
|
void svg_rect(double x, double y, double width, double height, string stroke = "black", string fill = "black") {
|
||||||
const string& stroke, const string& fill) {
|
cout << "<rect x='" << x << "' y='" << y << "' width='" << width << "' height='" << height
|
||||||
cout << "<rect x='" << x << "' y='" << y << "' width='" << width
|
<< "' stroke='" << stroke << "' fill='" << fill << "' />\n";
|
||||||
<< "' height='" << height
|
|
||||||
<< "' stroke='" << stroke
|
|
||||||
<< "' fill='" << fill << "' />\n";
|
|
||||||
}
|
}
|
||||||
|
|
||||||
void show_histogram_svg(const vector<size_t>& bins) {
|
void show_histogram_svg(const vector<size_t>& bins) {
|
||||||
|
if (bins.empty()) {
|
||||||
|
cerr << "Error: Empty bins vector\n";
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
const auto IMAGE_WIDTH = 400;
|
const auto IMAGE_WIDTH = 400;
|
||||||
const auto IMAGE_HEIGHT = 300;
|
const auto IMAGE_HEIGHT = 300;
|
||||||
const auto TEXT_LEFT = 20;
|
const auto TEXT_LEFT = 20;
|
||||||
const auto TEXT_BASELINE = 20;
|
const auto TEXT_BASELINE = 20;
|
||||||
const auto TEXT_WIDTH = 50;
|
const auto TEXT_WIDTH = 50;
|
||||||
const auto BIN_HEIGHT = 30;
|
const auto BIN_HEIGHT = 30;
|
||||||
|
const auto MAX_WIDTH = IMAGE_WIDTH - TEXT_WIDTH;
|
||||||
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);
|
svg_begin(IMAGE_WIDTH, IMAGE_HEIGHT);
|
||||||
|
|
||||||
double top = 0;
|
double top = 0;
|
||||||
|
size_t max_count = bins[0];
|
||||||
for (size_t bin : bins) {
|
for (size_t bin : bins) {
|
||||||
double bin_width = BLOCK_WIDTH * bin;
|
if (max_count < bin) {
|
||||||
|
max_count = bin;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
for (size_t bin : bins) {
|
||||||
|
const double bin_width = MAX_WIDTH * (static_cast<double>(bin) / max_count);
|
||||||
svg_text(TEXT_LEFT, top + TEXT_BASELINE, to_string(bin));
|
svg_text(TEXT_LEFT, top + TEXT_BASELINE, to_string(bin));
|
||||||
svg_rect(TEXT_WIDTH, top, bin_width, BIN_HEIGHT, "blue", "#aaaaff");
|
svg_rect(TEXT_WIDTH, top, bin_width, BIN_HEIGHT, "black", "red");
|
||||||
top += BIN_HEIGHT;
|
top += BIN_HEIGHT;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
16
svg.h
16
svg.h
@@ -1,11 +1,9 @@
|
|||||||
#pragma once
|
#ifndef SVG_H_INCLUDED
|
||||||
#include <vector>
|
#define SVG_H_INCLUDED
|
||||||
#include <string>
|
|
||||||
|
#include <vector>
|
||||||
|
#include <cstddef> // Äëÿ size_t
|
||||||
|
|
||||||
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);
|
void show_histogram_svg(const std::vector<size_t>& bins);
|
||||||
|
|
||||||
|
#endif // SVG_H_INCLUDED
|
||||||
|
|||||||
46
text.cpp
46
text.cpp
@@ -1,33 +1,37 @@
|
|||||||
#include "text.h"
|
#include "text.h"
|
||||||
#include <iostream>
|
#include <iostream>
|
||||||
|
|
||||||
using namespace std;
|
using namespace std;
|
||||||
|
|
||||||
void show_histogram_text(const vector<size_t>& bins) {
|
void show_histogram_text(vector<size_t> bins, size_t bin_count) {
|
||||||
const size_t SCREEN_WIDTH = 80;
|
const size_t SCREEN_WIDTH = 80;
|
||||||
const size_t MAX_ASTERISK = SCREEN_WIDTH - 3 - 1;
|
const size_t MAX_ASTERISK = SCREEN_WIDTH - 3 - 1;
|
||||||
|
double max_bin;
|
||||||
size_t max_count = 0;
|
size_t height;
|
||||||
for (size_t count : bins) {
|
max_bin = bins[0];
|
||||||
if (count > max_count) {
|
height = bins[0];
|
||||||
max_count = count;
|
for (size_t i = 0; i < bin_count; i++) {
|
||||||
|
if (bins[i] > max_bin) {
|
||||||
|
max_bin = bins[i];
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
for (size_t bin : bins) {
|
bool flag = false;
|
||||||
if (bin < 100) cout << " ";
|
if (max_bin > 80) {
|
||||||
if (bin < 10) cout << " ";
|
flag = true;
|
||||||
cout << bin << "|";
|
}
|
||||||
|
for (size_t i = 0; i < bin_count; i++) {
|
||||||
size_t height = bin;
|
if (bins[i] < 100) cout << " ";
|
||||||
if (max_count > MAX_ASTERISK) {
|
if (bins[i] < 10) cout << " ";
|
||||||
if (max_count != bin)
|
cout << bins[i] << "|";
|
||||||
height = MAX_ASTERISK * (static_cast<float>(bin) / max_count);
|
if (flag == true) {
|
||||||
else
|
height = MAX_ASTERISK * (static_cast<double>(bins[i]) / max_bin);
|
||||||
height = MAX_ASTERISK;
|
|
||||||
}
|
}
|
||||||
|
else {
|
||||||
for (size_t i = 0; i < height; i++) cout << "*";
|
height = bins[i];
|
||||||
cout << "\n";
|
}
|
||||||
|
for (size_t j = 0; j < height; j++) {
|
||||||
|
cout << "*";
|
||||||
|
}
|
||||||
|
cout << endl;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
11
text.h
11
text.h
@@ -1,4 +1,9 @@
|
|||||||
#pragma once
|
#ifndef TEXT_H_INCLUDED
|
||||||
#include <vector>
|
#define TEXT_H_INCLUDED
|
||||||
|
|
||||||
void show_histogram_text(const std::vector<size_t>& bins);
|
#include <vector>
|
||||||
|
using namespace std;
|
||||||
|
|
||||||
|
void show_histogram_text(vector<size_t> bins, size_t bin_count);
|
||||||
|
|
||||||
|
#endif
|
||||||
|
|||||||
31
unittest.cpp
31
unittest.cpp
@@ -1,3 +1,4 @@
|
|||||||
|
#define DOCTEST_CONFIG_NO_MULTITHREADING
|
||||||
#define DOCTEST_CONFIG_IMPLEMENT_WITH_MAIN
|
#define DOCTEST_CONFIG_IMPLEMENT_WITH_MAIN
|
||||||
#include "doctest.h"
|
#include "doctest.h"
|
||||||
#include "histogram_internal.h"
|
#include "histogram_internal.h"
|
||||||
@@ -10,28 +11,28 @@ TEST_CASE("distinct positive numbers") {
|
|||||||
CHECK(max == 2);
|
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") {
|
TEST_CASE("distinct negative numbers"){
|
||||||
double min = 0;
|
double min = 0;
|
||||||
double max = 0;
|
double max = 0;
|
||||||
find_minmax({-3, -7, -1}, min, max);
|
find_minmax({-1, -2}, min, max);
|
||||||
CHECK(min == -7);
|
CHECK(min == -2);
|
||||||
CHECK(max == -1);
|
CHECK(max == -1);
|
||||||
}
|
}
|
||||||
|
TEST_CASE("vector of the same elements"){
|
||||||
TEST_CASE("single number") {
|
|
||||||
double min = 0;
|
double min = 0;
|
||||||
double max = 0;
|
double max = 0;
|
||||||
find_minmax({42}, min, max);
|
find_minmax({3,3,3}, min, max);
|
||||||
CHECK(min == 42);
|
CHECK(min == 3);
|
||||||
CHECK(max == 42);
|
CHECK(max == 3);
|
||||||
|
}
|
||||||
|
|
||||||
|
TEST_CASE("single element vector") {
|
||||||
|
double min = 0;
|
||||||
|
double max = 0;
|
||||||
|
find_minmax({7}, min, max);
|
||||||
|
CHECK(min == 7);
|
||||||
|
CHECK(max == 7);
|
||||||
}
|
}
|
||||||
|
|
||||||
TEST_CASE("empty vector") {
|
TEST_CASE("empty vector") {
|
||||||
|
|||||||
Ссылка в новой задаче
Block a user