LatyshevGI 2 месяцев назад
Родитель 40e200994c
Сommit 6210fae2ed

@ -1,5 +1,10 @@
#include "histogram.h" #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]; min = vec[0];
max = vec[0]; max = vec[0];
for (double x : vec) { for (double x : vec) {
@ -11,6 +16,7 @@ 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);

@ -1,4 +1,5 @@
#include <vector> #include <vector>
#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);

@ -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);
//

@ -1,6 +1,7 @@
#include "histogram.h" #include "histogram.h"
#include "text.h" #include "text.h"
#include "svg.h" #include "svg.h"
struct Input { struct Input {
vector<double> vec; vector<double> vec;
size_t korz{}; size_t korz{};
@ -19,6 +20,7 @@ Input input_data() {
cin >> in.korz; cin >> in.korz;
return in; return in;
} }
int main() { int main() {
auto in = input_data(); auto in = input_data();
auto bins = make_histogram(in.korz, in.vec); auto bins = make_histogram(in.korz, in.vec);

@ -44,7 +44,7 @@ show_histogram_svg(const vector<size_t>& bins)
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 BLOCK_WIDTH = 10;
const auto BLACK = "black"; const auto GREEN = "green";
const auto RED = "red"; const auto RED = "red";
const auto MAX_WIDTH = IMAGE_WIDTH - TEXT_WIDTH; 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); double bin_width = (MAX_WIDTH) * (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, BLACK, RED); svg_rect(TEXT_WIDTH, top, bin_width, BIN_HEIGHT, GREEN, RED);
top += BIN_HEIGHT; top += BIN_HEIGHT;
} }

@ -2,5 +2,6 @@
#include <vector> #include <vector>
#include <string> #include <string>
#include <math.h> #include <math.h>
using namespace std; using namespace std;
void show_histogram_svg(const vector<size_t>& bins); void show_histogram_svg(const vector<size_t>& bins);

@ -19,7 +19,6 @@ void show_histogram(std::vector<size_t> bins) {
if (len > spaces) { if (len > spaces) {
spaces = len; spaces = len;
} }
} }
if (spaces == 1) { if (spaces == 1) {
for (size_t i = 0; i < bins.size(); i++) { for (size_t i = 0; i < bins.size(); i++) {

@ -1,3 +1,4 @@
#include <iostream> #include <iostream>
#include <vector> #include <vector>
void show_histogram(std::vector<size_t> bins); void show_histogram(std::vector<size_t> bins);

@ -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,23 +12,38 @@ 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("single vector"){
double min = 0; double min = 0;
double max = 0; double max = 0;
find_minmax({3,3,3}, min, max); vector<double> single = {3};
find_minmax(single, min, max);
CHECK(min == 3); CHECK(min == 3);
CHECK(max == 3); CHECK(max == 3);
} }
TEST_CASE("vector of one elements"){
TEST_CASE("negative vector"){
double min = 0; double min = 0;
double max = 0; double max = 0;
find_minmax({3}, min, max); vector<double> negative = {-1, -2};
CHECK(min == max); 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 # 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" "histogram.h"
1746114403 c:\users\german\desktop\Ïðîãè Ñ2\lab01\histogram.h 1746114403 c:\users\german\desktop\Ïðîãè Ñ2\lab01\histogram.h
<vector> <vector>
<iostream> <iostream>
1746116058 source:c:\users\german\desktop\Ïðîãè Ñ2\lab01\unittest.cpp 1746175582 source:c:\users\german\desktop\Ïðîãè Ñ2\lab01\unittest.cpp
"doctest.h" "doctest.h"
"histogram_internal.h" "histogram_internal.h"
@ -55,5 +55,5 @@
<sys/time.h> <sys/time.h>
<unistd.h> <unistd.h>
1746115147 c:\users\german\desktop\Ïðîãè Ñ2\lab01\histogram_internal.h 1746175326 c:\users\german\desktop\Ïðîãè Ñ2\lab01\histogram_internal.h

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