добавили laba34
Этот коммит содержится в:
5
.gitignore
поставляемый
5
.gitignore
поставляемый
@@ -2,4 +2,7 @@
|
||||
/obj
|
||||
/*.depend
|
||||
/*.layout
|
||||
/unittest
|
||||
<<<<<<< HEAD
|
||||
/unittest
|
||||
=======
|
||||
>>>>>>> origin/main
|
||||
|
||||
40
cs-lab34.cbp
Обычный файл
40
cs-lab34.cbp
Обычный файл
@@ -0,0 +1,40 @@
|
||||
<?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>
|
||||
<lib_finder disable_auto="1" />
|
||||
</Extensions>
|
||||
</Project>
|
||||
</CodeBlocks_project_file>
|
||||
@@ -1,5 +1,6 @@
|
||||
#include "histogram.h"
|
||||
|
||||
<<<<<<< HEAD
|
||||
bool find_minmax(vector<double> vec, double& min, double& max) {
|
||||
|
||||
if (vec.size()== 0){
|
||||
@@ -15,16 +16,36 @@ bool find_minmax(vector<double> vec, double& min, double& max) {
|
||||
if (x > max) max = x;
|
||||
}
|
||||
return true;
|
||||
=======
|
||||
void find_minmax(vector<double> vec, double& min, double& max) {
|
||||
min = vec[0];
|
||||
max = vec[0];
|
||||
for (double x : vec) {
|
||||
if (x < min) {
|
||||
min = x;
|
||||
}
|
||||
else if (x > max)
|
||||
{
|
||||
max = x;
|
||||
}
|
||||
}
|
||||
>>>>>>> origin/main
|
||||
}
|
||||
|
||||
vector<size_t> make_histogram(size_t number, vector<double> vec) {
|
||||
vector<size_t> bins(number);
|
||||
<<<<<<< HEAD
|
||||
if (vec.empty()) return bins;
|
||||
|
||||
double mn, mx;
|
||||
find_minmax(vec, mn, mx);
|
||||
float bin_size = (mx - mn) / number;
|
||||
|
||||
=======
|
||||
double mn, mx;
|
||||
find_minmax(vec, mn, mx);
|
||||
float bin_size = (mx - mn) / number;
|
||||
>>>>>>> origin/main
|
||||
for (size_t i = 0; i < vec.size(); i++) {
|
||||
bool fl = false;
|
||||
for (size_t j = 0; (j < number - 1) && !fl; j++) {
|
||||
@@ -37,6 +58,10 @@ vector<size_t> make_histogram(size_t number, vector<double> vec) {
|
||||
}
|
||||
if (!fl) {
|
||||
bins[number - 1]++;
|
||||
<<<<<<< HEAD
|
||||
=======
|
||||
|
||||
>>>>>>> origin/main
|
||||
}
|
||||
}
|
||||
return bins;
|
||||
|
||||
@@ -1,9 +1,14 @@
|
||||
#ifndef HISTOGRAM_H_INCLUDED
|
||||
#define HISTOGRAM_H_INCLUDED
|
||||
<<<<<<< HEAD
|
||||
|
||||
#include <vector>
|
||||
#include <iostream>
|
||||
|
||||
=======
|
||||
#include <vector>
|
||||
#include <iostream>
|
||||
>>>>>>> origin/main
|
||||
using namespace std;
|
||||
vector<size_t> make_histogram(size_t number, vector<double> vec);
|
||||
|
||||
|
||||
@@ -1,6 +1,11 @@
|
||||
#ifndef HISTOGRAM_INTERNAL_H_INCLUDED
|
||||
#define HISTOGRAM_INTERNAL_H_INCLUDED
|
||||
<<<<<<< HEAD
|
||||
using namespace std;
|
||||
bool find_minmax(vector<double> vec, double& min, double& max);
|
||||
=======
|
||||
|
||||
void find_minmax(std::vector<double> vec, double& min, double& max);
|
||||
>>>>>>> origin/main
|
||||
|
||||
#endif // HISTOGRAM_INTERNAL_H_INCLUDED
|
||||
|
||||
149
main.cpp
149
main.cpp
@@ -1,36 +1,185 @@
|
||||
#include <iostream>
|
||||
#include <vector>
|
||||
<<<<<<< HEAD
|
||||
#include "histogram.h"
|
||||
#include "text.h"
|
||||
#include "svg.h"
|
||||
=======
|
||||
|
||||
using namespace std;
|
||||
>>>>>>> origin/main
|
||||
struct Input {
|
||||
vector<double> numbers;
|
||||
size_t bin_count{};
|
||||
};
|
||||
|
||||
<<<<<<< HEAD
|
||||
Input input_data() {
|
||||
Input in;
|
||||
size_t number_count;
|
||||
=======
|
||||
Input
|
||||
input_data() {
|
||||
size_t number_count, bin_count;
|
||||
double minl, maxl, bin_size;
|
||||
|
||||
>>>>>>> origin/main
|
||||
|
||||
cerr << "Enter the number of elements: ";
|
||||
cin >> number_count;
|
||||
|
||||
<<<<<<< HEAD
|
||||
in.numbers.resize(number_count);
|
||||
|
||||
=======
|
||||
Input in;
|
||||
in.numbers.resize(number_count);
|
||||
|
||||
vector<double> numbers(number_count);
|
||||
|
||||
>>>>>>> origin/main
|
||||
cerr << "\nEnter " << number_count << " elements:" << endl;
|
||||
for (size_t i = 0; i < number_count; i++) {
|
||||
cin >> in.numbers[i];
|
||||
}
|
||||
|
||||
<<<<<<< HEAD
|
||||
=======
|
||||
|
||||
>>>>>>> origin/main
|
||||
cerr << "Enter the number of bins: ";
|
||||
cin >> in.bin_count;
|
||||
|
||||
return in;
|
||||
}
|
||||
|
||||
<<<<<<< HEAD
|
||||
int main() {
|
||||
auto in = input_data();
|
||||
auto bins = make_histogram(in.bin_count, in.numbers);
|
||||
show_histogram_svg(bins);
|
||||
=======
|
||||
void
|
||||
find_minmax(const vector<double>& numbers, double& minl, double& maxl) {
|
||||
minl = numbers[0];
|
||||
maxl = numbers[0];
|
||||
|
||||
for (double x : numbers) {
|
||||
if (x < minl) {
|
||||
minl = x;
|
||||
} else {
|
||||
if (x > maxl) {
|
||||
maxl = x;
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
vector<size_t>
|
||||
make_histogram(const vector<double>& numbers, size_t& bin_count) {
|
||||
double minn, maxn;
|
||||
find_minmax(numbers, minn, maxn);
|
||||
|
||||
double bin_size = (maxn - minn) / bin_count;
|
||||
|
||||
vector<size_t> bins(bin_count, 0);
|
||||
|
||||
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 = minn + j * bin_size;
|
||||
auto hi = minn + (j + 1) * bin_size;
|
||||
if ((numbers[i] >= lo) && (numbers[i] <= hi)) {
|
||||
bins[j]++;
|
||||
found = true;
|
||||
}
|
||||
}
|
||||
if (!found) {
|
||||
bins[bin_count - 1]++;
|
||||
}
|
||||
}
|
||||
return bins;
|
||||
}
|
||||
|
||||
void
|
||||
show_histogram_text(const vector<size_t> bins, size_t bin_count) {
|
||||
size_t max_count = bins[0];
|
||||
for (size_t i = 1; i < bin_count; i++) {
|
||||
if (bins[i] > max_count) {
|
||||
max_count = bins[i];
|
||||
}
|
||||
}
|
||||
|
||||
const size_t SCREEN_WIDTH = 80;
|
||||
const size_t MAX_ASTERISK = SCREEN_WIDTH - 3 - 1;
|
||||
|
||||
|
||||
short space_count;
|
||||
|
||||
cerr << "\nHistogram:" << endl;
|
||||
|
||||
if (max_count <= MAX_ASTERISK) {
|
||||
for (size_t i = 0; i < bin_count; i++) {
|
||||
if (bins[i] < 10) {
|
||||
space_count = 2;
|
||||
} else if (bins[i] >= 10 && bins[i] < 100) {
|
||||
space_count = 1;
|
||||
} else if (bins[i] >= 100 && bins[i] < 1000) {
|
||||
space_count = 0;
|
||||
}
|
||||
|
||||
|
||||
|
||||
for (size_t k = 0; k < space_count; k++) {
|
||||
cout << " ";
|
||||
}
|
||||
|
||||
cout << bins[i];
|
||||
|
||||
cout << "|";
|
||||
|
||||
|
||||
for (size_t j = 0; j < bins[i]; j++) {
|
||||
cout << "*";
|
||||
}
|
||||
|
||||
cout << "\n";
|
||||
}
|
||||
} else {
|
||||
for (size_t i = 0; i < bin_count; i++) {
|
||||
if (bins[i] < 10) {
|
||||
space_count = 2;
|
||||
} else if (bins[i] >= 10 && bins[i] < 100) {
|
||||
space_count = 1;
|
||||
} else if (bins[i] >= 100 && bins[i] < 1000) {
|
||||
space_count = 0;
|
||||
}
|
||||
|
||||
|
||||
for (size_t k = 0; k < space_count; k++) {
|
||||
cout << " ";
|
||||
}
|
||||
|
||||
cout << bins[i];
|
||||
|
||||
cout << "|";
|
||||
|
||||
size_t height = static_cast<size_t>(MAX_ASTERISK * (static_cast<double>(bins[i]) / max_count));
|
||||
for (size_t j = 0; j < height; j++) {
|
||||
cout << "*";
|
||||
}
|
||||
|
||||
cout << "\n";
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
int main() {
|
||||
|
||||
auto in = input_data();
|
||||
auto bins = make_histogram(in.numbers, in.bin_count);
|
||||
show_histogram_text(bins, in.bin_count);
|
||||
|
||||
>>>>>>> origin/main
|
||||
return 0;
|
||||
}
|
||||
|
||||
83
text.cpp
83
text.cpp
@@ -1,4 +1,5 @@
|
||||
#include "text.h"
|
||||
<<<<<<< HEAD
|
||||
#include <iostream>
|
||||
|
||||
void show_histogram(const std::vector<size_t>& bins) {
|
||||
@@ -44,5 +45,87 @@ void show_histogram(const std::vector<size_t>& bins) {
|
||||
}
|
||||
}
|
||||
std::cout << std::endl;
|
||||
=======
|
||||
|
||||
void show_histogram(std::vector<size_t> bins) {
|
||||
bool gigant = false;
|
||||
auto spaces = 0;
|
||||
size_t mx_count = 0;
|
||||
for (auto x : bins) {
|
||||
if (x > 76) {
|
||||
gigant = true;
|
||||
}
|
||||
if (x > mx_count) {
|
||||
mx_count = x;
|
||||
}
|
||||
auto len = 0;
|
||||
while (x > 0) {
|
||||
x /= 10;
|
||||
len++;
|
||||
}
|
||||
if (len > spaces) {
|
||||
spaces = len;
|
||||
}
|
||||
|
||||
}
|
||||
if (spaces == 1) {
|
||||
for (size_t i = 0; i < bins.size(); i++) {
|
||||
std::cout << " " << bins[i] << "|";
|
||||
if (gigant) {
|
||||
if (bins[i] == mx_count) {
|
||||
for (size_t j = 0; j < 76; j++) {
|
||||
std::cout << "*";
|
||||
}
|
||||
}
|
||||
else
|
||||
{
|
||||
for (size_t j = 0; j < 76 * static_cast<double>(bins[i]) / mx_count; j++) {
|
||||
std::cout << "*";
|
||||
}
|
||||
}
|
||||
}
|
||||
else
|
||||
{
|
||||
for (size_t j = 0; j < bins[i]; j++) {
|
||||
std::cout << "*";
|
||||
}
|
||||
std::cout << std::endl;
|
||||
}
|
||||
}
|
||||
}
|
||||
else
|
||||
{
|
||||
for (size_t i = 0; i < bins.size(); i++) {
|
||||
int len = 1;
|
||||
int k = bins[i];
|
||||
for (; k /= 10; ++len);
|
||||
while (len < spaces) {
|
||||
std::cout << " ";
|
||||
len++;
|
||||
}
|
||||
std::cout << bins[i];
|
||||
std::cout << "|";
|
||||
if (gigant) {
|
||||
if (bins[i] == mx_count) {
|
||||
for (size_t j = 0; j < 76; j++) {
|
||||
std::cout << "*";
|
||||
}
|
||||
}
|
||||
else
|
||||
{
|
||||
for (size_t j = 0; j < (76 * static_cast<double>(bins[i]) / mx_count - 1); j++) {
|
||||
std::cout << "*";
|
||||
}
|
||||
}
|
||||
}
|
||||
else
|
||||
{
|
||||
for (size_t j = 0; j < bins[i]; j++) {
|
||||
std::cout << "*";
|
||||
}
|
||||
}
|
||||
std::cout << std::endl;
|
||||
}
|
||||
>>>>>>> origin/main
|
||||
}
|
||||
}
|
||||
|
||||
6
text.h
6
text.h
@@ -1,8 +1,14 @@
|
||||
#ifndef TEXT_H_INCLUDED
|
||||
#define TEXT_H_INCLUDED
|
||||
|
||||
<<<<<<< HEAD
|
||||
#include <vector>
|
||||
using namespace std;
|
||||
void show_histogram(const std::vector<size_t>& bins);
|
||||
=======
|
||||
#include <iostream>
|
||||
#include <vector>
|
||||
void show_histogram(std::vector<size_t> bins);
|
||||
>>>>>>> origin/main
|
||||
|
||||
#endif // TEXT_H_INCLUDED
|
||||
|
||||
7106
unittest/doctest.h
Обычный файл
7106
unittest/doctest.h
Обычный файл
Разница между файлами не показана из-за своего большого размера
Загрузить разницу
@@ -31,6 +31,7 @@
|
||||
<Compiler>
|
||||
<Add option="-Wall" />
|
||||
</Compiler>
|
||||
<<<<<<< HEAD
|
||||
<Unit filename="../doctest.h">
|
||||
<Option target="<{~None~}>" />
|
||||
</Unit>
|
||||
@@ -41,6 +42,8 @@
|
||||
<Option target="<{~None~}>" />
|
||||
</Unit>
|
||||
<Unit filename="../unittest.cpp" />
|
||||
=======
|
||||
>>>>>>> origin/main
|
||||
<Extensions>
|
||||
<lib_finder disable_auto="1" />
|
||||
</Extensions>
|
||||
|
||||
56
unittest/unittest.cpp
Обычный файл
56
unittest/unittest.cpp
Обычный файл
@@ -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);
|
||||
}
|
||||
|
||||
@@ -1,4 +1,5 @@
|
||||
# depslib dependency file v1.0
|
||||
<<<<<<< HEAD
|
||||
1748243702 source:c:\users\home\desktop\lab34\laba01\histogram.cpp
|
||||
"histogram.h"
|
||||
|
||||
@@ -200,3 +201,12 @@
|
||||
<string>
|
||||
<math.h>
|
||||
|
||||
=======
|
||||
1748415920 source:c:\users\home\desktop\laba34\histogram.cpp
|
||||
"histogram.h"
|
||||
|
||||
1748415920 c:\users\home\desktop\laba34\histogram.h
|
||||
<vector>
|
||||
<iostream>
|
||||
|
||||
>>>>>>> origin/main
|
||||
|
||||
Ссылка в новой задаче
Block a user