code: программа разделена на файлы

main
Artyom (StepanovAV) 1 год назад
Родитель b31bbe8722
Сommit e5bd61e1b7

@ -0,0 +1,34 @@
#include "histogram.h"
#include <vector>
static void
find_minmax(const std::vector<double>& numbers, double& min, double& max){
min = numbers[0];
max = numbers[0];
for (auto x : numbers) {
if (x < min) {
min = x;
}
else if (x > max) {
max = x;
}
}
}
std::vector<size_t>
make_histogram(const std::vector<double>& numbers, size_t bin_count){
double min, max;
find_minmax (numbers, min, max);
double bin_size = (max - min) / bin_count;
std::vector<size_t> bins(bin_count);
for (auto x : numbers) {
bool found = false;
for (size_t j = 0; (j < bin_count - 1) && !found ; j++) {
if ((min + j * bin_size <= x) && (x < min + (j + 1) * bin_size)) {
bins[j] += 1;
found = true;
}
}
if (!found) bins[bin_count - 1]++;
}
return bins;
}

@ -0,0 +1,6 @@
#pragma once
#include <vector>
std::vector<size_t>
make_histogram(const std::vector<double>& numbers, size_t bin_count);

@ -1,11 +1,10 @@
#include <iostream>
#include <vector>
#include "histogram.h"
#include "text.h"
using namespace std;
const size_t SCREEN_WIDTH = 80;
const size_t MAX_LENGTH = SCREEN_WIDTH - 3 - 1;
struct Input {
vector<double> numbers;
size_t bin_count{};
@ -24,74 +23,11 @@ input_data(){
cin >> in.bin_count;
return in;
}
static void
find_minmax(const std::vector<double>& numbers, double& min, double& max){
min = numbers[0];
max = numbers[0];
for (auto x : numbers) {
if (x < min) {
min = x;
}
else if (x > max) {
max = x;
}
}
}
vector<size_t>
make_histogram(const std::vector<double>& numbers, size_t bin_count){
double min, max;
find_minmax (numbers, min, max);
double bin_size = (max - min) / bin_count;
std::vector<size_t> bins(bin_count);
for (auto x : numbers) {
bool found = false;
for (size_t j = 0; (j < bin_count - 1) && !found ; j++) {
if ((min + j * bin_size <= x) && (x < min + (j + 1) * bin_size)) {
bins[j] += 1;
found = true;
}
}
if (!found) bins[bin_count - 1]++;
}
return bins;
}
void
show_histogram_text(vector<size_t> bins, size_t bin_count ){
auto max_count = bins[0];
for (auto x : bins) {
if (x > max_count) {
max_count = x;
}
}
for (size_t i = 0; i < bin_count; i++) {
size_t j = 100;
while (bins[i] < j) {
cout << " ";
j /= 10;
}
cout << bins[i] << "|";
if (max_count > MAX_LENGTH) {
auto count = bins[i];
size_t height = MAX_LENGTH * (static_cast<double>(count) / max_count);
j = 0;
while (j < height) {
cout << "*";
j++;
}
}
else {
j = 0;
while (j < bins[i]) {
cout << "*";
j++;
}
}
cout << endl;
}
}
int
main() {
Input in = input_data();
auto in = input_data();
auto bins = make_histogram(in.numbers, in.bin_count);
show_histogram_text(bins, in.bin_count);
return 0;

@ -0,0 +1,43 @@
#include "text.h"
#include <vector>
#include <iostream>
using namespace std;
const size_t SCREEN_WIDTH = 80;
const size_t MAX_LENGTH = SCREEN_WIDTH - 3 - 1;
void
show_histogram_text(vector<size_t> bins, size_t bin_count ){
auto max_count = bins[0];
for (auto x : bins) {
if (x > max_count) {
max_count = x;
}
}
for (size_t i = 0; i < bin_count; i++) {
size_t j = 100;
while (bins[i] < j) {
cout << " ";
j /= 10;
}
cout << bins[i] << "|";
if (max_count > MAX_LENGTH) {
auto count = bins[i];
size_t height = MAX_LENGTH * (static_cast<double>(count) / max_count);
j = 0;
while (j < height) {
cout << "*";
j++;
}
}
else {
j = 0;
while (j < bins[i]) {
cout << "*";
j++;
}
}
cout << endl;
}
}

@ -0,0 +1,5 @@
#pragma once
#include <vector>
void
show_histogram_text(std::vector<size_t> bins, size_t bin_count );
Загрузка…
Отмена
Сохранить