code: код разделен на файлы
Этот коммит содержится в:
44
histogram.cpp
Обычный файл
44
histogram.cpp
Обычный файл
@@ -0,0 +1,44 @@
|
|||||||
|
#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){
|
||||||
|
size_t number_count = numbers.size();
|
||||||
|
vector<size_t> bins(bin_count);
|
||||||
|
double min, max;
|
||||||
|
find_minmax(numbers, min, max);
|
||||||
|
|
||||||
|
double bin_size = (max - min) / bin_count;
|
||||||
|
|
||||||
|
for (size_t i = 0; i < number_count; 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;
|
||||||
|
}
|
||||||
5
histogram.h
Обычный файл
5
histogram.h
Обычный файл
@@ -0,0 +1,5 @@
|
|||||||
|
#pragma once
|
||||||
|
#include <vector>
|
||||||
|
|
||||||
|
std::vector<size_t>
|
||||||
|
make_histogram(const std::vector<double>& numbers, size_t bin_count);
|
||||||
94
main.cpp
94
main.cpp
@@ -1,5 +1,8 @@
|
|||||||
#include <iostream>
|
#include <iostream>
|
||||||
#include <vector>
|
#include <vector>
|
||||||
|
#include "histogram.h"
|
||||||
|
#include "text.h"
|
||||||
|
|
||||||
using namespace std;
|
using namespace std;
|
||||||
|
|
||||||
struct Input {
|
struct Input {
|
||||||
@@ -23,97 +26,6 @@ input_data() {
|
|||||||
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){
|
|
||||||
size_t number_count = numbers.size();
|
|
||||||
vector<size_t> bins(bin_count);
|
|
||||||
double min, max;
|
|
||||||
find_minmax(numbers, min, max);
|
|
||||||
|
|
||||||
double bin_size = (max - min) / bin_count;
|
|
||||||
|
|
||||||
for (size_t i = 0; i < number_count; 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 - 3 - 1;
|
|
||||||
|
|
||||||
auto bin_count = bins.size();
|
|
||||||
|
|
||||||
size_t max_count = 0;
|
|
||||||
for (size_t i = 0; i < bin_count; i++){
|
|
||||||
size_t Count = bins[i];
|
|
||||||
if (max_count < Count){
|
|
||||||
max_count = Count;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
if (max_count <= MAX_ASTERISK){
|
|
||||||
for (size_t i = 0; i < bin_count; i++){
|
|
||||||
size_t Count = bins[i];
|
|
||||||
if (Count < 100){
|
|
||||||
cout << " ";
|
|
||||||
}
|
|
||||||
if (Count < 10){
|
|
||||||
cout << " ";
|
|
||||||
}
|
|
||||||
cout << Count << "|";
|
|
||||||
for (size_t j = 0; j < Count; j++){
|
|
||||||
cout << "*";
|
|
||||||
}
|
|
||||||
cout << "\n";
|
|
||||||
}
|
|
||||||
}
|
|
||||||
else{
|
|
||||||
for (size_t i = 0; i < bin_count; i++){
|
|
||||||
size_t Count = bins[i];
|
|
||||||
size_t height = 76 * (static_cast<double>(Count) / max_count);
|
|
||||||
if (Count < 100){
|
|
||||||
cout << " ";
|
|
||||||
}
|
|
||||||
if (Count < 10){
|
|
||||||
cout << " ";
|
|
||||||
}
|
|
||||||
cout << Count << "|";
|
|
||||||
for (size_t j = 0; j < height; j++){
|
|
||||||
cout << "*";
|
|
||||||
}
|
|
||||||
cout << "\n";
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
|
|
||||||
int
|
int
|
||||||
main() {
|
main() {
|
||||||
auto in = input_data();
|
auto in = input_data();
|
||||||
|
|||||||
53
text.cpp
Обычный файл
53
text.cpp
Обычный файл
@@ -0,0 +1,53 @@
|
|||||||
|
#include "text.h"
|
||||||
|
#include <iostream>
|
||||||
|
|
||||||
|
using namespace std;
|
||||||
|
|
||||||
|
void
|
||||||
|
show_histogram_text(const vector<size_t>& bins){
|
||||||
|
const size_t SCREEN_WIDTH = 80;
|
||||||
|
const size_t MAX_ASTERISK = SCREEN_WIDTH - 3 - 1;
|
||||||
|
|
||||||
|
auto bin_count = bins.size();
|
||||||
|
|
||||||
|
size_t max_count = 0;
|
||||||
|
for (size_t i = 0; i < bin_count; i++){
|
||||||
|
size_t Count = bins[i];
|
||||||
|
if (max_count < Count){
|
||||||
|
max_count = Count;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
if (max_count <= MAX_ASTERISK){
|
||||||
|
for (size_t i = 0; i < bin_count; i++){
|
||||||
|
size_t Count = bins[i];
|
||||||
|
if (Count < 100){
|
||||||
|
cout << " ";
|
||||||
|
}
|
||||||
|
if (Count < 10){
|
||||||
|
cout << " ";
|
||||||
|
}
|
||||||
|
cout << Count << "|";
|
||||||
|
for (size_t j = 0; j < Count; j++){
|
||||||
|
cout << "*";
|
||||||
|
}
|
||||||
|
cout << "\n";
|
||||||
|
}
|
||||||
|
}
|
||||||
|
else{
|
||||||
|
for (size_t i = 0; i < bin_count; i++){
|
||||||
|
size_t Count = bins[i];
|
||||||
|
size_t height = 76 * (static_cast<double>(Count) / max_count);
|
||||||
|
if (Count < 100){
|
||||||
|
cout << " ";
|
||||||
|
}
|
||||||
|
if (Count < 10){
|
||||||
|
cout << " ";
|
||||||
|
}
|
||||||
|
cout << Count << "|";
|
||||||
|
for (size_t j = 0; j < height; j++){
|
||||||
|
cout << "*";
|
||||||
|
}
|
||||||
|
cout << "\n";
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
5
text.h
Обычный файл
5
text.h
Обычный файл
@@ -0,0 +1,5 @@
|
|||||||
|
#pragma once
|
||||||
|
#include <vector>
|
||||||
|
|
||||||
|
void
|
||||||
|
show_histogram_text(const std::vector<size_t>& bins);
|
||||||
Ссылка в новой задаче
Block a user