code: добавление кода ЛР 1

main
Ivan (BeloziorovIA) 2 недель назад
Сommit 0ab81c952d

3
.gitignore поставляемый

@ -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="JIa6a_3" />
<Option pch_mode="2" />
<Option compiler="gcc" />
<Build>
<Target title="Debug">
<Option output="bin/Debug/JIa6a_3" 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/JIa6a_3" 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,96 @@
#include <cstdio>
#include <cstdlib>
#include <iostream>
#include <vector>
#include <cmath>
using namespace std;
int main() {
const size_t SCREEN_WIDTH = 80;
const size_t MAX_ASTERISK = SCREEN_WIDTH - 3 - 3;
size_t number_count, bin_count;
//Ââîä êîëè÷åñòâà ýëåìåíòîâ ìàññèâà
cerr << "Enter number count ";
cin >> number_count;
//Ââîä ìàññèâà
vector<double> Numbers(number_count);
cerr << "Enter array:\n";
cin >> Numbers[0];
for (int i = 1; i < number_count; i++) {
cin >> Numbers[i];
}
//Ïîèñê ìàêñèìóìà
double max = Numbers[0];
for (int i = 1; i < number_count; i++) {
if (Numbers[i] > max)
max = Numbers[i];
}
//Ïîèñê ìèíèìóìà
double min = Numbers[0];
for (int i = 1; i < number_count; i++) {
if (Numbers[i] < min)
min = Numbers[i];
}
//Ââîä êîëè÷åñòâà êîðçèí
cerr << "Enter bin count\n";
cin >> bin_count;
//Îïðåäåëåíèå øàãà ìåæäó êîðçèíàìè
double bin_size = (max - min) / bin_count;
//Ìàññèâ êîðçèí
vector<size_t> bins(bin_count);
for (size_t i = 0; i < bin_count; i++) // îáíóëåíèå êîðçèí
bins[i] = 0;
for (size_t i = 0; i < bin_count; i++) {
double lo = min + i * bin_size;
double hi = min + (i + 1) * bin_size;
for (size_t j = 0; j < number_count; j++) {
if (lo <= Numbers[j] && Numbers[j] <= hi)
bins[i]++;
}
}
//Ïðîâåðêà íåîáõîäèìîñòè ìàñøàòáèðîâàíèÿ
size_t max4scale = 0;
for (size_t x : bins){
if (x > max4scale){
max4scale = x;
}
}
if (max4scale > MAX_ASTERISK){
for (size_t x : bins) {
if (x >= 100){
cout << x << " | ";
}
else if (x >= 10){
cout << " " << x << " | ";
}
else{
cout << " " << x << " | ";
}
size_t count = x;
size_t height = MAX_ASTERISK * (static_cast<double>(count) / max4scale);
for (size_t i = 0; i < height; i++) {
cout << "*";
}
cout << "\n";
}
}
else{
//Âûâîä
for (size_t x : bins) {
if (x >= 100){
cout << x << " | ";
}
else if (x >= 10){
cout << " " << x << " | ";
}
else{
cout << " " << x << " | ";
}
for (size_t i = 0; i < x; i++) {
cout << "*";
}
cout << "\n";
}
}
return 0;
}
Загрузка…
Отмена
Сохранить