разделение программы на файлы
Этот коммит содержится в:
45
histogram.cpp
Обычный файл
45
histogram.cpp
Обычный файл
@@ -0,0 +1,45 @@
|
|||||||
|
#include "histogram.h"
|
||||||
|
#include <iostream>
|
||||||
|
#include <vector>
|
||||||
|
using namespace std;
|
||||||
|
static void find_minmax(const vector<double>& numbers, double& minN, double& maxN){
|
||||||
|
minN = numbers[0];
|
||||||
|
maxN = numbers[0];
|
||||||
|
for (double x : numbers){
|
||||||
|
if (x < minN){
|
||||||
|
minN = x;
|
||||||
|
}
|
||||||
|
else if (x > maxN) {
|
||||||
|
maxN = x;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
vector<size_t> make_histogram(const vector<double>& numbers, size_t bin_count){
|
||||||
|
double minN, maxN;
|
||||||
|
find_minmax(numbers, minN, maxN);
|
||||||
|
vector <size_t> bins(bin_count);
|
||||||
|
double diff = (maxN - minN) / bin_count;
|
||||||
|
size_t max_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 * diff;
|
||||||
|
auto hi = minN + (j + 1) * diff;
|
||||||
|
if ((lo <= numbers[i]) && (hi > numbers[i])){
|
||||||
|
bins[j]++;
|
||||||
|
if (bins[j] > max_count){
|
||||||
|
max_count = bins[j];
|
||||||
|
}
|
||||||
|
found = true;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
if(!found){
|
||||||
|
bins[bin_count - 1]++;
|
||||||
|
if (bins[bin_count - 1] > max_count){
|
||||||
|
max_count = bins[bin_count - 1];
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return bins;
|
||||||
|
}
|
||||||
10
histogram.h
Обычный файл
10
histogram.h
Обычный файл
@@ -0,0 +1,10 @@
|
|||||||
|
#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
|
||||||
79
main.cpp
79
main.cpp
@@ -1,5 +1,7 @@
|
|||||||
#include <iostream>
|
#include <iostream>
|
||||||
#include <vector>
|
#include <vector>
|
||||||
|
#include "histogram.h"
|
||||||
|
#include "text.h"
|
||||||
using namespace std;
|
using namespace std;
|
||||||
|
|
||||||
struct Input{
|
struct Input{
|
||||||
@@ -22,83 +24,6 @@ input_data()
|
|||||||
return in;
|
return in;
|
||||||
}
|
}
|
||||||
|
|
||||||
void find_minmax(const vector<double>& numbers, double& minN, double& maxN){
|
|
||||||
minN = numbers[0];
|
|
||||||
maxN = numbers[0];
|
|
||||||
for (double x : numbers){
|
|
||||||
if (x < minN){
|
|
||||||
minN = x;
|
|
||||||
}
|
|
||||||
else if (x > maxN) {
|
|
||||||
maxN = x;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
vector<size_t> make_histogram(const vector<double>& numbers, size_t bin_count){
|
|
||||||
double minN, maxN;
|
|
||||||
find_minmax(numbers, minN, maxN);
|
|
||||||
vector <size_t> bins(bin_count);
|
|
||||||
double diff = (maxN - minN) / bin_count;
|
|
||||||
size_t max_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 * diff;
|
|
||||||
auto hi = minN + (j + 1) * diff;
|
|
||||||
if ((lo <= numbers[i]) && (hi > numbers[i])){
|
|
||||||
bins[j]++;
|
|
||||||
if (bins[j] > max_count){
|
|
||||||
max_count = bins[j];
|
|
||||||
}
|
|
||||||
found = true;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
if(!found){
|
|
||||||
bins[bin_count - 1]++;
|
|
||||||
if (bins[bin_count - 1] > max_count){
|
|
||||||
max_count = bins[bin_count - 1];
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
return bins;
|
|
||||||
}
|
|
||||||
|
|
||||||
void show_histogram_text(const vector <size_t>& bins,size_t bin_count, size_t max_count){
|
|
||||||
const size_t SCREEN_WIDTH = 80;
|
|
||||||
const size_t MAX_ASTERISK = SCREEN_WIDTH - 3 - 1;
|
|
||||||
bool scaling = false;
|
|
||||||
if (max_count > MAX_ASTERISK){
|
|
||||||
scaling = true;
|
|
||||||
}
|
|
||||||
for (size_t i = 0; i < bin_count; i++){
|
|
||||||
if (bins[i] < 10){
|
|
||||||
cout << " ";
|
|
||||||
}
|
|
||||||
else if (bins[i] < 100){
|
|
||||||
cout << " ";
|
|
||||||
}
|
|
||||||
else{
|
|
||||||
cout << "";
|
|
||||||
}
|
|
||||||
cout << bins[i] << '|';
|
|
||||||
size_t number_of_stars = bins[i];
|
|
||||||
if (scaling){
|
|
||||||
if (bins[i] == max_count){
|
|
||||||
number_of_stars = MAX_ASTERISK * 1.0;
|
|
||||||
}
|
|
||||||
else{
|
|
||||||
number_of_stars = MAX_ASTERISK * (static_cast<double>(bins[i]) / max_count);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
for (size_t j = 0; j < number_of_stars; j++){
|
|
||||||
cout << '*';
|
|
||||||
}
|
|
||||||
cout << endl;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
int main()
|
int main()
|
||||||
{
|
{
|
||||||
size_t max_count;
|
size_t max_count;
|
||||||
|
|||||||
38
text.cpp
Обычный файл
38
text.cpp
Обычный файл
@@ -0,0 +1,38 @@
|
|||||||
|
#include "text.h"
|
||||||
|
#include <iostream>
|
||||||
|
#include <vector>
|
||||||
|
using namespace std;
|
||||||
|
void show_histogram_text(const vector <size_t>& bins,size_t bin_count, size_t max_count){
|
||||||
|
const size_t SCREEN_WIDTH = 80;
|
||||||
|
const size_t MAX_ASTERISK = SCREEN_WIDTH - 3 - 1;
|
||||||
|
bool scaling = false;
|
||||||
|
if (max_count > MAX_ASTERISK){
|
||||||
|
scaling = true;
|
||||||
|
}
|
||||||
|
for (size_t i = 0; i < bin_count; i++){
|
||||||
|
if (bins[i] < 10){
|
||||||
|
cout << " ";
|
||||||
|
}
|
||||||
|
else if (bins[i] < 100){
|
||||||
|
cout << " ";
|
||||||
|
}
|
||||||
|
else{
|
||||||
|
cout << "";
|
||||||
|
}
|
||||||
|
cout << bins[i] << '|';
|
||||||
|
size_t number_of_stars = bins[i];
|
||||||
|
if (scaling){
|
||||||
|
if (bins[i] == max_count){
|
||||||
|
number_of_stars = MAX_ASTERISK * 1.0;
|
||||||
|
}
|
||||||
|
else{
|
||||||
|
number_of_stars = MAX_ASTERISK * (static_cast<double>(bins[i]) / max_count);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
for (size_t j = 0; j < number_of_stars; j++){
|
||||||
|
cout << '*';
|
||||||
|
}
|
||||||
|
cout << endl;
|
||||||
|
}
|
||||||
|
}
|
||||||
7
text.h
Обычный файл
7
text.h
Обычный файл
@@ -0,0 +1,7 @@
|
|||||||
|
#ifndef TEXT_H_INCLUDED
|
||||||
|
#define TEXT_H_INCLUDED
|
||||||
|
|
||||||
|
#include <vector>
|
||||||
|
void show_histogram_text(const std::vector <size_t>& bins,size_t bin_count, size_t max_count);
|
||||||
|
|
||||||
|
#endif // TEXT_H_INCLUDED
|
||||||
Ссылка в новой задаче
Block a user