Сommit
778de65823
@ -0,0 +1,40 @@
|
|||||||
|
<?xml version="1.0" encoding="UTF-8" standalone="yes" ?>
|
||||||
|
<CodeBlocks_project_file>
|
||||||
|
<FileVersion major="1" minor="6" />
|
||||||
|
<Project>
|
||||||
|
<Option title="lab1" />
|
||||||
|
<Option pch_mode="2" />
|
||||||
|
<Option compiler="gcc" />
|
||||||
|
<Build>
|
||||||
|
<Target title="Debug">
|
||||||
|
<Option output="bin/Debug/lab1" prefix_auto="1" extension_auto="1" />
|
||||||
|
<Option object_output="obj/Debug/" />
|
||||||
|
<Option type="1" />
|
||||||
|
<Option compiler="gcc" />
|
||||||
|
<Compiler>
|
||||||
|
<Add option="-g" />
|
||||||
|
</Compiler>
|
||||||
|
</Target>
|
||||||
|
<Target title="Release">
|
||||||
|
<Option output="bin/Release/lab1" prefix_auto="1" extension_auto="1" />
|
||||||
|
<Option object_output="obj/Release/" />
|
||||||
|
<Option type="1" />
|
||||||
|
<Option compiler="gcc" />
|
||||||
|
<Compiler>
|
||||||
|
<Add option="-O2" />
|
||||||
|
</Compiler>
|
||||||
|
<Linker>
|
||||||
|
<Add option="-s" />
|
||||||
|
</Linker>
|
||||||
|
</Target>
|
||||||
|
</Build>
|
||||||
|
<Compiler>
|
||||||
|
<Add option="-Wall" />
|
||||||
|
<Add option="-fexceptions" />
|
||||||
|
</Compiler>
|
||||||
|
<Unit filename="main.cpp" />
|
||||||
|
<Extensions>
|
||||||
|
<lib_finder disable_auto="1" />
|
||||||
|
</Extensions>
|
||||||
|
</Project>
|
||||||
|
</CodeBlocks_project_file>
|
@ -0,0 +1,113 @@
|
|||||||
|
#include <iostream>
|
||||||
|
#include <vector>
|
||||||
|
#include <limits>
|
||||||
|
|
||||||
|
using namespace std;
|
||||||
|
|
||||||
|
const size_t SCREEN_WIDTH = 80;
|
||||||
|
const size_t MAX_ASTERISK = SCREEN_WIDTH - 3 - 1;
|
||||||
|
|
||||||
|
int main() {
|
||||||
|
|
||||||
|
size_t number_count;
|
||||||
|
cerr << "Enter number count: ";
|
||||||
|
cin >> number_count;
|
||||||
|
|
||||||
|
|
||||||
|
vector<double> numbers(number_count);
|
||||||
|
cerr << "Enter " << number_count << " numbers: ";
|
||||||
|
for (size_t i = 0; i < number_count; i++) {
|
||||||
|
cin >> numbers[i];
|
||||||
|
}
|
||||||
|
|
||||||
|
size_t bin_count;
|
||||||
|
cerr << "Enter bin count: ";
|
||||||
|
cin >> bin_count;
|
||||||
|
|
||||||
|
|
||||||
|
double min = numeric_limits<double>::max();
|
||||||
|
double max = numeric_limits<double>::lowest();
|
||||||
|
|
||||||
|
for (double x : numbers) {
|
||||||
|
if (x < min) {
|
||||||
|
min = x;
|
||||||
|
}
|
||||||
|
if (x > max) {
|
||||||
|
max = x;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
double bin_size = (max - min) / bin_count;
|
||||||
|
|
||||||
|
|
||||||
|
vector<size_t> bins(bin_count, 0);
|
||||||
|
|
||||||
|
|
||||||
|
for (size_t i = 0; i < numbers.size(); i++) {
|
||||||
|
bool found = false;
|
||||||
|
for (size_t j = 0; (j < bin_count - 1) && !found; j++) {
|
||||||
|
double lo = min + j * bin_size;
|
||||||
|
double 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 max_count = 0;
|
||||||
|
for (size_t count : bins) {
|
||||||
|
if (count > max_count) {
|
||||||
|
max_count = count;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
cerr << "Bin counts:" << endl;
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
size_t height;
|
||||||
|
for (size_t j = 0; j < bin_count; j++) {
|
||||||
|
// Âû÷èñëÿåì âûñîòó ãèñòîãðàììû
|
||||||
|
if(max_count > 76){
|
||||||
|
height = (max_count > 0) ? static_cast<size_t>(76 * (static_cast<double>(bins[j]) / max_count)) : 0;
|
||||||
|
}
|
||||||
|
else{
|
||||||
|
height = bins[j];
|
||||||
|
}
|
||||||
|
// Âûâîäèì êîëè÷åñòâî â áèíå ñ ó÷åòîì ôîðìàòèðîâàíèÿ
|
||||||
|
if (bins[j] < 10) {
|
||||||
|
cout << " " << bins[j] << "|";
|
||||||
|
} else if (bins[j] < 100) {
|
||||||
|
cout << " " << bins[j] << "|";
|
||||||
|
} else {
|
||||||
|
cout << bins[j] << "|";
|
||||||
|
}
|
||||||
|
|
||||||
|
// Âûâîäèì ñèìâîëû '*' â ñîîòâåòñòâèè ñ âûñîòîé
|
||||||
|
for (size_t k = 0; k < height; k++) {
|
||||||
|
cout << "*";
|
||||||
|
}
|
||||||
|
cout << endl;
|
||||||
|
}
|
||||||
|
|
||||||
|
return 0;
|
||||||
|
}
|
||||||
|
|
||||||
|
|
Загрузка…
Ссылка в новой задаче