Сравнить коммиты
2 Коммитов
da62cf2928
...
26780b22e7
Автор | SHA1 | Дата |
---|---|---|
![]() |
26780b22e7 | 6 месяцев назад |
![]() |
51dd8806bd | 6 месяцев назад |
@ -0,0 +1,5 @@
|
|||||||
|
/bin
|
||||||
|
/obj
|
||||||
|
lab34.depend
|
||||||
|
lab34.layout
|
||||||
|
main.exe
|
@ -0,0 +1,51 @@
|
|||||||
|
#include "histogram.h"
|
||||||
|
|
||||||
|
vector <size_t>
|
||||||
|
make_histogram(const vector<double>& numbers, size_t & bin_count, size_t & number_count, size_t & max_count) {
|
||||||
|
double min, max;
|
||||||
|
find_minmax(numbers, min, max);
|
||||||
|
double bin_size = (max - min) / bin_count;
|
||||||
|
vector<size_t> bins(bin_count);
|
||||||
|
max_count = bins[0];
|
||||||
|
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 (bins[j] > max_count){
|
||||||
|
max_count = bins[j];
|
||||||
|
}
|
||||||
|
}
|
||||||
|
if (!found){
|
||||||
|
bins[bin_count - 1]++;
|
||||||
|
}
|
||||||
|
if (bins[bin_count - 1] > max_count){
|
||||||
|
max_count = bins[bin_count - 1];
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return bins;
|
||||||
|
}
|
||||||
|
|
||||||
|
void
|
||||||
|
find_minmax(const vector<double>& numbers, double& min, double& max) {
|
||||||
|
if (!numbers.empty()) {
|
||||||
|
min = numbers[0];
|
||||||
|
max = numbers[0];
|
||||||
|
} else {
|
||||||
|
min = 0;
|
||||||
|
max = 0;
|
||||||
|
}
|
||||||
|
for (double x : numbers)
|
||||||
|
{
|
||||||
|
if (x < min){
|
||||||
|
min = x;
|
||||||
|
}
|
||||||
|
else if (x > max){
|
||||||
|
max = x;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
@ -0,0 +1,14 @@
|
|||||||
|
#ifndef HISTOGRAM_H_INCLUDED
|
||||||
|
#define HISTOGRAM_H_INCLUDED
|
||||||
|
|
||||||
|
#include <vector>
|
||||||
|
|
||||||
|
using namespace std;
|
||||||
|
|
||||||
|
vector<size_t>
|
||||||
|
make_histogram(const vector<double>& numbers, size_t & bin_count, size_t & number_count, size_t & max_count);
|
||||||
|
|
||||||
|
void
|
||||||
|
find_minmax(const vector<double>& numbers, double& min, double& max);
|
||||||
|
|
||||||
|
#endif // HISTOGRAM_H_INCLUDED
|
@ -1,64 +1,42 @@
|
|||||||
#include <iostream>
|
#include <iostream>
|
||||||
#include <vector>
|
#include <vector>
|
||||||
|
#include "histogram.h"
|
||||||
|
#include "text.h"
|
||||||
using namespace std;
|
using namespace std;
|
||||||
|
|
||||||
int main()
|
struct Input {
|
||||||
{
|
vector<double> numbers;
|
||||||
const size_t SCREEN_WIDTH = 80;
|
size_t bin_count{};
|
||||||
const size_t MAX_ASTERISK = SCREEN_WIDTH - 3 - 1;
|
size_t number_count{};
|
||||||
|
size_t max_count{};
|
||||||
|
};
|
||||||
|
|
||||||
size_t number_count, bin_count, i, bin;
|
Input
|
||||||
cerr << "Enter number count: " << endl;
|
input_data() {
|
||||||
cin>>number_count;
|
Input in;
|
||||||
vector<double> numbers(number_count);
|
cerr << "Enter number count: ";
|
||||||
cerr<< "Enter numbers: "<<endl;
|
cin >> in.number_count;
|
||||||
for (size_t i = 0; i < number_count; i++){
|
|
||||||
cin>>numbers[i];
|
vector<double> numbers(in.number_count);
|
||||||
}
|
in.numbers.resize(in.number_count);
|
||||||
double min = numbers[0];
|
for (size_t i = 0; i < in.number_count; i++) {
|
||||||
double max = numbers[0];
|
cin >> in.numbers[i];
|
||||||
for (double x : numbers) {
|
|
||||||
if (x < min) {
|
|
||||||
min = x;
|
|
||||||
}
|
|
||||||
else if (x > max) {
|
|
||||||
max = x;
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
cerr<< "Enter bin count: "<<endl;
|
size_t bin_count;
|
||||||
cin>>bin_count;
|
cerr << "Enter bin count: ";
|
||||||
vector<size_t> bins(bin_count);
|
cin >> in.bin_count;
|
||||||
double bin_size = (max - min) / bin_count;
|
|
||||||
|
size_t max_count;
|
||||||
|
in.max_count = 0;
|
||||||
|
return in;
|
||||||
|
}
|
||||||
|
|
||||||
|
int main()
|
||||||
|
{
|
||||||
|
Input in = input_data();
|
||||||
|
vector<size_t> bins = make_histogram(in.numbers, in.bin_count, in.number_count, in.max_count);
|
||||||
|
show_histogram_text(bins, in.max_count, in.bin_count);
|
||||||
|
return 0;
|
||||||
|
|
||||||
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]++;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
size_t height = 0;
|
|
||||||
size_t max_count = 0;
|
|
||||||
for (i = 0; i < bin_count; i++){
|
|
||||||
if (bins[i] > max_count) max_count = bins[i];
|
|
||||||
}
|
|
||||||
for(size_t bin:bins){
|
|
||||||
size_t height = bin;
|
|
||||||
height = MAX_ASTERISK * (static_cast<double>(bin) / max_count);
|
|
||||||
if(bin<100) cout<<" ";
|
|
||||||
if(bin<10) cout<<" ";
|
|
||||||
cout<<bin<<"|";
|
|
||||||
for(size_t i = 0; i < height; i++){
|
|
||||||
cout<<"*";
|
|
||||||
}
|
|
||||||
cout<<endl;
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
@ -0,0 +1,35 @@
|
|||||||
|
#include "text.h"
|
||||||
|
#include <cstdio>
|
||||||
|
#include <iostream>
|
||||||
|
using namespace std;
|
||||||
|
void
|
||||||
|
show_histogram_text(vector<size_t>& bins, size_t & max_count,size_t & bin_count) {
|
||||||
|
const size_t SCREEN_WIDTH = 80;
|
||||||
|
const size_t MAX_ASTERISK = SCREEN_WIDTH - 3 - 1;
|
||||||
|
if (max_count > MAX_ASTERISK){
|
||||||
|
vector<size_t> hights(bin_count);
|
||||||
|
for (size_t i = 0; i < bin_count; i++){
|
||||||
|
size_t height = MAX_ASTERISK * (static_cast<double>(bins[i]) / max_count);
|
||||||
|
hights[i] = height;
|
||||||
|
}
|
||||||
|
|
||||||
|
for (size_t i = 0; i < bin_count; i++){
|
||||||
|
printf("%3d|", bins[i]);
|
||||||
|
for (size_t j = 0; j < hights[i]; j++){
|
||||||
|
cout<<"*";
|
||||||
|
}
|
||||||
|
cout << endl;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
else{
|
||||||
|
for (size_t i = 0; i < bin_count; i++)
|
||||||
|
{
|
||||||
|
printf("%3d|", bins[i]);
|
||||||
|
for (size_t j = 0; j < bins[i]; j++){
|
||||||
|
cout<<"*";
|
||||||
|
}
|
||||||
|
cout << endl;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
@ -0,0 +1,11 @@
|
|||||||
|
#ifndef SHOW-SVG_H_INCLUDED
|
||||||
|
#define SHOW-SVG_H_INCLUDED
|
||||||
|
|
||||||
|
#include <vector>
|
||||||
|
|
||||||
|
using namespace std;
|
||||||
|
|
||||||
|
void
|
||||||
|
show_histogram_text(vector<size_t>& bins, size_t & max_count,size_t & bin_count);
|
||||||
|
|
||||||
|
#endif // SHOW-SVG_H_INCLUDED
|
Загрузка…
Ссылка в новой задаче