main
KalenskovaDA 2 недель назад
Родитель 454cc7cfe9
Сommit 133ecfbbb6

2
.gitignore поставляемый

@ -0,0 +1,2 @@
/bin
/obj

@ -0,0 +1,8 @@
-----BEGIN OPENSSH PRIVATE KEY-----
b3BlbnNzaC1rZXktdjEAAAAACmFlczI1Ni1jdHIAAAAGYmNyeXB0AAAAGAAAABAqjR8VKL
iXqwsebTTkYzLtAAAAGAAAAAEAAAAzAAAAC3NzaC1lZDI1NTE5AAAAIAMVcWMSwkHQ796Q
v12uM1/L9QSqCC1guyCtw8nZgqYaAAAAoAX6y1g+sl014TmIQ7zX/gv2ngRTEKBcujJq8O
UqkMywAQC10aGheZuBego39vP3S84Tf+kKNzxIUWu+kRpbgi4x2bIc5/kYbYh76DGK+o9z
zCGU6ju3j1fNVHknGYkR0aw2SSu9/Q8GjU+xMd5Rdqj567rbHY/w7Qt8ZYImEKVvYoO/6G
BenNIJk2c3S/lY26lh51GG9DF9PKMrnWNhZIY=
-----END OPENSSH PRIVATE KEY-----

@ -0,0 +1 @@
ssh-ed25519 AAAAC3NzaC1lZDI1NTE5AAAAIAMVcWMSwkHQ796Qv12uM1/L9QSqCC1guyCtw8nZgqYa aleks@DESKTOP-3A8L66V

@ -0,0 +1,42 @@
#include "histogram.h"
#include "histogram_internal.h"
#include <vector>
using std::vector;
void find_minmax(const std::vector<double>& numbers, double& min, double& max) {
min = numbers[0];
max = numbers[0];
for (double number : numbers) {
if (number < min) {
min = number;
}
if (number > max) {
max = number;
}
}
}
std::vector<size_t> make_histogram(const std::vector<double>& numbers, size_t bin_count) {
std::vector<size_t> bins(bin_count, 0);
double min, max;
find_minmax(numbers, min, max);
double bin_size = (max - min) / bin_count;
for (double number : numbers) {
bool found = false;
for (size_t j = 0; (j < bin_count - 1) && !found; j++) {
double lo = min + j * bin_size;
double hi = min + (j + 1) * bin_size;
if ((lo <= number) && (number < hi)) {
bins[j]++;
found = true;
}
}
if (!found) {
bins[bin_count - 1]++;
}
}
return bins;
}

@ -0,0 +1,10 @@
#ifndef HISTOGRAM_H_INCLUDED
#define HISTOGRAM_H_INCLUDED
#include <vector>
using std::vector;
std::vector<size_t>
make_histogram(const std::vector<double>& numbers, size_t bin_count);
#endif // HISTOGRAM_H_INCLUDED

@ -0,0 +1,8 @@
#ifndef HISTOGRAM_INTERNAL_H_INCLUDED
#define HISTOGRAM_INTERNAL_H_INCLUDED
#include <vector>
using std::vector;
void find_minmax(const std::vector<double>& numbers, double& min, double& max);
#endif // HISTOGRAM_INTERNAL_H_INCLUDED

@ -32,6 +32,11 @@
<Add option="-Wall" /> <Add option="-Wall" />
<Add option="-fexceptions" /> <Add option="-fexceptions" />
</Compiler> </Compiler>
<Unit filename=".gitignore" />
<Unit filename="histogram.cpp" />
<Unit filename="histogram.h">
<Option target="&lt;{~None~}&gt;" />
</Unit>
<Unit filename="main.cpp" /> <Unit filename="main.cpp" />
<Extensions> <Extensions>
<lib_finder disable_auto="1" /> <lib_finder disable_auto="1" />

@ -0,0 +1,80 @@
# depslib dependency file v1.0
1748812442 source:c:\users\aleks\onedrive\Ðàáî÷èé ñòîë\laba1\main.cpp
<iostream>
<vector>
"histogram.h"
"text.h"
"svg.h"
1748386910 c:\users\aleks\onedrive\Ðàáî÷èé ñòîë\laba1\histogram.h
<vector>
1748730472 c:\users\aleks\onedrive\Ðàáî÷èé ñòîë\laba1\text.h
<vector>
1748808306 source:c:\users\aleks\onedrive\Ðàáî÷èé ñòîë\laba1\text.cpp
"text.h"
<iostream>
<vector>
1748392747 c:\users\aleks\onedrive\Ðàáî÷èé ñòîë\laba1\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>
1748392232 c:\users\aleks\onedrive\Ðàáî÷èé ñòîë\laba1\histogram_internal.h
<vector>
1748811805 source:c:\users\aleks\onedrive\Ðàáî÷èé ñòîë\laba1\svg.cpp
"svg.h"
<iostream>
1748810901 c:\users\aleks\onedrive\Ðàáî÷èé ñòîë\laba1\svg.h
<vector>
<cstddef>
1748808114 source:c:\users\aleks\onedrive\Ðàáî÷èé ñòîë\laba1\histogram.cpp
"histogram.h"
"histogram_internal.h"
<vector>

@ -0,0 +1,20 @@
<?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="893" topLine="30" />
</Cursor>
</File>
<File name="histogram.cpp" open="1" top="0" tabpos="3" split="0" active="1" splitpos="0" zoom_1="0" zoom_2="0">
<Cursor>
<Cursor1 position="193" topLine="0" />
</Cursor>
</File>
<File name=".gitignore" open="1" top="0" tabpos="2" split="0" active="1" splitpos="0" zoom_1="0" zoom_2="0">
<Cursor>
<Cursor1 position="10" topLine="0" />
</Cursor>
</File>
</CodeBlocks_layout_file>

@ -0,0 +1,38 @@
<?xml version="1.0" encoding="UTF-8" standalone="yes" ?>
<CodeBlocks_project_file>
<FileVersion major="1" minor="6" />
<Project>
<Option title="laba1" />
<Option pch_mode="2" />
<Option compiler="gcc" />
<Build>
<Target title="Debug">
<Option output="bin/Debug/laba1" 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/laba1" 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>

@ -0,0 +1,25 @@
# depslib dependency file v1.0
1748387373 source:c:\users\aleks\onedrive\Ðàáî÷èé ñòîë\laba1\histogram.cpp
"histogram.h"
<vector>
1748386910 c:\users\aleks\onedrive\Ðàáî÷èé ñòîë\laba1\histogram.h
<vector>
1748388937 source:c:\users\aleks\onedrive\Ðàáî÷èé ñòîë\laba1\main.cpp
<iostream>
<vector>
"histogram.h"
"text.h"
1748388603 c:\users\aleks\onedrive\Ðàáî÷èé ñòîë\laba1\text.h
<vector>
1584205420 source:c:\users\aleks\onedrive\Ðàáî÷èé ñòîë\laba1\laba1\main.cpp
<iostream>
1748391445 source:c:\users\aleks\onedrive\Ðàáî÷èé ñòîë\laba1\text.cpp
"text.h"
<iostream>
<vector>

@ -0,0 +1,5 @@
<?xml version="1.0" encoding="UTF-8" standalone="yes" ?>
<CodeBlocks_layout_file>
<FileVersion major="1" minor="0" />
<ActiveTarget name="Debug" />
</CodeBlocks_layout_file>

Двоичные данные
laba1/obj/Debug/histogram.o

Двоичный файл не отображается.

Двоичные данные
laba1/obj/Debug/main.o

Двоичный файл не отображается.

Двоичные данные
laba1/obj/Debug/text.o

Двоичный файл не отображается.

@ -1,23 +1,24 @@
#include <iostream> #include <iostream>
#include <vector> #include <vector>
#include "histogram.h"
#include "text.h"
#include "svg.h"
using namespace std; using namespace std;
const size_t SCREEN_WIDTH = 80;
const size_t MAX_ASTERISK = SCREEN_WIDTH - 3 - 1;
struct Input { struct Input {
vector<double> numbers; std::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: "; 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: "; cerr << "Enter numbers: ";
@ -32,83 +33,18 @@ Input input_data() {
} }
// Ïîèñê ìèíèìóìà è ìàêñèìóìà // Ïîèñê ìèíèìóìà è ìàêñèìóìà
void find_minmax(const vector<double>& numbers, double& min, double& max) {
min = numbers[0];
max = numbers[0];
for (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) {
vector<size_t> bins(bin_count, 0);
double min, max;
find_minmax(numbers, min, max);
double bin_size = (max - min) / bin_count;
for (double number : numbers) {
bool found = false;
for (size_t j = 0; (j < bin_count - 1) && !found; j++) {
double lo = min + j * bin_size;
double hi = min + (j + 1) * bin_size;
if ((lo <= number) && (number < hi)) {
bins[j]++;
found = true;
}
}
if (!found) {
bins[bin_count - 1]++;
}
}
return bins;
}
// Âûâîä ãèñòîãðàììû // Âûâîä ãèñòîãðàììû
void show_histogram_text(const vector<size_t>& bins) {
size_t max_bin_count = 0;
for (size_t bin : bins) {
if (bin > max_bin_count) {
max_bin_count = bin;
}
}
if (max_bin_count <= MAX_ASTERISK) {
for (size_t bin : bins) {
if (bin < 10) cout << " ";
cout << " " << bin << "|";
for (size_t j = 0; j < bin; j++) {
cout << "*";
}
cout << endl;
}
} else {
for (size_t bin : bins) {
size_t height = static_cast<size_t>(MAX_ASTERISK * (static_cast<double>(bin) / max_bin_count));
if (bin < 100) cout << " ";
if (bin < 10) cout << " ";
cout << bin << "|";
for (size_t j = 0; j < height; j++) {
cout << "*";
}
cout << endl;
}
}
}
// Îñíîâíàÿ ôóíêöèÿ // Îñíîâíàÿ ôóíêöèÿ
int main() { int main() {
Input in = input_data(); auto in = input_data();
vector<size_t> 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;
} }

Двоичные данные
main.o

Двоичный файл не отображается.

@ -0,0 +1,68 @@
#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 << "' ";
cout << "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, string text)
{
cout << "<text x='" << left << "' y='" << baseline << "'>" << text << "</text>";
}
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<<"' height='"<<height<<"' stroke='"<<stroke<<"' fill='"<<fill<<"' />";
}
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 YELLOW = "yellow";
const auto PURPLE = "purple";
const auto MAX_WIDTH = IMAGE_WIDTH-TEXT_WIDTH;
svg_begin(IMAGE_WIDTH,IMAGE_HEIGHT);
double top = 0;
double max_count = bins[0];
for (size_t i = 0; i < bins.size(); i++)
{
if (max_count<bins[i])
{
max_count=bins[i];
}
}
for (size_t bin : bins)
{
double bin_width = (MAX_WIDTH)*(bin/max_count);
svg_text(TEXT_LEFT, top + TEXT_BASELINE, to_string(bin));
svg_rect(TEXT_WIDTH, top, bin_width, BIN_HEIGHT, YELLOW, PURPLE);
top += BIN_HEIGHT;
}
svg_end();
}

@ -0,0 +1,9 @@
#ifndef SVG_H_INCLUDED
#define SVG_H_INCLUDED
#include <vector>
#include <cstddef>
void show_histogram_svg(const std::vector<size_t>& bins);
#endif

@ -0,0 +1,37 @@
#include "text.h"
#include <iostream>
#include <vector>
using std::vector;
const size_t SCREEN_WIDTH = 80;
const size_t MAX_ASTERISK = SCREEN_WIDTH - 3 - 1;
// Âûâîä ãèñòîãðàììû
void show_histogram_text(const std::vector<size_t>& bins) {
size_t max_bin_count = 0;
for (size_t bin : bins) {
if (bin > max_bin_count) {
max_bin_count = bin;
}
}
if (max_bin_count <= MAX_ASTERISK) {
for (size_t bin : bins) {
if (bin < 10) std::cout << " ";
std::cout << " " << bin << "|";
for (size_t j = 0; j < bin; j++) {
std::cout << "*";
}
std::cout << std::endl;
}
} else {
for (size_t bin : bins) {
size_t height = static_cast<size_t>(MAX_ASTERISK * (static_cast<double>(bin) / max_bin_count));
if (bin < 100) std::cout << " ";
if (bin < 10) std::cout << " ";
std::cout << bin << "|";
for (size_t j = 0; j < height; j++) {
std::cout << "*";
}
std::cout << std::endl;
}
}
}

@ -0,0 +1,9 @@
#ifndef TEXT_H_INCLUDED
#define TEXT_H_INCLUDED
#include <vector>
using std::vector;
void show_histogram_text(const std::vector<size_t>& bins);
#endif // TEXT_H_INCLUDED

@ -0,0 +1,38 @@
<?xml version="1.0" encoding="UTF-8" standalone="yes" ?>
<CodeBlocks_project_file>
<FileVersion major="1" minor="6" />
<Project>
<Option title="unittest" />
<Option pch_mode="2" />
<Option compiler="gcc" />
<Build>
<Target title="Debug">
<Option output="bin/Debug/unittest" 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/unittest" 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" />
</Compiler>
<Extensions>
<lib_finder disable_auto="1" />
</Extensions>
</Project>
</CodeBlocks_project_file>

@ -0,0 +1,56 @@
#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("distinct negative numbers"){
double min = 0;
double max = 0;
find_minmax({-1, -2}, min, max);
CHECK(min == -2);
CHECK(max == -1);
}
TEST_CASE("vector of the same elements"){
double min = 0;
double max = 0;
find_minmax({3,3,3}, min, max);
CHECK(min == 3);
CHECK(max == 3);
}
TEST_CASE("vector of one elements"){
double min = 0;
double max = 0;
find_minmax({3}, min, max);
CHECK(min == max);
}
TEST_CASE("mixed positive and negative numbers") {
double min = 0;
double max = 0;
find_minmax({-1, 2, 0, -3, 5}, min, max);
CHECK(min == -3);
CHECK(max == 5);
}
TEST_CASE("vector with all negative numbers") {
double min = 0;
double max = 0;
find_minmax({-5, -10, -2}, min, max);
CHECK(min == -10);
CHECK(max == -2);
}
TEST_CASE("vector with zero") {
double min = 0;
double max = 0;
find_minmax({0, -1, 1}, min, max);
CHECK(min == -1);
CHECK(max == 1);
}

@ -0,0 +1,68 @@
# depslib dependency file v1.0
1748392121 source:c:\users\aleks\onedrive\Ðàáî÷èé ñòîë\laba1\histogram.cpp
"histogram.h"
<vector>
1748386910 c:\users\aleks\onedrive\Ðàáî÷èé ñòîë\laba1\histogram.h
<vector>
1748729850 source:c:\users\aleks\onedrive\Ðàáî÷èé ñòîë\laba1\unittest.cpp
"doctest.h"
"histogram_internal.h"
1748392747 c:\users\aleks\onedrive\Ðàáî÷èé ñòîë\laba1\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>
1748392232 c:\users\aleks\onedrive\Ðàáî÷èé ñòîë\laba1\histogram_internal.h
<vector>
1748802937 source:c:\users\aleks\onedrive\Ðàáî÷èé ñòîë\laba1\svg.cpp
"svg.h"
<iostream>
1748730522 c:\users\aleks\onedrive\Ðàáî÷èé ñòîë\laba1\svg.h
<vector>
<cstddef>

@ -0,0 +1,5 @@
<?xml version="1.0" encoding="UTF-8" standalone="yes" ?>
<CodeBlocks_layout_file>
<FileVersion major="1" minor="0" />
<ActiveTarget name="Debug" />
</CodeBlocks_layout_file>
Загрузка…
Отмена
Сохранить