|
|
#include <iostream>
|
|
|
#include <vector>
|
|
|
#include "histogram.h"
|
|
|
#include "text.h"
|
|
|
#include "svg.h"
|
|
|
using namespace std;
|
|
|
|
|
|
struct Input {
|
|
|
vector<double> numbers;
|
|
|
size_t bin_count{};
|
|
|
double COLUMN_HIGHT;
|
|
|
};
|
|
|
Input
|
|
|
input_data() {
|
|
|
size_t number_count;
|
|
|
cerr << "Enter number count: ";
|
|
|
cin >> number_count;
|
|
|
Input in;
|
|
|
in.numbers.resize(number_count);
|
|
|
cerr << "Enter numbers: ";
|
|
|
for (size_t i = 0; i < number_count; i++) {
|
|
|
cin >> in.numbers[i];
|
|
|
}
|
|
|
cerr << "Enter bin count: ";
|
|
|
cin>> in.bin_count;
|
|
|
cerr << "Enter column hight: ";
|
|
|
cin>> in.COLUMN_HIGHT;
|
|
|
return in;
|
|
|
}
|
|
|
|
|
|
|
|
|
/*void
|
|
|
find_minmax(const vector<double>& numbers, double& min, double& max)
|
|
|
{
|
|
|
min = numbers[0];
|
|
|
max = numbers[0];
|
|
|
for (double x : numbers)
|
|
|
{
|
|
|
if (x > max )
|
|
|
{
|
|
|
max = x;
|
|
|
}
|
|
|
if (x < min)
|
|
|
{
|
|
|
min = x;
|
|
|
}
|
|
|
}
|
|
|
}
|
|
|
vector<size_t>
|
|
|
make_histogram(const vector<double>& numbers, size_t& bin_count)
|
|
|
{
|
|
|
double min = 0;
|
|
|
double max = 0;
|
|
|
find_minmax(numbers, min, max);
|
|
|
auto bin_size = (max - min)/bin_count;
|
|
|
vector<size_t> bins(bin_count);
|
|
|
for (double number : numbers)
|
|
|
{
|
|
|
bool found = false;
|
|
|
for (size_t j = 0; (j < bin_count - 1) && !found; j++)
|
|
|
{
|
|
|
auto lo = min + j * bin_size;
|
|
|
auto hi = min + (j + 1) * bin_size;
|
|
|
if ((lo <= number) && (number < hi))
|
|
|
{
|
|
|
bins[j]++;
|
|
|
found = true;
|
|
|
}
|
|
|
}
|
|
|
if (!found)
|
|
|
{
|
|
|
bins[bin_count - 1]++;
|
|
|
}
|
|
|
}
|
|
|
return bins;
|
|
|
}*/
|
|
|
/*void
|
|
|
show_histogram_text(const vector<size_t>& bins, size_t& bin_count) {
|
|
|
const size_t SCREEN_WIDTH = 80;
|
|
|
const size_t MAX_ASTERISK = SCREEN_WIDTH - 3 - 1;
|
|
|
size_t max_count = 0;
|
|
|
for (size_t s = 0; s < bin_count; s++)
|
|
|
{
|
|
|
if (bins[s] > max_count)
|
|
|
{
|
|
|
max_count = bins[s];
|
|
|
}
|
|
|
}
|
|
|
auto kof = static_cast<double>(MAX_ASTERISK) / max_count;
|
|
|
if (kof > 1)
|
|
|
{
|
|
|
kof = 1;
|
|
|
}
|
|
|
for (size_t bin : bins)
|
|
|
{
|
|
|
if (bin <100)
|
|
|
{
|
|
|
cout << " ";
|
|
|
}
|
|
|
if (bin <10)
|
|
|
{
|
|
|
cout << " ";
|
|
|
}
|
|
|
cout << bin << "|";
|
|
|
size_t weight = bin * kof;
|
|
|
for (size_t t = 0; t < weight; t++)
|
|
|
{
|
|
|
cout << "*";
|
|
|
}
|
|
|
cout << endl;
|
|
|
}
|
|
|
}*/
|
|
|
int
|
|
|
main() {
|
|
|
auto in = input_data();
|
|
|
auto bins = make_histogram(in.numbers, in.bin_count);
|
|
|
// show_histogram_svg(bins);
|
|
|
COLUMN_hight_function(in.COLUMN_HIGHT,in.bin_count,bins);
|
|
|
|
|
|
/*int main()
|
|
|
{
|
|
|
|
|
|
Input in = input_data();
|
|
|
double min, max;
|
|
|
find_minmax(in.numbers, min, max);
|
|
|
cout<< min << " " << max;
|
|
|
|
|
|
|
|
|
|
|
|
const size_t SCREEN_WIDTH = 80;
|
|
|
const size_t MAX_ASTERISK = SCREEN_WIDTH - 3 - 1;
|
|
|
size_t max_count = 0;
|
|
|
size_t max_count_bin;
|
|
|
size_t number_count;
|
|
|
//cerr << "Enter number count: ";
|
|
|
//cin >> number_count;
|
|
|
//vector<double> numbers(number_count);
|
|
|
//cerr << "Enter numbers: ";
|
|
|
//for (int i = 0; i < number_count; i++)
|
|
|
// {
|
|
|
// cin >> numbers[i];
|
|
|
// }
|
|
|
//size_t bin_count;
|
|
|
// cerr << "Enter bin count: ";
|
|
|
// cin >> bin_count;
|
|
|
// vector<size_t > bins(bin_count);
|
|
|
|
|
|
|
|
|
double bin_size = ((max - min) / bin_count);
|
|
|
|
|
|
for (size_t i = 0; i < number_count; i++)
|
|
|
{
|
|
|
bool found = false;
|
|
|
for (size_t 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]++;
|
|
|
}
|
|
|
for (size_t i = 0; i < bin_count; i++)
|
|
|
{
|
|
|
if (bins[i] > max_count)
|
|
|
{
|
|
|
max_count = bins[i];
|
|
|
max_count_bin = i;
|
|
|
}
|
|
|
}
|
|
|
}
|
|
|
|
|
|
|
|
|
vector<size_t> bins_height(bin_count);
|
|
|
if (max_count > MAX_ASTERISK) {
|
|
|
for (size_t i = 0; i < bin_count; i++) {
|
|
|
bins_height[i]= MAX_ASTERISK * (static_cast<double>(bins[i]) / max_count);
|
|
|
}
|
|
|
}
|
|
|
else {
|
|
|
for (size_t i = 0; i < bin_count; i++) {
|
|
|
bins_height[i] = bins[i];
|
|
|
}
|
|
|
}
|
|
|
|
|
|
for (size_t i = 0; i < bin_count; i++) {
|
|
|
if (bins[i] < 100) {
|
|
|
cout << " ";
|
|
|
if (bins[i] < 10) {
|
|
|
cout << " ";
|
|
|
}
|
|
|
}
|
|
|
cout << bins[i] << "|";
|
|
|
for (size_t j = 0; j < bins_height[i]; j++) {
|
|
|
cout << "*";
|
|
|
}
|
|
|
cout << endl;
|
|
|
}
|
|
|
|
|
|
|
|
|
size_t histogram_height;
|
|
|
cerr << "Enter the histogram height" << endl;
|
|
|
cin >> histogram_height;
|
|
|
size_t C = (histogram_height / bin_count);
|
|
|
//<2F><><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD> <20><><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD> <20><><EFBFBD><EFBFBD><EFBFBD> 3:
|
|
|
|
|
|
if (C >= 1) {
|
|
|
for (size_t i = 0; i < bin_count; i++) {
|
|
|
if (bins[i] < 100) {
|
|
|
cout << " ";
|
|
|
if (bins[i] < 10) {
|
|
|
cout << " ";
|
|
|
}
|
|
|
}
|
|
|
cout << bins[i];
|
|
|
cout << "|";
|
|
|
for (size_t j = 0; j < bins_height[i]; j++) {
|
|
|
cout << "*";
|
|
|
}
|
|
|
cout << endl;
|
|
|
|
|
|
for (size_t g = 1; g < C; g++) {
|
|
|
cout << " ";
|
|
|
cout << "|";
|
|
|
|
|
|
for (size_t j = 0; j < bins_height[i]; j++) {
|
|
|
cout << "*";
|
|
|
}
|
|
|
cout << endl;
|
|
|
|
|
|
}
|
|
|
}
|
|
|
}
|
|
|
else
|
|
|
{
|
|
|
cout << "Histogram height is too small";
|
|
|
}
|
|
|
*/
|
|
|
return 0;
|
|
|
}
|