#include <iostream>
#include <vector>
<<<<<<< HEAD
#include "histogram.h"
#include "text.h"
#include "svg.h"
=======

using namespace std;
>>>>>>> origin/main
struct Input {
    vector<double> numbers;
    size_t bin_count{};
};

<<<<<<< HEAD
Input input_data() {
    Input in;
    size_t number_count;
=======
Input
input_data() {
    size_t number_count, bin_count;
    double minl, maxl, bin_size;

>>>>>>> origin/main

    cerr << "Enter the number of elements: ";
    cin >> number_count;

<<<<<<< HEAD
    in.numbers.resize(number_count);

=======
    Input in;
    in.numbers.resize(number_count);

    vector<double> numbers(number_count);

>>>>>>> origin/main
    cerr << "\nEnter " << number_count << " elements:" << endl;
    for (size_t i = 0; i < number_count; i++) {
        cin >> in.numbers[i];
    }

<<<<<<< HEAD
=======

>>>>>>> origin/main
    cerr << "Enter the number of bins: ";
    cin >> in.bin_count;

    return in;
}

<<<<<<< HEAD
int main() {
    auto in = input_data();
    auto bins = make_histogram(in.bin_count, in.numbers);
    show_histogram_svg(bins);
=======
void
find_minmax(const vector<double>& numbers, double& minl, double& maxl) {
    minl = numbers[0];
    maxl = numbers[0];

    for (double x : numbers) {
        if (x < minl) {
            minl = x;
        } else {
            if (x > maxl) {
                maxl = x;
            }
        }
    }
}

vector<size_t>
make_histogram(const vector<double>& numbers, size_t& bin_count) {
    double minn, maxn;
    find_minmax(numbers, minn, maxn);

    double bin_size = (maxn - minn) / bin_count;

    vector<size_t> bins(bin_count, 0);

    for (size_t i = 0; i < numbers.size(); i++) {
        bool found = false;
        for (size_t j = 0; (j < bin_count - 1) && !found; j++) {
            auto lo = minn + j * bin_size;
            auto hi = minn + (j + 1) * bin_size;
            if ((numbers[i] >= lo) && (numbers[i] <= 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) {
    size_t max_count = bins[0];
    for (size_t i = 1; i < bin_count; i++) {
        if (bins[i] > max_count) {
            max_count = bins[i];
        }
    }

    const size_t SCREEN_WIDTH = 80;
    const size_t MAX_ASTERISK = SCREEN_WIDTH - 3 - 1;


    short space_count;

    cerr << "\nHistogram:" << endl;

    if (max_count <= MAX_ASTERISK) {
        for (size_t i = 0; i < bin_count; i++) {
            if (bins[i] < 10) {
                space_count = 2;
            } else if (bins[i] >= 10 && bins[i] < 100) {
                space_count = 1;
            } else if (bins[i] >= 100 && bins[i] < 1000) {
                space_count = 0;
            }



            for (size_t k = 0; k < space_count; k++) {
                cout << " ";
            }

            cout << bins[i];

            cout << "|";


            for (size_t j = 0; j < bins[i]; j++) {
                cout << "*";
            }

            cout << "\n";
        }
    } else {
        for (size_t i = 0; i < bin_count; i++) {
            if (bins[i] < 10) {
                space_count = 2;
            } else if (bins[i] >= 10 && bins[i] < 100) {
                space_count = 1;
            } else if (bins[i] >= 100 && bins[i] < 1000) {
                space_count = 0;
            }


            for (size_t k = 0; k < space_count; k++) {
                cout << " ";
            }

            cout << bins[i];

            cout << "|";

            size_t height = static_cast<size_t>(MAX_ASTERISK * (static_cast<double>(bins[i]) / max_count));
            for (size_t j = 0; j < height; j++) {
                cout << "*";
            }

            cout << "\n";
        }
    }
}

int main() {

    auto in = input_data();
    auto bins = make_histogram(in.numbers, in.bin_count);
    show_histogram_text(bins, in.bin_count);

>>>>>>> origin/main
    return 0;
}
