code: refactoring
Этот коммит содержится в:
@@ -1,18 +1,26 @@
|
|||||||
#include "histogram.h"
|
#include "histogram.h"
|
||||||
|
|
||||||
void
|
bool
|
||||||
find_minmax(const std::vector<double>& numbers, double& min, double& max){
|
find_minmax(const std::vector<double>& numbers, double& min, double& max){
|
||||||
min = numbers[0];
|
if (numbers.size() != 0) {
|
||||||
max = numbers[0];
|
min = numbers[0];
|
||||||
|
max = numbers[0];
|
||||||
|
|
||||||
for (double x : numbers) {
|
for (double x : numbers) {
|
||||||
if (x < min) {
|
if (x < min) {
|
||||||
min = x;
|
min = x;
|
||||||
}
|
}
|
||||||
else if (x > max) {
|
else if (x > max) {
|
||||||
max = x;
|
max = x;
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
return true;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
else {
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
|
|||||||
@@ -3,8 +3,7 @@
|
|||||||
#include <vector>
|
#include <vector>
|
||||||
#include <string>
|
#include <string>
|
||||||
|
|
||||||
void
|
bool find_minmax(const std::vector<double>& numbers, double& min, double& max);
|
||||||
find_minmax(const std::vector<double>& numbers, double& min, double& max);
|
|
||||||
|
|
||||||
std::string
|
std::string
|
||||||
bin_color(size_t bin, size_t max_count);
|
bin_color(size_t bin, size_t max_count);
|
||||||
|
|||||||
25
main.cpp
25
main.cpp
@@ -11,29 +11,32 @@ using namespace std;
|
|||||||
};
|
};
|
||||||
|
|
||||||
Input
|
Input
|
||||||
input_data(){
|
input_data(istream& in, bool prompt){
|
||||||
|
|
||||||
|
if (prompt == true) cerr << "Enter number_count: ";
|
||||||
|
|
||||||
size_t number_count;
|
size_t number_count;
|
||||||
cerr << "Enter number_count: ";
|
in >> number_count;
|
||||||
cin >> number_count;
|
|
||||||
|
Input data;
|
||||||
|
data.numbers.resize(number_count);
|
||||||
|
|
||||||
|
if (prompt == true) cerr << "Enter numbers: ";
|
||||||
|
|
||||||
Input in;
|
|
||||||
in.numbers.resize(number_count);
|
|
||||||
cerr << "Enter numbers: ";
|
|
||||||
for (size_t i = 0; i < number_count; i++){
|
for (size_t i = 0; i < number_count; i++){
|
||||||
cin >> in.numbers[i];
|
in >> data.numbers[i];
|
||||||
}
|
}
|
||||||
|
|
||||||
cerr << "Enter bin count: ";
|
if (prompt == true) cerr << "Enter bin count: ";
|
||||||
cin >> in.bin_count;
|
|
||||||
|
|
||||||
return in;
|
in >> data.bin_count;
|
||||||
|
return data;
|
||||||
}
|
}
|
||||||
|
|
||||||
int
|
int
|
||||||
main()
|
main()
|
||||||
{
|
{
|
||||||
auto in = input_data();
|
auto in = input_data(cin, false);
|
||||||
auto bins = make_histogram(in.numbers, in.bin_count);
|
auto bins = make_histogram(in.numbers, in.bin_count);
|
||||||
show_histogram_svg(bins);
|
show_histogram_svg(bins);
|
||||||
return 0;
|
return 0;
|
||||||
|
|||||||
@@ -39,6 +39,10 @@
|
|||||||
<Unit filename="mainmain.c">
|
<Unit filename="mainmain.c">
|
||||||
<Option compilerVar="CC" />
|
<Option compilerVar="CC" />
|
||||||
</Unit>
|
</Unit>
|
||||||
|
<Unit filename="svg.cpp" />
|
||||||
|
<Unit filename="svg.h">
|
||||||
|
<Option target="<{~None~}>" />
|
||||||
|
</Unit>
|
||||||
<Unit filename="text.cpp" />
|
<Unit filename="text.cpp" />
|
||||||
<Unit filename="text.h" />
|
<Unit filename="text.h" />
|
||||||
<Extensions />
|
<Extensions />
|
||||||
|
|||||||
10
svg.cpp
10
svg.cpp
@@ -28,9 +28,9 @@ svg_rect(double x, double y, double width, double height, string stroke, string
|
|||||||
cout << "<rect x = '" << x << "' y = '" << y << "' width = '" << width << "' height = '" << height << "' stroke = '" << stroke << "' fill = '" << fil << "' />";
|
cout << "<rect x = '" << x << "' y = '" << y << "' width = '" << width << "' height = '" << height << "' stroke = '" << stroke << "' fill = '" << fil << "' />";
|
||||||
}
|
}
|
||||||
|
|
||||||
string bin_color(size_t bin, size_t max_count){
|
//string bin_color(size_t bin, size_t max_count){
|
||||||
return ("#" + to_string(10 - (bin * 9) / max_count) + to_string(10 - (bin * 9) / max_count) + to_string(10 - (bin * 9) / max_count));
|
// return ("#" + to_string(10 - (bin * 9) / max_count) + to_string(10 - (bin * 9) / max_count) + to_string(10 - (bin * 9) / max_count));
|
||||||
}
|
//}
|
||||||
|
|
||||||
void
|
void
|
||||||
show_histogram_svg(const vector<size_t>& bins) {
|
show_histogram_svg(const vector<size_t>& bins) {
|
||||||
@@ -53,9 +53,9 @@ show_histogram_svg(const vector<size_t>& bins) {
|
|||||||
double top = 0;
|
double top = 0;
|
||||||
for (size_t bin : bins) {
|
for (size_t bin : bins) {
|
||||||
const double bin_width = (IMAGE_WIDTH - TEXT_WIDTH) * bin / max_count;
|
const double bin_width = (IMAGE_WIDTH - TEXT_WIDTH) * bin / max_count;
|
||||||
const auto color = bin_color(bin,max_count);
|
//const auto color = bin_color(bin,max_count);
|
||||||
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, bin_width, BIN_HEIGHT,"grey", color);
|
svg_rect(TEXT_WIDTH, top, bin_width, BIN_HEIGHT,"red", "#ffeeee");
|
||||||
top += BIN_HEIGHT;
|
top += BIN_HEIGHT;
|
||||||
}
|
}
|
||||||
svg_end();
|
svg_end();
|
||||||
|
|||||||
@@ -31,6 +31,11 @@
|
|||||||
<Compiler>
|
<Compiler>
|
||||||
<Add option="-Wall" />
|
<Add option="-Wall" />
|
||||||
</Compiler>
|
</Compiler>
|
||||||
|
<Unit filename="doctest.h" />
|
||||||
|
<Unit filename="histogram.cpp" />
|
||||||
|
<Unit filename="histogram_internal.h" />
|
||||||
|
<Unit filename="svg.cpp" />
|
||||||
|
<Unit filename="unittest.cpp" />
|
||||||
<Extensions>
|
<Extensions>
|
||||||
<lib_finder disable_auto="1" />
|
<lib_finder disable_auto="1" />
|
||||||
</Extensions>
|
</Extensions>
|
||||||
|
|||||||
31
unittest.cpp
31
unittest.cpp
@@ -13,6 +13,37 @@ TEST_CASE("distinct positive numbers") {
|
|||||||
CHECK(max == 2);
|
CHECK(max == 2);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
TEST_CASE("one element vector") {
|
||||||
|
double min = 0;
|
||||||
|
double max = 0;
|
||||||
|
find_minmax({1}, min, max);
|
||||||
|
CHECK(min == max);
|
||||||
|
}
|
||||||
|
|
||||||
|
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("identical elements vector") {
|
||||||
|
double min = 0;
|
||||||
|
double max = 0;
|
||||||
|
find_minmax({3,3,3,3}, min, max);
|
||||||
|
CHECK(min == 3);
|
||||||
|
CHECK(max == 3);
|
||||||
|
}
|
||||||
|
|
||||||
|
TEST_CASE("empty vector") {
|
||||||
|
double min = 0;
|
||||||
|
double max = 0;
|
||||||
|
bool result = find_minmax({}, min, max);
|
||||||
|
CHECK(result == false);
|
||||||
|
}
|
||||||
|
|
||||||
TEST_CASE("bin color") {
|
TEST_CASE("bin color") {
|
||||||
size_t max_bin = 10;
|
size_t max_bin = 10;
|
||||||
size_t min_bin = 3;
|
size_t min_bin = 3;
|
||||||
|
|||||||
Ссылка в новой задаче
Block a user