build: 3. Разделение программы на файлы. Код разнесен на три файла, добавлены заголовочные файлы

main
AkinshinaDA 1 месяц назад
Родитель 07ea6cf513
Сommit 20bcfffcc5

@ -0,0 +1,40 @@
#include <vector>
#include <math.h>
#include <iostream>
#include <conio.h>
#include "histogram.h"
using namespace std;
static 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<size_t> make_histogram(const vector<double>& numbers, size_t& bin_count){
double min, max;
find_minmax(numbers, min, max);
vector<size_t> bins(bin_count);
double bin_size = (max - min) / bin_count;
size_t number_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;
}

@ -0,0 +1,7 @@
#ifndef HISTOGRAM_H_INCLUDED
#define HISTOGRAM_H_INCLUDED
#include <vector>
std::vector<size_t> make_histogram(const std::vector<double>& numbers, size_t& bin_count);
#endif // HISTOGRAM_H_INCLUDED

@ -2,6 +2,8 @@
#include <math.h> #include <math.h>
#include <iostream> #include <iostream>
#include <conio.h> #include <conio.h>
#include "histogram.h"
#include "text.h"
using namespace std; using namespace std;
struct Input { struct Input {
vector<double> numbers; vector<double> numbers;
@ -19,80 +21,6 @@ input_data(){
cin >> in.bin_count; cin >> in.bin_count;
return in; 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<size_t> make_histogram(const vector<double>& numbers, size_t& bin_count){
double min, max;
find_minmax(numbers, min, max);
vector<size_t> bins(bin_count);
double bin_size = (max - min) / bin_count;
size_t number_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(size_t& bin_count, vector<size_t>& bins){
vector<size_t> counts(bin_count);
for (size_t i = 0; i < bin_count; i++){
for (size_t j = 0; j < bins[i]; j++){
counts[i]++;
}
}
size_t max_count = counts[0];
for (size_t i=0; i<bin_count; i++){
if(counts[i]>max_count){
max_count = counts[i];
}
}
const size_t screen_width = 80;
const size_t max_asterisk = screen_width - 3 - 1;
vector<size_t> heights(bin_count);
size_t height;
for (size_t i=0; i<bin_count; i++){
if (max_count > 76){
height = (max_asterisk*(static_cast<double>(counts[i])/max_count));
}
else {
height = counts[i];
}
heights[i] = height;
}
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 < heights[i]; j++){
cout << "*";
}
cout << "\n";
}
}
int main(){ int main(){
auto in = input_data(); auto in = input_data();
auto bins = make_histogram(in.numbers, in.bin_count); auto bins = make_histogram(in.numbers, in.bin_count);

@ -0,0 +1,46 @@
#include <vector>
#include <math.h>
#include <iostream>
#include <conio.h>
#include "text.h"
using namespace std;
void show_histogram_text(size_t& bin_count, vector<size_t>& bins){
vector<size_t> counts(bin_count);
for (size_t i = 0; i < bin_count; i++){
for (size_t j = 0; j < bins[i]; j++){
counts[i]++;
}
}
size_t max_count = counts[0];
for (size_t i=0; i<bin_count; i++){
if(counts[i]>max_count){
max_count = counts[i];
}
}
const size_t screen_width = 80;
const size_t max_asterisk = screen_width - 3 - 1;
vector<size_t> heights(bin_count);
size_t height;
for (size_t i=0; i<bin_count; i++){
if (max_count > 76){
height = (max_asterisk*(static_cast<double>(counts[i])/max_count));
}
else {
height = counts[i];
}
heights[i] = height;
}
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 < heights[i]; j++){
cout << "*";
}
cout << "\n";
}
}

@ -0,0 +1,7 @@
#ifndef TEXT_H_INCLUDED
#define TEXT_H_INCLUDED
#include <vector>
void show_histogram_text(size_t& bin_count, std::vector<size_t>& bins);
#endif // TEXT_H_INCLUDED
Загрузка…
Отмена
Сохранить