Yaroslav Spesivtsev 16 часов назад
Родитель 921a966a37
Сommit 95a128b320

@ -27,83 +27,9 @@ Input input_data() {
using namespace std; using namespace std;
int main() int main() {
{ Input in = input_data();
const size_t SCREEN_WIDTH = 80; auto bins = make_histogram(in.numbers, in.bin_count);
const size_t MAX_ASTERISK = SCREEN_WIDTH - 3 - 1; show_histogram_text(bins);
int number_count, bucket;
cerr << "Enter number count: "; cin >> number_count;
std::cerr << "Enter numbers: \n";
vector <double> numbers(number_count);
for (int i = 0; i < number_count; i++) cin >> numbers[i];
float min = numbers[0];
float max = numbers[0];
for (float x : numbers)
{
if (x < min) min = x;
else if (x > max) max = x;
}
if (max == min) {// ïðîâåðêà íà àíîìàëèþ
std::cout << " Unable to create a histogram." << std::endl;
return 1;
}
cerr << "Enter bucket: "; cin >> bucket;
float k = (max - min) / bucket;
if (numbers.empty()) {//áûëè ëè ââåäåíû ÷èñëà
std::cout << "Empty. unable to create histogram." << std::endl;
return 1;
}
vector <int> stolb(bucket);
vector <int> proz(stolb);
for (int j = 0; j < bucket; j++) stolb[j] = 0;
for (int i = 0; i < number_count; i++)
{
bool flag = false;
for (int j = 0; (j < bucket && !flag); j++)
{
if (numbers[i] >= (min + k * j) && numbers[i] < (min + k * (1 + j)))
{
stolb[j]++;
flag = true;
}
}
if (!flag) stolb[bucket - 1]++;
}
int maxlen = 0;
for (int j = 0; j < bucket; j++)
{
if (maxlen < stolb[j]) maxlen = stolb[j];
}
for (int j = 0; j < bucket; j++)
{
if (stolb[j] < 100) cout << " ";
if (stolb[j] < 10) cout << "";
cout << stolb[j] << " |";
size_t height = stolb[j];
if (maxlen > MAX_ASTERISK)
{
if (maxlen != stolb[j]) height = MAX_ASTERISK * (static_cast <float>(stolb[j]) / maxlen);
else if (maxlen == stolb[j]) height = MAX_ASTERISK;
}
for (int i = 0; i < height; i++) cout << "*";
cout << "\n";
}
return 0; return 0;
} }

@ -31,6 +31,9 @@
<Compiler> <Compiler>
<Add option="-Wall" /> <Add option="-Wall" />
</Compiler> </Compiler>
<Unit filename="Histogram/histogram.cpp" />
<Unit filename="Histogram/histogram_internal.h" />
<Unit filename="unittest.cpp" />
<Extensions> <Extensions>
<lib_finder disable_auto="1" /> <lib_finder disable_auto="1" />
</Extensions> </Extensions>

@ -2,4 +2,9 @@
<CodeBlocks_layout_file> <CodeBlocks_layout_file>
<FileVersion major="1" minor="0" /> <FileVersion major="1" minor="0" />
<ActiveTarget name="Debug" /> <ActiveTarget name="Debug" />
<File name="unittest.cpp" open="1" top="1" tabpos="1" split="0" active="1" splitpos="0" zoom_1="0" zoom_2="0">
<Cursor>
<Cursor1 position="271" topLine="0" />
</Cursor>
</File>
</CodeBlocks_layout_file> </CodeBlocks_layout_file>

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