code: добавлен параматер promt

master
Andrew (ShabatovAA) 1 год назад
Родитель 3156754b21
Сommit 48586b2650

@ -1,10 +1,13 @@
#include <vector> #include <vector>
#include "histogram.h" #include "histogram.h"
using namespace std; using namespace std;
void find_minmax(const vector<double>& numbers, double& min, double& max)
{ bool
find_minmax(const std::vector<double>& numbers, double& min, double& max){
if (numbers.size() != 0) {
min = numbers[0]; min = numbers[0];
max = numbers[0]; max = numbers[0];
for (double x : numbers) { for (double x : numbers) {
if (x < min) { if (x < min) {
min = x; min = x;
@ -13,8 +16,16 @@ void find_minmax(const vector<double>& numbers, double& min, double& max)
max = x; max = x;
} }
} }
return true;
}
else {
return false;
}
} }
vector<size_t> make_histogram(const vector<double>& numbers, size_t bin_count) vector<size_t> make_histogram(const vector<double>& numbers, size_t bin_count)
{ {
double min,max; double min,max;

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

@ -10,28 +10,44 @@ struct Input{
size_t bin_count{}; size_t bin_count{};
}; };
Input input_data() Input input_data(istream& in, bool promt)
{ {
if (promt){
size_t number_count; size_t number_count;
cerr << "Enter number count: "; cerr << "Enter number count: ";
cin >> number_count; in >> number_count;
Input in; Input In;
in.numbers.resize(number_count); In.numbers.resize(number_count);
cerr << "Enter numbers: "; 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 >> In.numbers[i];
} }
cerr << "Enter bin count: "; cerr << "Enter bin count: ";
cin >> in.bin_count; in >> In.bin_count;
return in; return In;
}
else{
size_t number_count;
in >> number_count;
Input In;
In.numbers.resize(number_count);
for(size_t i = 0; i < number_count; i++){
in >> In.numbers[i];
}
in >> In.bin_count;
return In;
}
} }
int main() int 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;

@ -2,7 +2,6 @@
#include <iostream> #include <iostream>
#include <vector> #include <vector>
#include <string> #include <string>
const size_t x=20;
using namespace std; using namespace std;
void void
@ -35,7 +34,7 @@ void
show_histogram_svg(const vector<size_t>& bins) { show_histogram_svg(const vector<size_t>& bins) {
const auto IMAGE_WIDTH = 400; const auto IMAGE_WIDTH = 400;
const auto IMAGE_HEIGHT = 300; const auto IMAGE_HEIGHT = 300;
const auto TEXT_LEFT = IMAGE_WIDTH-30; 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;
@ -54,7 +53,7 @@ show_histogram_svg(const vector<size_t>& bins) {
for (size_t bin : bins) { for (size_t bin : bins) {
const double bin_width = space * bin / max_count; const double bin_width = space * 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_LEFT - bin_width - 20, top, bin_width, BIN_HEIGHT, "black", "blue"); svg_rect(TEXT_WIDTH, top, bin_width, BIN_HEIGHT, "black", "blue");
top += BIN_HEIGHT; top += BIN_HEIGHT;
} }
svg_end(); svg_end();
@ -63,7 +62,7 @@ show_histogram_svg(const vector<size_t>& bins) {
for (size_t bin : bins) { for (size_t bin : bins) {
const double bin_width = BLOCK_WIDTH * bin; const double bin_width = BLOCK_WIDTH * bin;
svg_text(TEXT_LEFT, top + TEXT_BASELINE, to_string(bin)); svg_text(TEXT_LEFT, top + TEXT_BASELINE, to_string(bin));
svg_rect(TEXT_LEFT - bin_width - 20, top, bin_width, BIN_HEIGHT, "black", "blue"); svg_rect(TEXT_WIDTH, top, bin_width, BIN_HEIGHT, "black", "blue");
top += BIN_HEIGHT; top += BIN_HEIGHT;
} }
svg_end(); svg_end();

@ -4,9 +4,40 @@
#include "histogram_internal.h" #include "histogram_internal.h"
TEST_CASE("distinct positive numbers") { TEST_CASE("distinct positive numbers") {
double min = 0;
double max = 0;
find_minmax({1, 2}, min, max);
CHECK(min == 1);
CHECK(max == 2);
}
TEST_CASE("negative numbers") {
double min = 0; double min = 0;
double max = 0; double max = 0;
find_minmax({-1, -2 , -5}, min, max); find_minmax({-1, -2 , -5}, min, max);
CHECK(min == -3); CHECK(min == -5);
CHECK(max == -1); CHECK(max == -1);
} }
TEST_CASE("one number") {
double min = 0;
double max = 0;
find_minmax({1}, min, max);
CHECK(min == 1);
CHECK(max == 1);
}
TEST_CASE("equal elements") {
double min = 0;
double max = 0;
find_minmax({1,1,1,1}, min, max);
CHECK(min == 1);
CHECK(max == 1);
}
TEST_CASE("zero vector") {
double min = 0;
double max = 0;
CHECK(!find_minmax({}, min, max));
}

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