code: дизайн
Этот коммит содержится в:
@@ -1,5 +1,10 @@
|
||||
#include "histogram.h"
|
||||
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];
|
||||
max = vec[0];
|
||||
for (double x : vec) {
|
||||
@@ -11,6 +16,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,4 +1,5 @@
|
||||
#include <vector>
|
||||
#include <iostream>
|
||||
|
||||
using namespace std;
|
||||
vector<size_t> make_histogram(size_t number, vector<double> vec);
|
||||
|
||||
@@ -1 +1,2 @@
|
||||
void find_minmax(std::vector<double> vec, double& min, double& max);
|
||||
bool find_minmax(std::vector<double> vec, double& min, double& max);
|
||||
//
|
||||
|
||||
2
main.cpp
2
main.cpp
@@ -1,6 +1,7 @@
|
||||
#include "histogram.h"
|
||||
#include "text.h"
|
||||
#include "svg.h"
|
||||
|
||||
struct Input {
|
||||
vector<double> vec;
|
||||
size_t korz{};
|
||||
@@ -19,6 +20,7 @@ Input input_data() {
|
||||
cin >> in.korz;
|
||||
return in;
|
||||
}
|
||||
|
||||
int main() {
|
||||
auto in = input_data();
|
||||
auto bins = make_histogram(in.korz, in.vec);
|
||||
|
||||
4
svg.cpp
4
svg.cpp
@@ -44,7 +44,7 @@ show_histogram_svg(const vector<size_t>& bins)
|
||||
const auto TEXT_WIDTH = 50;
|
||||
const auto BIN_HEIGHT = 30;
|
||||
const auto BLOCK_WIDTH = 10;
|
||||
const auto BLACK = "black";
|
||||
const auto GREEN = "green";
|
||||
const auto RED = "red";
|
||||
const auto MAX_WIDTH = IMAGE_WIDTH - TEXT_WIDTH;
|
||||
|
||||
@@ -65,7 +65,7 @@ show_histogram_svg(const vector<size_t>& bins)
|
||||
{
|
||||
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);
|
||||
top += BIN_HEIGHT;
|
||||
}
|
||||
|
||||
|
||||
1
svg.h
1
svg.h
@@ -2,5 +2,6 @@
|
||||
#include <vector>
|
||||
#include <string>
|
||||
#include <math.h>
|
||||
|
||||
using namespace std;
|
||||
void show_histogram_svg(const vector<size_t>& bins);
|
||||
|
||||
1
text.cpp
1
text.cpp
@@ -19,7 +19,6 @@ void show_histogram(std::vector<size_t> bins) {
|
||||
if (len > spaces) {
|
||||
spaces = len;
|
||||
}
|
||||
|
||||
}
|
||||
if (spaces == 1) {
|
||||
for (size_t i = 0; i < bins.size(); i++) {
|
||||
|
||||
1
text.h
1
text.h
@@ -1,3 +1,4 @@
|
||||
#include <iostream>
|
||||
#include <vector>
|
||||
|
||||
void show_histogram(std::vector<size_t> bins);
|
||||
|
||||
35
unittest.cpp
35
unittest.cpp
@@ -3,6 +3,8 @@
|
||||
#include "doctest.h"
|
||||
#include "histogram_internal.h"
|
||||
|
||||
using namespace std;
|
||||
|
||||
TEST_CASE("distinct positive numbers") {
|
||||
double min = 0;
|
||||
double max = 0;
|
||||
@@ -10,23 +12,38 @@ TEST_CASE("distinct positive numbers") {
|
||||
CHECK(min == 1);
|
||||
CHECK(max == 2);
|
||||
}
|
||||
TEST_CASE("distinct negative numbers"){
|
||||
|
||||
TEST_CASE("empty vector"){
|
||||
double min = 0;
|
||||
double max = 0;
|
||||
find_minmax({-1, -2}, min, max);
|
||||
CHECK(min == -2);
|
||||
CHECK(max == -1);
|
||||
vector<double> empty;
|
||||
bool result = find_minmax(empty, min, max);
|
||||
CHECK(!result);
|
||||
}
|
||||
TEST_CASE("vector of the same elements"){
|
||||
|
||||
TEST_CASE("single vector"){
|
||||
double min = 0;
|
||||
double max = 0;
|
||||
find_minmax({3,3,3}, min, max);
|
||||
vector<double> single = {3};
|
||||
find_minmax(single, min, max);
|
||||
CHECK(min == 3);
|
||||
CHECK(max == 3);
|
||||
}
|
||||
TEST_CASE("vector of one elements"){
|
||||
|
||||
TEST_CASE("negative vector"){
|
||||
double min = 0;
|
||||
double max = 0;
|
||||
find_minmax({3}, min, max);
|
||||
CHECK(min == max);
|
||||
vector<double> negative = {-1, -2};
|
||||
find_minmax(negative, min, max);
|
||||
CHECK(min == -2);
|
||||
CHECK(max == -1);
|
||||
}
|
||||
|
||||
TEST_CASE("same vector"){
|
||||
double min = 0;
|
||||
double max = 0;
|
||||
vector<double> identical = {1, 1};
|
||||
find_minmax(identical, min, max);
|
||||
CHECK(min == 1);
|
||||
CHECK(max == 1);
|
||||
}
|
||||
|
||||
@@ -1,12 +1,12 @@
|
||||
# depslib dependency file v1.0
|
||||
1746114684 source:c:\users\german\desktop\Ïðîãè Ñ2\lab01\histogram.cpp
|
||||
1746175747 source:c:\users\german\desktop\Ïðîãè Ñ2\lab01\histogram.cpp
|
||||
"histogram.h"
|
||||
|
||||
1746114403 c:\users\german\desktop\Ïðîãè Ñ2\lab01\histogram.h
|
||||
<vector>
|
||||
<iostream>
|
||||
|
||||
1746116058 source:c:\users\german\desktop\Ïðîãè Ñ2\lab01\unittest.cpp
|
||||
1746175582 source:c:\users\german\desktop\Ïðîãè Ñ2\lab01\unittest.cpp
|
||||
"doctest.h"
|
||||
"histogram_internal.h"
|
||||
|
||||
@@ -55,5 +55,5 @@
|
||||
<sys/time.h>
|
||||
<unistd.h>
|
||||
|
||||
1746115147 c:\users\german\desktop\Ïðîãè Ñ2\lab01\histogram_internal.h
|
||||
1746175326 c:\users\german\desktop\Ïðîãè Ñ2\lab01\histogram_internal.h
|
||||
|
||||
|
||||
Ссылка в новой задаче
Block a user