lab01(PokhilAA) 12 месяцев назад
Родитель e5d8445c80
Сommit 4117860c45

@ -1,5 +1,8 @@
#include <iostream>
#include <vector>
#include "histogram.h"
#include "text.h"
#include "svg.h"
using namespace std;
struct Input {
@ -25,81 +28,12 @@ input_data() {
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 < min) {
min = x;
}
else if (x > max) {
max = x;
}
}
}
vector <double>
make_histogram(const vector<double>& numbers, size_t bin_count){
vector<double> bins (bin_count);
double min = 0, max = 0;
find_minmax(numbers, min, max);
double bin_size = (max - min) / bin_count;
for (int i = 0; i < numbers.size(); i++) {
bool found = false;
for (int 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]++;
}
}
return bins;
}
void
show_histogram_text (const vector<double>& bins,size_t MAX_ASTERISK, size_t bin_count){
size_t bin_max = 0;
size_t height = 0;
for (double x : bins)
{
if (x > bin_max){
bin_max = x;
}
}
for (size_t bin: bins)
{
size_t height = bin;
height = MAX_ASTERISK * (static_cast<double>(bin) / bin_max);
if (bin < 100)
{
cout << " ";
}
if (bin < 10)
{
cout << " ";
}
cout << bin << "|";
for (size_t i = 0; i < height; i++)
{
cout << "*";
}
cout << endl;
}
}
int main(){
const size_t SCREEN_WIDTH = 80;
const size_t MAX_ASTERISK = SCREEN_WIDTH - 3 - 1;
auto in = input_data();
auto bins = make_histogram(in.numbers, in.bin_count);
show_histogram_text(bins, MAX_ASTERISK, in.bin_count);
const std::vector<double> bins = make_histogram(in.numbers, in.bin_count);
show_histogram_svg(bins);
return 0;
}

Загрузка…
Отмена
Сохранить