code: разделение программы на файлы

master
GneushevaYM 1 месяц назад
Родитель 5951281347
Сommit 923ef8e23a

@ -32,6 +32,10 @@
<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" />
<Extensions>
<lib_finder disable_auto="1" />

@ -2,9 +2,19 @@
<CodeBlocks_layout_file>
<FileVersion major="1" minor="0" />
<ActiveTarget name="Debug" />
<File name="main.cpp" open="1" top="1" tabpos="1" split="0" active="1" splitpos="0" zoom_1="0" zoom_2="0">
<File name="main.cpp" open="1" top="0" tabpos="1" split="0" active="1" splitpos="0" zoom_1="0" zoom_2="0">
<Cursor>
<Cursor1 position="2137" topLine="0" />
<Cursor1 position="1733" topLine="0" />
</Cursor>
</File>
<File name="histogram.h" open="1" top="0" tabpos="3" split="0" active="1" splitpos="0" zoom_1="0" zoom_2="0">
<Cursor>
<Cursor1 position="0" topLine="0" />
</Cursor>
</File>
<File name="histogram.cpp" open="1" top="1" tabpos="2" split="0" active="1" splitpos="0" zoom_1="0" zoom_2="0">
<Cursor>
<Cursor1 position="0" topLine="0" />
</Cursor>
</File>
</CodeBlocks_layout_file>

@ -1,6 +1,6 @@
#include <iostream>
#include <vector>
#include <algorithm> // Äëÿ std::min è std::max (åñëè âû åùå íå âêëþ÷èëè)
#include "histogram.h"
using namespace std;
@ -28,73 +28,14 @@ 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;
}
if (x > max) {
max = x;
}
}
}
vector<size_t>
make_histogram(const vector<double>& numbers, size_t bin_count) {
double min, max;
find_minmax(numbers, min, max);
vector<size_t> bins(bin_count);
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;
}
void
show_histogram_text(const vector<size_t>& bins, size_t max_count, size_t MAX_ASTERISK) { // Óáðàëè bin_count ò.ê. åñòü bins.size()
for (size_t i = 0; i < bins.size(); i++) {
if (bins[i] < 10) {
cout << " " << bins[i] << "|";
} else if (bins[i] < 100) {
cout << " " << bins[i] << "|";
} else if (bins[i] < 1000) {
cout << bins[i] << "|";
}
}
size_t height;
if (max_count <= MAX_ASTERISK) {
height = bins[i];
} else {
height = MAX_ASTERISK * (static_cast<double>(bins[i]) / max_count);
}
for (size_t j = 0; j < height; j++) {
cout << "*";
}
cout << endl;
}
}
int
main() {
int main()
{
const size_t SCREEN_WIDTH = 80;
const size_t MAX_ASTERISK = SCREEN_WIDTH - 3 - 1;
@ -102,12 +43,12 @@ main() {
auto bins = make_histogram(in.numbers, in.bin_count);
size_t max_count = bins[0];
for (size_t i = 0; i < bins.size(); i++) { // Èñïðàâèëè öèêë íà bins.size()
for (size_t i = 0; i < bins.size(); i++) {
if (bins[i] > max_count) {
max_count = bins[i];
}
}
show_histogram_text(bins, max_count, MAX_ASTERISK);
show_histogram_text(bins, max_count, MAX_ASTERISK, in.bin_count);
return 0;
}

Загрузка…
Отмена
Сохранить