code: добавлен параматер promt
Этот коммит содержится в:
@@ -1,20 +1,31 @@
|
|||||||
#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
|
||||||
min = numbers[0];
|
find_minmax(const std::vector<double>& numbers, double& min, double& max){
|
||||||
max = numbers[0];
|
if (numbers.size() != 0) {
|
||||||
for (double x : numbers) {
|
min = numbers[0];
|
||||||
if (x < min) {
|
max = numbers[0];
|
||||||
min = x;
|
|
||||||
}
|
for (double x : numbers) {
|
||||||
else if (x > max) {
|
if (x < min) {
|
||||||
max = x;
|
min = x;
|
||||||
|
}
|
||||||
|
else if (x > max) {
|
||||||
|
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
|
||||||
|
|||||||
42
main.cpp
42
main.cpp
@@ -10,28 +10,44 @@ struct Input{
|
|||||||
size_t bin_count{};
|
size_t bin_count{};
|
||||||
};
|
};
|
||||||
|
|
||||||
Input input_data()
|
Input input_data(istream& in, bool promt)
|
||||||
{
|
{
|
||||||
size_t number_count;
|
if (promt){
|
||||||
cerr << "Enter number count: ";
|
size_t number_count;
|
||||||
cin >> number_count;
|
cerr << "Enter 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: ";
|
||||||
|
in >> In.bin_count;
|
||||||
|
return In;
|
||||||
}
|
}
|
||||||
|
else{
|
||||||
|
size_t number_count;
|
||||||
|
in >> number_count;
|
||||||
|
|
||||||
cerr << "Enter bin count: ";
|
Input In;
|
||||||
cin >> in.bin_count;
|
|
||||||
return 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;
|
||||||
|
|||||||
7
svg.cpp
7
svg.cpp
@@ -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();
|
||||||
|
|||||||
33
unittest.cpp
33
unittest.cpp
@@ -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));
|
||||||
|
}
|
||||||
|
|
||||||
|
|||||||
Ссылка в новой задаче
Block a user