build: разделение на файлы

main
RybakovaSA 2 месяцев назад
Родитель b97f8123e9
Сommit eff0c6a12c

@ -0,0 +1,8 @@
//
// histogram.cpp
// lab01
//
// Created by Светлана Рыбакова on 01.05.2025.
//
#include <stdio.h>

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

@ -0,0 +1,8 @@
//
// text.cpp
// lab01
//
// Created by Светлана Рыбакова on 01.05.2025.
//
#include <stdio.h>

@ -0,0 +1,12 @@
//
// text.h
// lab01
//
// Created by Светлана Рыбакова on 01.05.2025.
//
#ifndef text_h
#define text_h
#endif /* text_h */
Загрузка…
Отмена
Сохранить