SavinSA 12 месяцев назад
Родитель 9adfa15935
Сommit 91e075003d

@ -3,41 +3,47 @@
#include <iostream> #include <iostream>
using namespace std; using namespace std;
void bool
find_minmax(const vector<double>& numbers, double& min, double& max) { find_minmax(const vector<double>& numbers, double& min, double& max) {
min = numbers[0]; if (numbers.empty()) {
max = numbers[0]; return false;
for (double x : numbers) { }
if (x < min) {
min = x; min = numbers[0];
} max = numbers[0];
else if (x > max) { for (double x : numbers) {
max = x; if (x < min) {
} min = x;
} }
else if (x > max) {
max = x;
}
}
return true;
} }
std::vector<double> std::vector<double>
make_histogram(const std::vector<double>& numbers, size_t bin_count) { make_histogram(const std::vector<double>& numbers, size_t bin_count) {
vector<double> bins(bin_count); vector<double> bins(bin_count);
double min = 0, max = 0; double min = 0, max = 0;
find_minmax(numbers, min, max); find_minmax(numbers, min, max);
double bin_size = (max - min) / bin_count; double bin_size = (max - min) / bin_count;
for (int i = 0; i < numbers.size(); i++) { for (int i = 0; i < numbers.size(); i++) {
bool found = false; bool found = false;
for (int j = 0; (j < bin_count - 1) && !found; j++) { for (int j = 0; (j < bin_count - 1) && !found; j++) {
auto lo = min + j * bin_size; auto lo = min + j * bin_size;
auto hi = min + (j + 1) * bin_size; auto hi = min + (j + 1) * bin_size;
if ((lo <= numbers[i]) && (numbers[i] < hi)) { if ((lo <= numbers[i]) && (numbers[i] < hi)) {
bins[j]++; bins[j]++;
found = true; found = true;
} }
} }
if (!found) { if (!found) {
bins[bin_count - 1]++; bins[bin_count - 1]++;
} }
} }
return bins; return bins;
} }

@ -1,4 +1,4 @@
#pragma once #pragma once
#include <vector> #include <vector>
void bool
find_minmax(const std::vector<double>& numbers, double& min, double& max); find_minmax(const std::vector<double>& numbers, double& min, double& max);

@ -21,42 +21,42 @@ input_data() {
cerr << "Enter number count: "; cerr << "Enter number count: ";
cin >> number_count; cin >> number_count;
Input in; Input in1;
in.numbers.resize(number_count); in1.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]; cin >> in1.numbers[i];
} }
cerr << "Enter bin count: "; cerr << "Enter bin count: ";
cin >> in.bin_count; cin >> in1.bin_count;
cerr << "Enter stroke colour without spaces or in code format:"; cerr << "Enter stroke colour without spaces or in code format:";
cin.ignore(); cin.ignore();
getline(cin, in.stroke); getline(cin, in1.stroke);
for (i = 0; i < in.stroke.length(); i++) { for (i = 0; i < in1.stroke.length(); i++) {
if (isspace(in.stroke[i])) { if (isspace(in1.stroke[i])) {
cerr << "invalid input"; cerr << "invalid input";
exit(0); exit(0);
} }
} }
if (in.stroke[0] == check2[0]) { if (in1.stroke[0] == check2[0]) {
if (in.stroke.length() != 7) { if (in1.stroke.length() != 7) {
cerr << "invalid input"; cerr << "invalid input";
exit(0); exit(0);
} }
} }
return in; return in1;
} }
int main() int main()
{ {
const size_t SCREEN_WIDTH = 80; const size_t SCREEN_WIDTH = 80;
const size_t MAX_ASTERISK = SCREEN_WIDTH - 3 - 1; const size_t MAX_ASTERISK = SCREEN_WIDTH - 3 - 1;
auto in = input_data(); auto in1 = input_data();
auto bins = make_histogram(in.numbers, in.bin_count); auto bins = make_histogram(in1.numbers, in1.bin_count);
show_histogram_svg(bins, in.stroke); show_histogram_svg(bins, in1.stroke);
return 0; return 0;
} }

@ -13,9 +13,7 @@ TEST_CASE("distinct positive numbers") {
TEST_CASE("empty vector") { TEST_CASE("empty vector") {
double min = 0; double min = 0;
double max = 0; double max = 0;
find_minmax({NULL}, min, max); CHECK(!find_minmax({}, min, max));
CHECK(min == 0);
CHECK(max == 0);
} }
TEST_CASE("one element") { TEST_CASE("one element") {
double min = 0; double min = 0;

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