code: добавлен вывод процентов

master
Varvara (ZeninaVA) 3 месяцев назад
Родитель 9333e93a79
Сommit ccff7aed06

@ -1,6 +1,10 @@
#include "histogram.h" #include "histogram.h"
#include <cmath>
void find_minmax(vector<double> vec, double& min, double& max) { bool find_minmax(vector<double> vec, double& min, double& max) {
if (vec.size() == 0) {
cerr << "Empty vec";
return false;
}
min = vec[0]; min = vec[0];
max = vec[0]; max = vec[0];
for (double x : vec) { for (double x : vec) {
@ -12,8 +16,8 @@ void find_minmax(vector<double> vec, double& min, double& max) {
max = x; max = x;
} }
} }
return true;
} }
vector<size_t> make_histogram(size_t number, vector<double> vec) { vector<size_t> make_histogram(size_t number, vector<double> vec) {
vector<size_t> bins(number); vector<size_t> bins(number);
double mn, mx; double mn, mx;
@ -36,3 +40,19 @@ vector<size_t> make_histogram(size_t number, vector<double> vec) {
} }
return bins; return bins;
} }
vector<int> make_percentages(const vector<size_t>& bins) {
size_t total = 0;
for (auto x : bins) {
total += x;
}
vector<int> pct(bins.size());
for (size_t i = 0; i < bins.size(); i++) {
if (total > 0) {
pct[i] = static_cast<int>(round(100.0 * bins[i] / total));
}
else {
pct[i] = 0;
}
}
return pct;
}

@ -4,5 +4,5 @@
#include <iostream> #include <iostream>
using namespace std; using namespace std;
vector<size_t> make_histogram(size_t number, vector<double> vec); vector<size_t> make_histogram(size_t number, vector<double> vec);
vector<int> make_percentages(const vector<size_t>& bins);
#endif // HISTOGRAM_H_INCLUDED #endif // HISTOGRAM_H_INCLUDED

@ -1,6 +1,6 @@
#ifndef HISTOGRAM_INTERNAL_H_INCLUDED #ifndef HISTOGRAM_INTERNAL_H_INCLUDED
#define HISTOGRAM_INTERNAL_H_INCLUDED #define HISTOGRAM_INTERNAL_H_INCLUDED
void find_minmax(std::vector<double> vec, double& min, double& max); bool find_minmax(std::vector<double> vec, double& min, double& max);
#endif // HISTOGRAM_INTERNAL_H_INCLUDED #endif // HISTOGRAM_INTERNAL_H_INCLUDED

@ -32,7 +32,22 @@
<Add option="-Wall" /> <Add option="-Wall" />
<Add option="-fexceptions" /> <Add option="-fexceptions" />
</Compiler> </Compiler>
<Unit filename="histogram.cpp" />
<Unit filename="histogram.h">
<Option target="&lt;{~None~}&gt;" />
</Unit>
<Unit filename="histogram_internal.h">
<Option target="&lt;{~None~}&gt;" />
</Unit>
<Unit filename="main.cpp" /> <Unit filename="main.cpp" />
<Unit filename="svg.cpp" />
<Unit filename="svg.h">
<Option target="&lt;{~None~}&gt;" />
</Unit>
<Unit filename="text.cpp" />
<Unit filename="text.h">
<Option target="&lt;{~None~}&gt;" />
</Unit>
<Extensions> <Extensions>
<lib_finder disable_auto="1" /> <lib_finder disable_auto="1" />
</Extensions> </Extensions>

@ -1,5 +1,5 @@
#include "svg.h" #include "svg.h"
#include <cmath>
using namespace std; using namespace std;
void void
@ -34,38 +34,62 @@ svg_rect(double x, double y, double width, double height, string stroke = "black
void
show_histogram_svg(const vector<size_t>& bins) void show_histogram_svg(const vector<size_t>& bins)
{ {
const auto IMAGE_WIDTH = 400; const auto ORIG_WIDTH = 400;
const auto IMAGE_HEIGHT = 300; const auto PADDING_RIGHT = 50;
const auto TEXT_LEFT = 20; const auto IMAGE_WIDTH = ORIG_WIDTH + PADDING_RIGHT;
const auto IMAGE_HEIGHT = 300;
const auto TEXT_LEFT = 20;
const auto TEXT_BASELINE = 20; const auto TEXT_BASELINE = 20;
const auto TEXT_WIDTH = 50; const auto TEXT_WIDTH = 50;
const auto BIN_HEIGHT = 30; const auto BIN_HEIGHT = 30;
const auto BLOCK_WIDTH = 10; const auto BLACK = "black";
const auto GREEN = "green" const auto RED = "red";
const auto RED = "red"; const auto MAX_WIDTH = ORIG_WIDTH - TEXT_WIDTH;
const auto MAX_WIDTH = IMAGE_WIDTH - TEXT_WIDTH; const auto PERCENT_X = TEXT_WIDTH + MAX_WIDTH + 10;
svg_begin(IMAGE_WIDTH, IMAGE_HEIGHT);
svg_begin(IMAGE_WIDTH,IMAGE_HEIGHT); double max_count = 0;
for (auto b : bins) {
if (b > max_count) {
max_count = b;
}
}
size_t total_count = 0;
for (auto b : bins) {
total_count += b;
}
double top = 0; double top = 0;
double max_count = bins[0]; for (auto b : bins) {
for (size_t i = 0; i < bins.size(); i++)
{ double bin_width = 0;
if (max_count < bins[i]) if (max_count > 0) {
{ bin_width = MAX_WIDTH * (b / max_count);
max_count = bins[i];
} }
}
for (size_t bin : bins)
{ svg_text(TEXT_LEFT, top + TEXT_BASELINE, to_string(b));
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, GREEN, RED);
int pct = 0;
if (total_count > 0) {
pct = static_cast<int>(round(100.0 * b / total_count));
}
string pct_str = to_string(pct);
if (pct < 10) {
pct_str = "0" + pct_str;
}
pct_str += "%";
svg_text(PERCENT_X, top + TEXT_BASELINE, pct_str);
top += BIN_HEIGHT; top += BIN_HEIGHT;
} }

@ -1,6 +1,5 @@
#ifndef SVG_H_INCLUDED #ifndef SVG_H_INCLUDED
#define SVG_H_INCLUDED #define SVG_H_INCLUDED
#include <iostream> #include <iostream>
#include <vector> #include <vector>
#include <string> #include <string>

@ -1,9 +1,15 @@
#include "text.h" #include "text.h"
#include "histogram.h"
#include <iostream>
#include <vector>
#include <string>
void show_histogram(std::vector<size_t> bins) { void show_histogram(vector<size_t> bins) {
bool gigant = false; bool gigant = false;
auto spaces = 0; size_t spaces = 0;
size_t mx_count = 0; size_t mx_count = 0;
for (auto x : bins) { for (auto x : bins) {
if (x > 76) { if (x > 76) {
gigant = true; gigant = true;
@ -11,73 +17,68 @@ void show_histogram(std::vector<size_t> bins) {
if (x > mx_count) { if (x > mx_count) {
mx_count = x; mx_count = x;
} }
auto len = 0; size_t len = 0;
while (x > 0) { size_t t = x;
x /= 10; do {
t /= 10;
len++; len++;
} } while (t > 0);
if (len > spaces) { if (len > spaces) {
spaces = len; spaces = len;
} }
}
size_t bar_max_width;
if (gigant) {
bar_max_width = 76;
} else {
bar_max_width = mx_count;
} }
if (spaces == 1) {
for (size_t i = 0; i < bins.size(); i++) { auto percentages = make_percentages(bins);
std::cout << " " << bins[i] << "|";
if (gigant) { for (size_t i = 0; i < bins.size(); ++i) {
if (bins[i] == mx_count) {
for (size_t j = 0; j < 76; j++) { size_t len = 0;
std::cout << "*"; size_t t = bins[i];
} do {
} t /= 10;
else len++;
{ } while (t > 0);
for (size_t j = 0; j < 76 * static_cast<double>(bins[i]) / mx_count; j++) { for (size_t s = len; s < spaces + 1; ++s) {
std::cout << "*"; cout << " ";
}
}
}
else
{
for (size_t j = 0; j < bins[i]; j++) {
std::cout << "*";
}
std::cout << std::endl;
}
} }
} cout << bins[i] << "|";
else
{
for (size_t i = 0; i < bins.size(); i++) { size_t star_count = 0;
int len = 1; if (gigant) {
int k = bins[i]; if (bins[i] == mx_count) {
for (; k /= 10; ++len); star_count = 76;
while (len < spaces) { } else {
std::cout << " "; star_count = static_cast<size_t>(76 * static_cast<double>(bins[i]) / mx_count);
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
{
for (size_t j = 0; j < (76 * static_cast<double>(bins[i]) / mx_count - 1); j++) {
std::cout << "*";
}
}
}
else
{
for (size_t j = 0; j < bins[i]; j++) {
std::cout << "*";
}
} }
std::cout << std::endl; } else {
star_count = bins[i];
}
for (size_t j = 0; j < star_count; ++j) {
cout << "*";
} }
size_t pad = bar_max_width - star_count;
for (size_t p = 0; p < pad + 2; ++p) {
cout << " ";
}
int pct = percentages[i];
string pct_str;
if (pct < 10) {
pct_str = "0" + to_string(pct);
} else {
pct_str = to_string(pct);
}
pct_str += "%";
cout << pct_str << "\n";
} }
} }

@ -3,6 +3,7 @@
#include <iostream> #include <iostream>
#include <vector> #include <vector>
void show_histogram(std::vector<size_t> bins); void show_histogram(std::vector<size_t> bins);
#endif // TEXT_H_INCLUDED #endif // TEXT_H_INCLUDED

@ -31,6 +31,9 @@
<Compiler> <Compiler>
<Add option="-Wall" /> <Add option="-Wall" />
</Compiler> </Compiler>
<Unit filename="histogram.cpp" />
<Unit filename="histogram_internal.h" />
<Unit filename="unittest.cpp" />
<Extensions> <Extensions>
<lib_finder disable_auto="1" /> <lib_finder disable_auto="1" />
</Extensions> </Extensions>

@ -3,6 +3,8 @@
#include "doctest.h" #include "doctest.h"
#include "histogram_internal.h" #include "histogram_internal.h"
using namespace std;
TEST_CASE("distinct positive numbers") { TEST_CASE("distinct positive numbers") {
double min = 0; double min = 0;
double max = 0; double max = 0;
@ -10,12 +12,13 @@ TEST_CASE("distinct positive numbers") {
CHECK(min == 1); CHECK(min == 1);
CHECK(max == 2); CHECK(max == 2);
} }
TEST_CASE("distinct negative numbers"){
TEST_CASE("empty vector"){
double min = 0; double min = 0;
double max = 0; double max = 0;
find_minmax({-1, -2}, min, max); vector<double> empty;
CHECK(min == -2); bool result = find_minmax(empty, min, max);
CHECK(max == -1); CHECK(!result);
} }
TEST_CASE("vector of the same elements"){ TEST_CASE("vector of the same elements"){
double min = 0; double min = 0;

@ -1,8 +1,60 @@
# depslib dependency file v1.0 # depslib dependency file v1.0
1746270053 source:c:\users\professional\desktop\cs-lab34\lab1\histogram.cpp 1746444035 source:c:\users\professional\desktop\cs-lab34\lab1\histogram.cpp
"histogram.h" "histogram.h"
<cmath>
1746269334 c:\users\professional\desktop\cs-lab34\lab1\histogram.h 1746443248 c:\users\professional\desktop\cs-lab34\lab1\histogram.h
<vector> <vector>
<iostream> <iostream>
1746274690 source:c:\users\professional\desktop\cs-lab34\lab1\unittest.cpp
"doctest.h"
"histogram_internal.h"
1746271199 c:\users\professional\desktop\cs-lab34\lab1\doctest.h
<signal.h>
<ciso646>
<cstddef>
<ostream>
<istream>
<type_traits>
"doctest_fwd.h"
<ctime>
<cmath>
<climits>
<math.h>
<new>
<cstdio>
<cstdlib>
<cstring>
<limits>
<utility>
<fstream>
<sstream>
<iostream>
<algorithm>
<iomanip>
<vector>
<atomic>
<mutex>
<set>
<map>
<unordered_set>
<exception>
<stdexcept>
<csignal>
<cfloat>
<cctype>
<cstdint>
<string>
<sys/types.h>
<unistd.h>
<sys/sysctl.h>
<AfxWin.h>
<windows.h>
<io.h>
<sys/time.h>
<unistd.h>
1746444163 c:\users\professional\desktop\cs-lab34\lab1\histogram_internal.h

Загрузка…
Отмена
Сохранить