Сравнить коммиты

...

2 Коммитов

Автор SHA1 Сообщение Дата
ShirokovIV 6eb46fd053 unittest
4 дней назад
ShirokovIV e93efef297 zhachita
4 дней назад

@ -32,7 +32,14 @@
<Add option="-Wall" />
<Add option="-fexceptions" />
</Compiler>
<Unit filename="histogram.cpp" />
<Unit filename="histogram.h" />
<Unit filename="histogram_internal.h" />
<Unit filename="main.cpp" />
<Unit filename="svg.cpp" />
<Unit filename="svg.h" />
<Unit filename="text.cpp" />
<Unit filename="text.h" />
<Extensions>
<lib_finder disable_auto="1" />
</Extensions>

@ -1,5 +1,9 @@
#include "histogram.h"
void find_minmax(vector<double> vec, double& min, double& max) {
#include "histogram_internal.h"
bool find_minmax(vector<double> vec, double& min, double& max) {
if(vec.size() == 0){
return false;
}
min = vec[0];
max = vec[0];
for (double x : vec) {
@ -11,6 +15,7 @@ void find_minmax(vector<double> vec, double& min, double& max) {
max = x;
}
}
return true;
}
vector<size_t> make_histogram(size_t number, vector<double> vec) {
vector<size_t> bins(number);

@ -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,5 +1,6 @@
#include "histogram.h"
#include "text.h"
#include "svg.h"
struct Input {
vector<double> vec;
size_t korz{};
@ -20,5 +21,5 @@ Input input_data() {
int main() {
auto in = input_data();
auto bins = make_histogram(in.korz, in.vec);
show_histogram(bins);
show_histogram_svg(bins);
}

@ -45,7 +45,7 @@ show_histogram_svg(const vector<size_t>& bins)
const auto BIN_HEIGHT = 30;
const auto BLOCK_WIDTH = 10;
const auto BLACK = "black";
const auto RED = "red";
const auto MAX_WIDTH = IMAGE_WIDTH-TEXT_WIDTH;
@ -63,9 +63,10 @@ show_histogram_svg(const vector<size_t>& bins)
for (size_t bin : bins)
{
const auto RED = "#" + std::to_string(int(ceil(10 - (bin * 9) / max_count))) + std::to_string(int(ceil(10 - (bin * 9) / max_count))) + std::to_string(int(ceil(10 - (bin * 9) / max_count)));
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, BLACK, RED);
svg_rect(TEXT_WIDTH, top, bin_width, BIN_HEIGHT, BLACK,RED);
top += BIN_HEIGHT;
}

@ -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);
}
Загрузка…
Отмена
Сохранить