Сommit
23ef1a4d47
@ -0,0 +1,3 @@
|
||||
bin
|
||||
obj
|
||||
*.layout
|
@ -0,0 +1,38 @@
|
||||
<?xml version="1.0" encoding="UTF-8" standalone="yes" ?>
|
||||
<CodeBlocks_project_file>
|
||||
<FileVersion major="1" minor="6" />
|
||||
<Project>
|
||||
<Option title="lab01" />
|
||||
<Option pch_mode="2" />
|
||||
<Option compiler="gcc" />
|
||||
<Build>
|
||||
<Target title="Debug">
|
||||
<Option output="bin/Debug/lab01" 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/lab01" 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 />
|
||||
</Project>
|
||||
</CodeBlocks_project_file>
|
@ -0,0 +1,167 @@
|
||||
/*Ïîñëå ââîäà êîëè÷åñòâà ÷èñåë ïðåäëàãàéòå ïîëüçîâàòåëþ ãåíåðèðîâàòü èõ.
|
||||
Ïðè ïîëîæèòåëüíîì îòâåòå çàïîëíèòå èñõîäíûé ìàññèâ ïðè ïîìîùè ôóíêöèè rand(): êàæäûé ýëåìåíò äîëæåí áûòü ñóììîé 12 åå ðåçóëüòàòîâ.
|
||||
|
||||
Óêàçàíèå.  íà÷àëå ïðîãðàììû äîáàâüòå srand(time(0)), ÷òîáû ñëó÷àéíûå ÷èñëà îòëè÷àëèñü ìåæäó çàïóñêàìè ïðîãðàììû (àíàëîã Randomize() â Pascal).
|
||||
Äëÿ ñîñòàâëåíèÿ ýòàëîííîãî âûâîäà çàìåíèòå time(0) íà 42.*/
|
||||
#include <time.h>
|
||||
#include <iostream>
|
||||
#include <vector>
|
||||
#include <cstdlib>
|
||||
using namespace std;
|
||||
|
||||
int main()
|
||||
{
|
||||
|
||||
//# Ïåðåìåííûå + íà÷àëî ïðîãðàììû(ââîäû)
|
||||
const size_t SCREEN_WIDTH = 80;
|
||||
const size_t MAX_ASTERISK = SCREEN_WIDTH - 3 - 1;
|
||||
|
||||
size_t number_count, bin_count, max_count = 0;
|
||||
|
||||
cerr << "Enter number count: ";
|
||||
cin >> number_count;
|
||||
|
||||
vector<double> numbers(number_count);
|
||||
|
||||
char gen_num;
|
||||
|
||||
cerr << "randoma want? (y/n): ";
|
||||
cin >> gen_num;
|
||||
|
||||
if (gen_num == 'y')
|
||||
{
|
||||
//srand(42);
|
||||
srand(time(0));
|
||||
|
||||
for (int i = 0; i != number_count; i++)
|
||||
{
|
||||
double sum = 0;
|
||||
for (int j = 0; j < 12; j++)
|
||||
{
|
||||
sum += rand();
|
||||
}
|
||||
numbers[i] = sum;
|
||||
}
|
||||
}
|
||||
else
|
||||
{
|
||||
for (int i = 0; i != number_count; i++)
|
||||
{
|
||||
cerr << "Input number \n";
|
||||
cin >> numbers[i];
|
||||
}
|
||||
}
|
||||
|
||||
cerr << "korzinki? ";
|
||||
cin >> bin_count;
|
||||
vector<double> 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;
|
||||
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 = 76 * (static_cast<double>(bin_count) / max_count);
|
||||
|
||||
for (int i = 0; i != bin_count; i++)
|
||||
{
|
||||
if (bins[i] > max_count)
|
||||
{
|
||||
max_count = bins[i];
|
||||
}
|
||||
}
|
||||
|
||||
if (max_count > 76)
|
||||
{
|
||||
for (int i = 0; i != bin_count; i++)
|
||||
{
|
||||
if (bins[i] < 100 && bins[i] > 10)
|
||||
{
|
||||
cout << " " << bins[i] << "|";
|
||||
}
|
||||
else if (bins[i] < 10)
|
||||
{
|
||||
cout << " " << bins[i] << "|";
|
||||
}
|
||||
else
|
||||
{
|
||||
cout << bins[i] << "|";
|
||||
}
|
||||
|
||||
if (bins[i] != max_count)
|
||||
{
|
||||
height = 76 * (bins[i] / max_count);
|
||||
for (int j = 0; j != height; j++)
|
||||
{
|
||||
cout << "*";
|
||||
}
|
||||
cout << "\n";
|
||||
}
|
||||
else
|
||||
{
|
||||
height = 76 * 1.0;
|
||||
for (int j = 0; j != height; j++)
|
||||
{
|
||||
cout << "*";
|
||||
}
|
||||
cout << "\n";
|
||||
}
|
||||
}
|
||||
}
|
||||
//-----
|
||||
else
|
||||
{
|
||||
for (int i = 0; i != bin_count; i++)
|
||||
{
|
||||
if (bins[i] < 100 && bins[i] > 10)
|
||||
{
|
||||
cout << " " << bins[i] << "|";
|
||||
}
|
||||
else if (bins[i] < 10)
|
||||
{
|
||||
cout << " " << bins[i] << "|";
|
||||
}
|
||||
else
|
||||
{
|
||||
cout << bins[i] << "|";
|
||||
}
|
||||
|
||||
for (int j = 0; j != bins[i]; j++)
|
||||
{
|
||||
cout << "*";
|
||||
}
|
||||
cout << "\n";
|
||||
}
|
||||
}
|
||||
}
|
||||
// | 3 3 5 5 5 4 4 4 4 4
|
Загрузка…
Ссылка в новой задаче