Сравнить коммиты
2 Коммитов
0d3fb9ea6a
...
6eb46fd053
Автор | SHA1 | Дата |
---|---|---|
|
6eb46fd053 | 4 дней назад |
|
e93efef297 | 4 дней назад |
@ -1 +1 @@
|
||||
void find_minmax(std::vector<double> vec, double& min, double& max);
|
||||
bool find_minmax(const std::vector<double> vec, double& min, double& max);
|
||||
|
@ -1,83 +1,44 @@
|
||||
#include <iostream>
|
||||
#include <vector>
|
||||
#include "text.h"
|
||||
|
||||
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;
|
||||
}
|
||||
using namespace std;
|
||||
|
||||
}
|
||||
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
|
||||
void show_histogram_text(vector<size_t> bins, size_t bin_count){
|
||||
const size_t SCREEN_WIDTH = 80;
|
||||
const size_t MAX_ASTERISK = SCREEN_WIDTH - 3 - 1;
|
||||
|
||||
size_t max_bin = bins[0];
|
||||
for(size_t i = 0; i < bin_count; i++)
|
||||
{
|
||||
for (size_t j = 0; j < 76 * static_cast<double>(bins[i]) / mx_count; j++) {
|
||||
std::cout << "*";
|
||||
}
|
||||
}
|
||||
}
|
||||
else
|
||||
if(bins[i] > max_bin)
|
||||
{
|
||||
for (size_t j = 0; j < bins[i]; j++) {
|
||||
std::cout << "*";
|
||||
}
|
||||
std::cout << std::endl;
|
||||
max_bin = bins[i];
|
||||
}
|
||||
}
|
||||
}
|
||||
else
|
||||
|
||||
for (size_t bin: bins)
|
||||
{
|
||||
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
|
||||
size_t height = bin;
|
||||
|
||||
if (max_bin > MAX_ASTERISK)
|
||||
{
|
||||
for (size_t j = 0; j < (76 * static_cast<double>(bins[i]) / mx_count - 1); j++) {
|
||||
std::cout << "*";
|
||||
height = MAX_ASTERISK * (static_cast<double>(bin) / max_bin);
|
||||
}
|
||||
}
|
||||
}
|
||||
else
|
||||
|
||||
if (bin < 100)
|
||||
{
|
||||
for (size_t j = 0; j < bins[i]; j++) {
|
||||
std::cout << "*";
|
||||
cout << ' ';
|
||||
}
|
||||
if (bin < 10)
|
||||
{
|
||||
cout << ' ';
|
||||
}
|
||||
std::cout << std::endl;
|
||||
cout << bin << "|";
|
||||
for(size_t i = 0; i < height; i++)
|
||||
{
|
||||
cout << "*";
|
||||
}
|
||||
cout << endl;
|
||||
}
|
||||
}
|
||||
|
@ -1,3 +1,3 @@
|
||||
#include <iostream>
|
||||
#include <vector>
|
||||
void show_histogram(std::vector<size_t> bins);
|
||||
void show_histogram_text(std::vector<size_t> bins);
|
||||
|
@ -0,0 +1,37 @@
|
||||
#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("empty vector"){
|
||||
double min = 0;
|
||||
double max = 0;
|
||||
CHECK(!find_minmax({}, min, max));
|
||||
}
|
||||
TEST_CASE("vector of one elements"){
|
||||
double min = 0;
|
||||
double max = 0;
|
||||
find_minmax({3}, min, max);
|
||||
CHECK(min == max);
|
||||
}
|
Загрузка…
Ссылка в новой задаче