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