Сравнить коммиты
Ничего общего в коммитах. 'd4e6e739047e784d0144cf0e1871329ffb9adbba' и 'e9aceb1d0cac390c8f1c86431a5d2ad61ef17062' имеют совершенно разные истории.
d4e6e73904
...
e9aceb1d0c
Разница между файлами не показана из-за своего большого размера
Загрузить разницу
Разница между файлами не показана из-за своего большого размера
Загрузить разницу
@ -1,45 +0,0 @@
|
||||
#include <iostream>
|
||||
#include <vector>
|
||||
#include <cmath>
|
||||
#include "histogram.h"
|
||||
using namespace std;
|
||||
|
||||
|
||||
void find_minmax(const vector<double>& numbers, double& mini, double& maxi) {
|
||||
|
||||
mini = numbers[0];
|
||||
maxi = numbers[0];
|
||||
for (double x : numbers) {
|
||||
if (x < mini) {
|
||||
mini = x;
|
||||
}
|
||||
else if (x > maxi) {
|
||||
maxi = x;
|
||||
}
|
||||
}
|
||||
}
|
||||
vector<size_t>
|
||||
make_histogram(const vector<double>& numbers, size_t bin_count) {
|
||||
double mini, maxi;
|
||||
find_minmax(numbers, mini, maxi);
|
||||
|
||||
vector<size_t> bins(bin_count);
|
||||
|
||||
double bin_size = (maxi - mini) / bin_count;
|
||||
for (double num : numbers) {
|
||||
bool found = false;
|
||||
for (size_t j = 0; (j < bin_count - 1) && !found; j++){
|
||||
auto lo = mini + j * bin_size;
|
||||
auto hi = mini + (j + 1) * bin_size;
|
||||
if ((lo <= num) && (num < hi)) {
|
||||
bins[j]++;
|
||||
found = true;
|
||||
}
|
||||
}
|
||||
if (!found) {
|
||||
bins[bin_count - 1]++;
|
||||
}
|
||||
}
|
||||
|
||||
return bins;
|
||||
}
|
@ -1,5 +0,0 @@
|
||||
#pragma once
|
||||
#include <vector>
|
||||
|
||||
std::vector<size_t>
|
||||
make_histogram(const std::vector<double>& numbers, size_t bin_count);
|
@ -1,5 +0,0 @@
|
||||
#pragma once
|
||||
#include <vector>
|
||||
|
||||
void
|
||||
find_minmax(const std::vector<double>& numbers, double& mini, double& maxi);
|
@ -1,42 +1,87 @@
|
||||
#include <iostream>
|
||||
#include <vector>
|
||||
#include <cmath>
|
||||
#include "histogram.h"
|
||||
#include "text.h"
|
||||
using namespace std;
|
||||
|
||||
const size_t SCREEN_WIDTH = 80;
|
||||
const size_t MAX_ASTERISK = SCREEN_WIDTH - 4;
|
||||
|
||||
struct Input {
|
||||
vector<double> numbers;
|
||||
size_t bin_count{};
|
||||
};
|
||||
int main() {
|
||||
|
||||
Input
|
||||
input_data() {
|
||||
size_t number_count;
|
||||
cerr << "Enter number count: ";
|
||||
cin >> number_count;
|
||||
|
||||
Input in;
|
||||
in.numbers.resize(number_count);
|
||||
vector<double> numbers(number_count);
|
||||
cerr << "Enter massive elements: ";
|
||||
for (size_t i = 0; i < number_count; i++) {
|
||||
cin >> in.numbers[i];
|
||||
cin >> numbers[i];
|
||||
}
|
||||
|
||||
in.bin_count; //ÇÀ×ÅÌ
|
||||
size_t bin_count;
|
||||
cerr << "Enter bin count: ";
|
||||
cin >> in.bin_count;
|
||||
return in;
|
||||
}
|
||||
cin >> bin_count;
|
||||
|
||||
vector<size_t> bins(bin_count);
|
||||
|
||||
double mini = numbers[0];
|
||||
double maxi = numbers[0];
|
||||
for (double x : numbers) {
|
||||
if (x < mini) {
|
||||
mini = x;
|
||||
}
|
||||
else if (x > maxi) {
|
||||
maxi = x;
|
||||
}
|
||||
}
|
||||
|
||||
double bin_size = (maxi - mini) / bin_count;
|
||||
for (size_t i = 0; i < number_count; i++) {
|
||||
bool found = false;
|
||||
for (size_t j = 0; (j < bin_count - 1) && !found; j++){
|
||||
auto lo = mini + j * bin_size;
|
||||
auto hi = mini + (j + 1) * bin_size;
|
||||
if ((lo <= numbers[i]) && (numbers[i] < hi)) {
|
||||
bins[j]++;
|
||||
found = true;
|
||||
}
|
||||
}
|
||||
if (!found) {
|
||||
bins[bin_count - 1]++;
|
||||
}
|
||||
}
|
||||
|
||||
int main() {
|
||||
auto in = input_data();
|
||||
auto bins = make_histogram(in.numbers, in.bin_count);
|
||||
show_histogram_text(bins, in.bin_count);
|
||||
|
||||
return 0;
|
||||
|
||||
size_t max_count = bins[0];
|
||||
for (size_t i = 0; i < bin_count; i++) {
|
||||
if (bins[i]>max_count) {
|
||||
max_count = bins[i];
|
||||
}
|
||||
}
|
||||
|
||||
vector<size_t> height(bin_count);
|
||||
for (size_t i = 0; i < bin_count; i++) {
|
||||
if (max_count > MAX_ASTERISK) {
|
||||
height[i] = round(MAX_ASTERISK * (static_cast<double>(bins[i]) / max_count));
|
||||
} else {
|
||||
height[i]=bins[i];
|
||||
}
|
||||
}
|
||||
|
||||
for (size_t i = 0; i < bin_count; i++) {
|
||||
if (bins[i] < 10) {
|
||||
cout << " ";
|
||||
} else if (bins[i] < 100) {
|
||||
cout << " ";
|
||||
}
|
||||
cout << bins[i] << "| ";
|
||||
|
||||
|
||||
for (size_t j = 0; j<height[i]; j++) {
|
||||
cout <<" * ";
|
||||
}
|
||||
cout << endl;
|
||||
}
|
||||
}
|
||||
|
||||
|
@ -1,38 +0,0 @@
|
||||
#include <iostream>
|
||||
#include <vector>
|
||||
#include <cmath>
|
||||
#include "text.h"
|
||||
using namespace std;
|
||||
|
||||
void
|
||||
show_histogram_text(const vector<size_t>& bins, size_t bin_count) {
|
||||
size_t max_count = bins[0];
|
||||
for (size_t i = 0; i < bin_count; i++) {
|
||||
if (bins[i] > max_count) {
|
||||
max_count = bins[i];
|
||||
}
|
||||
}
|
||||
|
||||
vector<size_t> height(bin_count);
|
||||
for (size_t i = 0; i < bin_count; i++) {
|
||||
if (max_count > MAX_ASTERISK) {
|
||||
height[i] = round(MAX_ASTERISK * (static_cast<double>(bins[i]) / max_count));
|
||||
} else {
|
||||
height[i] = bins[i];
|
||||
}
|
||||
}
|
||||
|
||||
for (size_t i = 0; i < bin_count; i++) {
|
||||
if (bins[i] < 10) {
|
||||
cout << " ";
|
||||
} else if (bins[i] < 100) {
|
||||
cout << " ";
|
||||
}
|
||||
cout << bins[i] << "| ";
|
||||
|
||||
for (size_t j = 0; j < height[i]; j++) {
|
||||
cout << " * ";
|
||||
}
|
||||
cout << endl;
|
||||
}
|
||||
}
|
@ -1,5 +0,0 @@
|
||||
#pragma once
|
||||
#include <vector>
|
||||
|
||||
void
|
||||
show_histogram_text(const vector<size_t>& bins, size_t bin_count);
|
@ -1,38 +0,0 @@
|
||||
<?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>
|
@ -1,60 +0,0 @@
|
||||
#define DOCTEST_CONFIG_NO_MULTITHREADING
|
||||
#define DOCTEST_CONFIG_IMPLEMENT_WITH_MAIN
|
||||
#include "doctest.h"
|
||||
#include "histogram_internal.h"
|
||||
|
||||
TEST_CASE("distinct positive numbers") {
|
||||
double mini = 0;
|
||||
double maxi = 0;
|
||||
find_minmax({1, 2}, mini, maxi);
|
||||
CHECK(mini == 1);
|
||||
CHECK(maxi == 2);
|
||||
}
|
||||
|
||||
TEST_CASE("empty vector") {
|
||||
double mini = 0;
|
||||
double maxi = 0;
|
||||
find_minmax({1, 2}, mini, maxi);
|
||||
CHECK(mini == 1);
|
||||
CHECK(maxi == 2);
|
||||
}
|
||||
|
||||
TEST_CASE("vector with one element") {
|
||||
double mini = 0;
|
||||
double maxi = 0;
|
||||
find_minmax({1}, mini, maxi);
|
||||
CHECK(mini == 1);
|
||||
CHECK(maxi == 1);
|
||||
}
|
||||
|
||||
TEST_CASE("vector with negative elements") {
|
||||
double mini = 0;
|
||||
double maxi = 0;
|
||||
find_minmax({-1, -2}, mini, maxi);
|
||||
CHECK(mini == -2);
|
||||
CHECK(maxi == -1);
|
||||
}
|
||||
|
||||
TEST_CASE("vector with equivalent elements") {
|
||||
double mini = 0;
|
||||
double maxi = 0;
|
||||
find_minmax({2, 2}, mini, maxi);
|
||||
CHECK(mini == 2);
|
||||
CHECK(maxi == 2);
|
||||
}
|
||||
|
||||
TEST_CASE("vector with 0") {
|
||||
double mini = 0;
|
||||
double maxi = 0;
|
||||
find_minmax({0, 0}, mini, maxi);
|
||||
CHECK(mini == 0);
|
||||
CHECK(maxi == 0);
|
||||
}
|
||||
|
||||
TEST_CASE("vector with fractional numbers") {
|
||||
double mini = 0;
|
||||
double maxi = 0;
|
||||
find_minmax({2.4, 2.2}, mini, maxi);
|
||||
CHECK(mini == 2.2);
|
||||
CHECK(maxi == 2.4);
|
||||
}
|
Загрузка…
Ссылка в новой задаче