Сравнить коммиты

...

7 Коммитов

@ -0,0 +1,34 @@
static void
find_minmax(const std::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;
}
}
std::vector <size_t>
make_histogram(const std::vector<double>& numbers, size_t bucket, size_t number_count) {
std::vector <size_t> stolb(bucket);
double min, max;
find_minmax (numbers, min, max);
float k = (max-min)/bucket;
for (size_t i = 0; i < bucket; i++) stolb[i] = 0;
for (size_t i = 0; i < number_count; i++)
{
bool flag = false;
for (size_t j = 0; (j < bucket && !flag); j++)
{
if (numbers[i] >= (min+k*j) && numbers[i] < (min+k*(1+j)))
{
stolb[j]++;
flag = true;
}
}
if (!flag) stolb[bucket-1]++;
}
return stolb;
}

@ -0,0 +1,10 @@
#ifndef HISTOGRAM_H_INCLUDED
#define HISTOGRAM_H_INCLUDED
#include <vector>
#include "histogram.cpp"
std::vector<size_t>
make_histogram(const std::vector<double>& numbers, size_t buckets, size_t number_count);
#endif // HISTOGRAM_H_INCLUDED

@ -1,74 +1,34 @@
#include <iostream>
#include <vector>
#include "histogram.h"
#include "text.h"
using namespace std;
int main()
{
const size_t SCREEN_WIDTH = 80;
const size_t MAX_ASTERISK = SCREEN_WIDTH - 3 - 1;
int number_count, bucket;
cerr << "Enter number count: "; cin >> number_count;
cerr << "Enter bucket: "; cin >> bucket;
vector <double> numbers(number_count);
for (int i = 0; i < number_count; i++) cin >> numbers[i];
float min = numbers[0];
float max = numbers[0];
for (float x : numbers)
{
if (x < min) min = x;
else if (x > max) max = x;
struct Input {
vector<double> numbers;
size_t bucket{};
size_t number_count{};
};
Input
input_data() {
Input in;
cin >> in.number_count;
cin >> in.bucket;
in.numbers.resize(in.number_count);
for (size_t i = 0; i < in.number_count; i++) {
cin >> in.numbers[i];
}
return in;
}
float k = (max-min)/bucket;
vector <int> stolb(bucket);
for (int j = 0; j < bucket; j++) stolb[j] = 0;
for (int i = 0; i < number_count; i++)
{
bool flag = false;
for (int j = 0; (j < bucket && !flag); j++)
{
if (numbers[i] >= (min+k*j) && numbers[i] < (min+k*(1+j)))
{
stolb[j]++;
flag = true;
}
}
if (!flag) stolb[bucket-1]++;
}
int maxlen = 0;
for (int j = 0; j < bucket; j++)
{
if (maxlen<stolb[j]) maxlen = stolb[j];
}
int main()
{
for (int j = 0; j < bucket; j++)
{
if (stolb[j] < 100) cout << " ";
if (stolb[j] < 10) cout << " ";
cout << stolb[j] << "|";
size_t height = stolb[j];
if (maxlen > MAX_ASTERISK)
{
if (maxlen != stolb[j]) height = MAX_ASTERISK * (static_cast <float> (stolb[j])/maxlen);
else if (maxlen == stolb[j]) height = MAX_ASTERISK;
}
for (int i = 0; i < height; i++) cout << "*";
cout << "\n";
}
auto in = input_data();
auto stolb = make_histogram(in.numbers, in.bucket, in.number_count);
show_histogram_text(stolb, in.bucket);
return 0;
}

@ -0,0 +1,26 @@
void
show_histogram_text(std::vector <size_t> stolb, size_t bucket) {
const size_t SCREEN_WIDTH = 80;
const size_t MAX_ASTERISK = SCREEN_WIDTH - 3 - 1;
int maxlen = 0;
for (int j = 0; j < bucket; j++)
{
if (maxlen<stolb[j]) maxlen = stolb[j];
}
for (int j = 0; j < bucket; j++)
{
if (stolb[j] < 100) std::cout << " ";
if (stolb[j] < 10) std::cout << " ";
std::cout << stolb[j] << "|";
size_t height = stolb[j];
if (maxlen > MAX_ASTERISK)
{
if (maxlen != stolb[j]) height = MAX_ASTERISK * (static_cast <float> (stolb[j])/maxlen);
else if (maxlen == stolb[j]) height = MAX_ASTERISK;
}
for (int i = 0; i < height; i++) std::cout << "*";
std::cout << "\n";
}
}

@ -0,0 +1,10 @@
#ifndef TEXT_H_INCLUDED
#define TEXT_H_INCLUDED
#include <vector>
#include "text.cpp"
void
show_histogram_text(std::vector <size_t> stolb, size_t bucket);
#endif // TEXT_H_INCLUDED
Загрузка…
Отмена
Сохранить