code: added stream
Этот коммит содержится в:
@@ -3,41 +3,47 @@
|
||||
#include <iostream>
|
||||
using namespace std;
|
||||
|
||||
void
|
||||
bool
|
||||
find_minmax(const vector<double>& numbers, double& min, double& max) {
|
||||
min = numbers[0];
|
||||
max = numbers[0];
|
||||
for (double x : numbers) {
|
||||
if (x < min) {
|
||||
min = x;
|
||||
}
|
||||
else if (x > max) {
|
||||
max = x;
|
||||
}
|
||||
}
|
||||
if (numbers.empty()) {
|
||||
return false;
|
||||
}
|
||||
|
||||
min = numbers[0];
|
||||
max = numbers[0];
|
||||
for (double x : numbers) {
|
||||
if (x < min) {
|
||||
min = x;
|
||||
}
|
||||
else if (x > max) {
|
||||
max = x;
|
||||
}
|
||||
}
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
std::vector<double>
|
||||
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;
|
||||
find_minmax(numbers, min, max);
|
||||
double min = 0, max = 0;
|
||||
find_minmax(numbers, min, max);
|
||||
|
||||
double bin_size = (max - min) / bin_count;
|
||||
for (int i = 0; i < numbers.size(); i++) {
|
||||
bool found = false;
|
||||
for (int j = 0; (j < bin_count - 1) && !found; j++) {
|
||||
auto lo = min + j * bin_size;
|
||||
auto hi = min + (j + 1) * bin_size;
|
||||
if ((lo <= numbers[i]) && (numbers[i] < hi)) {
|
||||
bins[j]++;
|
||||
found = true;
|
||||
}
|
||||
}
|
||||
if (!found) {
|
||||
bins[bin_count - 1]++;
|
||||
}
|
||||
}
|
||||
return bins;
|
||||
double bin_size = (max - min) / bin_count;
|
||||
for (int i = 0; i < numbers.size(); i++) {
|
||||
bool found = false;
|
||||
for (int j = 0; (j < bin_count - 1) && !found; j++) {
|
||||
auto lo = min + j * bin_size;
|
||||
auto hi = min + (j + 1) * bin_size;
|
||||
if ((lo <= numbers[i]) && (numbers[i] < hi)) {
|
||||
bins[j]++;
|
||||
found = true;
|
||||
}
|
||||
}
|
||||
if (!found) {
|
||||
bins[bin_count - 1]++;
|
||||
}
|
||||
}
|
||||
return bins;
|
||||
}
|
||||
|
||||
@@ -1,4 +1,4 @@
|
||||
#pragma once
|
||||
#include <vector>
|
||||
void
|
||||
bool
|
||||
find_minmax(const std::vector<double>& numbers, double& min, double& max);
|
||||
26
main.cpp
26
main.cpp
@@ -21,42 +21,42 @@ input_data() {
|
||||
cerr << "Enter number count: ";
|
||||
cin >> number_count;
|
||||
|
||||
Input in;
|
||||
in.numbers.resize(number_count);
|
||||
Input in1;
|
||||
in1.numbers.resize(number_count);
|
||||
cerr << "Enter numbers: ";
|
||||
for (size_t i = 0; i < number_count; i++) {
|
||||
cin >> in.numbers[i];
|
||||
cin >> in1.numbers[i];
|
||||
}
|
||||
|
||||
cerr << "Enter bin count: ";
|
||||
cin >> in.bin_count;
|
||||
cin >> in1.bin_count;
|
||||
|
||||
|
||||
cerr << "Enter stroke colour without spaces or in code format:";
|
||||
cin.ignore();
|
||||
getline(cin, in.stroke);
|
||||
getline(cin, in1.stroke);
|
||||
|
||||
for (i = 0; i < in.stroke.length(); i++) {
|
||||
if (isspace(in.stroke[i])) {
|
||||
for (i = 0; i < in1.stroke.length(); i++) {
|
||||
if (isspace(in1.stroke[i])) {
|
||||
cerr << "invalid input";
|
||||
exit(0);
|
||||
}
|
||||
}
|
||||
if (in.stroke[0] == check2[0]) {
|
||||
if (in.stroke.length() != 7) {
|
||||
if (in1.stroke[0] == check2[0]) {
|
||||
if (in1.stroke.length() != 7) {
|
||||
cerr << "invalid input";
|
||||
exit(0);
|
||||
}
|
||||
}
|
||||
return in;
|
||||
return in1;
|
||||
}
|
||||
|
||||
int main()
|
||||
{
|
||||
const size_t SCREEN_WIDTH = 80;
|
||||
const size_t MAX_ASTERISK = SCREEN_WIDTH - 3 - 1;
|
||||
auto in = input_data();
|
||||
auto bins = make_histogram(in.numbers, in.bin_count);
|
||||
show_histogram_svg(bins, in.stroke);
|
||||
auto in1 = input_data();
|
||||
auto bins = make_histogram(in1.numbers, in1.bin_count);
|
||||
show_histogram_svg(bins, in1.stroke);
|
||||
return 0;
|
||||
}
|
||||
|
||||
@@ -13,9 +13,7 @@ TEST_CASE("distinct positive numbers") {
|
||||
TEST_CASE("empty vector") {
|
||||
double min = 0;
|
||||
double max = 0;
|
||||
find_minmax({NULL}, min, max);
|
||||
CHECK(min == 0);
|
||||
CHECK(max == 0);
|
||||
CHECK(!find_minmax({}, min, max));
|
||||
}
|
||||
TEST_CASE("one element") {
|
||||
double min = 0;
|
||||
|
||||
Ссылка в новой задаче
Block a user