code вариант 4
Этот коммит содержится в:
16
project/percent.cpp
Обычный файл
16
project/percent.cpp
Обычный файл
@@ -0,0 +1,16 @@
|
|||||||
|
#include "percent.h"
|
||||||
|
|
||||||
|
#include <vector>
|
||||||
|
using namespace std;
|
||||||
|
|
||||||
|
void
|
||||||
|
percent(const vector<size_t>& bins, vector<size_t>&bins_percent){
|
||||||
|
size_t number_count = 0;
|
||||||
|
size_t bin_count = bins.size();
|
||||||
|
for (size_t bin : bins){
|
||||||
|
number_count=number_count+bin;
|
||||||
|
}
|
||||||
|
for (int i=0; i<bin_count; i++){
|
||||||
|
bins_percent.push_back(bins[i]*100/number_count);
|
||||||
|
}
|
||||||
|
}
|
||||||
10
project/percent.h
Обычный файл
10
project/percent.h
Обычный файл
@@ -0,0 +1,10 @@
|
|||||||
|
#ifndef PERCENT_H_INCLUDED
|
||||||
|
#define PERCENT_H_INCLUDED
|
||||||
|
|
||||||
|
#include <vector>
|
||||||
|
using namespace std;
|
||||||
|
|
||||||
|
void
|
||||||
|
percent(const vector<size_t>& bins, vector<size_t>&bins_percent);
|
||||||
|
|
||||||
|
#endif // PERCENT_H_INCLUDED
|
||||||
@@ -1,4 +1,5 @@
|
|||||||
#include "svg.h"
|
#include "svg.h"
|
||||||
|
#include "percent.h"
|
||||||
|
|
||||||
void
|
void
|
||||||
show_histogram_svg(const vector<size_t>& bins) {
|
show_histogram_svg(const vector<size_t>& bins) {
|
||||||
@@ -10,8 +11,13 @@ show_histogram_svg(const vector<size_t>& bins) {
|
|||||||
const auto BIN_HEIGHT = 30;
|
const auto BIN_HEIGHT = 30;
|
||||||
const auto BLOCK_WIDTH = 10;
|
const auto BLOCK_WIDTH = 10;
|
||||||
svg_begin(IMAGE_WIDTH, IMAGE_HEIGHT);
|
svg_begin(IMAGE_WIDTH, IMAGE_HEIGHT);
|
||||||
|
|
||||||
|
|
||||||
double top = 0;
|
double top = 0;
|
||||||
size_t longest;
|
size_t longest;
|
||||||
|
vector<size_t>bins_percent{};
|
||||||
|
|
||||||
|
|
||||||
if (!bins.empty()) {
|
if (!bins.empty()) {
|
||||||
longest = bins[0];
|
longest = bins[0];
|
||||||
} else {
|
} else {
|
||||||
@@ -26,7 +32,7 @@ show_histogram_svg(const vector<size_t>& bins) {
|
|||||||
const double bin_width = BLOCK_WIDTH * bin;
|
const double bin_width = BLOCK_WIDTH * bin;
|
||||||
if (bin == longest){
|
if (bin == longest){
|
||||||
svg_text(TEXT_LEFT, top + TEXT_BASELINE, to_string(bin));
|
svg_text(TEXT_LEFT, top + TEXT_BASELINE, to_string(bin));
|
||||||
svg_rect(TEXT_WIDTH, top, IMAGE_WIDTH - TEXT_WIDTH, BIN_HEIGHT, "#000000", "#ff00a2");
|
svg_rect(TEXT_WIDTH, top, IMAGE_WIDTH - 2*TEXT_WIDTH, BIN_HEIGHT, "#000000", "#ff00a2");
|
||||||
top += BIN_HEIGHT;
|
top += BIN_HEIGHT;
|
||||||
} else{
|
} else{
|
||||||
svg_text(TEXT_LEFT, top + TEXT_BASELINE, to_string(bin));
|
svg_text(TEXT_LEFT, top + TEXT_BASELINE, to_string(bin));
|
||||||
@@ -34,5 +40,11 @@ show_histogram_svg(const vector<size_t>& bins) {
|
|||||||
top += BIN_HEIGHT;
|
top += BIN_HEIGHT;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
top = 0;
|
||||||
|
percent(bins, bins_percent);
|
||||||
|
for (size_t bin_percent : bins_percent){
|
||||||
|
svg_text(IMAGE_WIDTH - TEXT_WIDTH + TEXT_LEFT, top + TEXT_BASELINE, to_string(bin_percent)+"%");
|
||||||
|
top += BIN_HEIGHT;
|
||||||
|
}
|
||||||
svg_end();
|
svg_end();
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -2,8 +2,9 @@
|
|||||||
#define DOCTEST_CONFIG_IMPLEMENT_WITH_MAIN
|
#define DOCTEST_CONFIG_IMPLEMENT_WITH_MAIN
|
||||||
#include "doctest.h"
|
#include "doctest.h"
|
||||||
#include "histogram_internal.h"
|
#include "histogram_internal.h"
|
||||||
|
#include "percent.h"
|
||||||
|
|
||||||
TEST_CASE("distinct positive numbers") {
|
TEST_CASE("distinct empty") {
|
||||||
double min = 0;
|
double min = 0;
|
||||||
double max = 0;
|
double max = 0;
|
||||||
find_minmax({}, min, max);
|
find_minmax({}, min, max);
|
||||||
@@ -17,24 +18,30 @@ TEST_CASE("distinct positive numbers") {
|
|||||||
CHECK(min == 1);
|
CHECK(min == 1);
|
||||||
CHECK(max == 2);
|
CHECK(max == 2);
|
||||||
}
|
}
|
||||||
TEST_CASE("distinct positive numbers") {
|
TEST_CASE("distinct single") {
|
||||||
double min = 0;
|
double min = 0;
|
||||||
double max = 0;
|
double max = 0;
|
||||||
find_minmax({1}, min, max);
|
find_minmax({1}, min, max);
|
||||||
CHECK(min == 1);
|
CHECK(min == 1);
|
||||||
CHECK(max == 1);
|
CHECK(max == 1);
|
||||||
}
|
}
|
||||||
TEST_CASE("distinct positive numbers") {
|
TEST_CASE("distinct same") {
|
||||||
double min = 0;
|
double min = 0;
|
||||||
double max = 0;
|
double max = 0;
|
||||||
find_minmax({1,1,1,1,1,1,1,1,1}, min, max);
|
find_minmax({1,1,1,1,1,1,1,1,1}, min, max);
|
||||||
CHECK(min == 1);
|
CHECK(min == 1);
|
||||||
CHECK(max == 1);
|
CHECK(max == 1);
|
||||||
}
|
}
|
||||||
TEST_CASE("distinct positive numbers") {
|
TEST_CASE("distinct negative numbers") {
|
||||||
double min = 0;
|
double min = 0;
|
||||||
double max = 0;
|
double max = 0;
|
||||||
find_minmax({-1.5,-2,-3,-4}, min, max);
|
find_minmax({-1.5,-2,-3,-4}, min, max);
|
||||||
CHECK(min == -4);
|
CHECK(min == -4);
|
||||||
CHECK(max == -1.5);
|
CHECK(max == -1.5);
|
||||||
}
|
}
|
||||||
|
TEST_CASE("percent 1") {
|
||||||
|
const vector<size_t>bins{5, 10, 10, 50, 25};
|
||||||
|
vector<size_t>bins_percent{};
|
||||||
|
percent(bins, bins_percent);
|
||||||
|
CHECK(bins_percent[0] == 5);
|
||||||
|
}
|
||||||
|
|||||||
Ссылка в новой задаче
Block a user