Сравнить коммиты

..

8 Коммитов

1
.gitignore поставляемый

@ -1,3 +1,4 @@
/bin
/obj
/project.layout
/project.depend

@ -0,0 +1,39 @@
#include "histogram.h"
using namespace std;
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) {
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 < 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,8 @@
#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

@ -1,82 +1,33 @@
#include <iostream>
#include <vector>
#include "histogram.h"
#include "text.h"
using namespace std;
int main() {
//êîíñòàíòû äëÿ ìàñøòàáèðîâàíèÿ
const size_t SCREEN_WIDTH = 80;
const size_t MAX_ASTERISK = SCREEN_WIDTH - 3 - 1;
struct Input {
vector<double> numbers;
size_t bin_count{};
};
Input input_data() {
//ââîä êîëè÷åñòâà ÷èñåë
size_t number_count;
cerr << "Enter number count: ";
cin >> number_count;
//ïåðåìåííàÿ-ýêçåìïëÿð
Input in;
//ââîä ÷èñåë
vector<double> numbers(number_count);
for (size_t i = 0;i<number_count;i++) {
cin >> numbers[i];
in.numbers.resize(number_count);
for (size_t i = 0 ; i < number_count ; i++) {
cin >> in.numbers[i];
}
//ââîä êîëè÷åñòâà êîðçèí
size_t bin_count;
cerr << "Enter bin count: ";
cin >> bin_count;
//îïðåäåëåíèå íàèáîëüøåãî è ìèíèìàëüíîãî ÷èñëà
vector<size_t> bins(bin_count);
double min = numbers[0];
double max = numbers[0];
for (double x : numbers) {
if (x < min) {
min = x;
}
else if (x > max) {
max = x;
}
}
//ðàçíèöà ìåæäó ãðàíèöàìè
double bin_size = (max - min) / bin_count;
//ðàñïðåäåëåíèå ÷èñåë ïî êîðçèíàì è ïîèñê ìàêñèìàëüíîãî êîëè÷åñòâà ÷èñåë â êîðçèíå
size_t max_count = 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]++;
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];
}
}
}
//âûâîä
for (size_t i = 0;i < bin_count;i++){
if (bins[i] < 100){
cout << " ";
}
if (bins[i] < 10){
cout << " ";
}
cout << bins[i] << "|";
if (max_count > MAX_ASTERISK){
size_t height = MAX_ASTERISK * (static_cast<double>(bins[i]) / max_count);
for (size_t j = 0;j < height;j++){
cout << "*";
}
}
else{
for (size_t j = 0;j < bins[i];j++){
cout << "*";
}
}
cout << endl;
}
return 0;
cin >> in.bin_count;
return in;
}
int main() {
auto in = input_data();
auto bins = make_histogram(in.numbers, in.bin_count);
show_histogram_text(bins);
}

@ -32,7 +32,15 @@
<Add option="-Wall" />
<Add option="-fexceptions" />
</Compiler>
<Unit filename="histogram.cpp" />
<Unit filename="histogram.h">
<Option target="&lt;{~None~}&gt;" />
</Unit>
<Unit filename="main.cpp" />
<Unit filename="text.cpp" />
<Unit filename="text.h">
<Option target="&lt;{~None~}&gt;" />
</Unit>
<Extensions />
</Project>
</CodeBlocks_project_file>

@ -0,0 +1,38 @@
#include "text.h"
using namespace std;
void show_histogram_text(const vector<size_t> &bins){
//ïîèñê ìàêñèìàëüíîãî êîëè÷åñòâà ÷èñåë â êîðçèíå
size_t max_count = 0;
for (double x : bins){
if (x > max_count){
max_count = x;
}
}
//êîíñòàíòû äëÿ ìàñøòàáèðîâàíèÿ
const size_t SCREEN_WIDTH = 80;
const size_t MAX_ASTERISK = SCREEN_WIDTH - 3 - 1;
//âûâîä
for (size_t i = 0;i < bins.size();i++){
if (bins[i] < 100){
cout << " ";
}
if (bins[i] < 10){
cout << " ";
}
cout << bins[i] << "|";
if (max_count > MAX_ASTERISK){
size_t height = MAX_ASTERISK * (static_cast<double>(bins[i]) / max_count);
for (size_t j = 0;j < height;j++){
cout << "*";
}
}
else{
for (size_t j = 0;j < bins[i];j++){
cout << "*";
}
}
cout << endl;
}
}

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