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

..

3 Коммитов

@ -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;
}

@ -0,0 +1,5 @@
#pragma once
#include <vector>
std::vector<size_t>
make_histogram(const std::vector<double>& numbers, size_t bin_count);

@ -33,7 +33,15 @@
<Add option="-fexceptions" />
</Compiler>
<Unit filename=".gitignore" />
<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>
<lib_finder disable_auto="1" />
</Extensions>

@ -1,5 +1,8 @@
#include <iostream>
#include <vector>
#include "histogram.h"
#include "text.h"
using namespace std;
struct Input {
@ -23,97 +26,6 @@ input_data() {
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
main() {
auto in = input_data();

@ -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";
}
}
}

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