|
|
@ -1,6 +1,7 @@
|
|
|
|
#include <iostream>
|
|
|
|
#include <iostream>
|
|
|
|
#include <vector>
|
|
|
|
#include <vector>
|
|
|
|
|
|
|
|
#include "histogram.h"
|
|
|
|
|
|
|
|
#include "text.h"
|
|
|
|
struct Input{
|
|
|
|
struct Input{
|
|
|
|
std::vector<double> numbers;
|
|
|
|
std::vector<double> numbers;
|
|
|
|
size_t bin_count{};
|
|
|
|
size_t bin_count{};
|
|
|
@ -24,86 +25,11 @@ Input input_data() {
|
|
|
|
return in;
|
|
|
|
return in;
|
|
|
|
};
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
using namespace std;
|
|
|
|
using namespace std;
|
|
|
|
|
|
|
|
|
|
|
|
int main()
|
|
|
|
int main() {
|
|
|
|
{
|
|
|
|
Input in = input_data();
|
|
|
|
const size_t SCREEN_WIDTH = 80;
|
|
|
|
auto bins = make_histogram(in.numbers, in.bin_count);
|
|
|
|
const size_t MAX_ASTERISK = SCREEN_WIDTH - 3 - 1;
|
|
|
|
show_histogram_svg(bins);
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
int number_count, bucket;
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
cerr << "Enter number count: "; cin >> number_count;
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
std::cerr << "Enter numbers: \n";
|
|
|
|
|
|
|
|
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;
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
if (max == min) {// ïðîâåðêà íà àíîìàëèþ
|
|
|
|
|
|
|
|
std::cout << " Unable to create a histogram." << std::endl;
|
|
|
|
|
|
|
|
return 1;
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
cerr << "Enter bucket: "; cin >> bucket;
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
float k = (max - min) / bucket;
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
if (numbers.empty()) {//áûëè ëè ââåäåíû ÷èñëà
|
|
|
|
|
|
|
|
std::cout << "Empty. unable to create histogram." << std::endl;
|
|
|
|
|
|
|
|
return 1;
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
vector <int> stolb(bucket);
|
|
|
|
|
|
|
|
vector <int> proz(stolb);
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
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];
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
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";
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
}
|
|
|
|