Сommit
94468310b5
@ -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,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
|
||||
|
@ -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;
|
||||
}
|
||||
|
@ -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
|
||||
|
Разница между файлами не показана из-за своего большого размера
Загрузить разницу
@ -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);
|
||||
}
|
||||
|
Загрузка…
Ссылка в новой задаче