Сравнить коммиты
2 Коммитов
92a689e777
...
9ec235110a
| Автор | SHA1 | Дата | |
|---|---|---|---|
| 9ec235110a | |||
| bed2d1d21c |
40
lab01.cbp
Обычный файл
40
lab01.cbp
Обычный файл
@@ -0,0 +1,40 @@
|
||||
<?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>
|
||||
<lib_finder disable_auto="1" />
|
||||
</Extensions>
|
||||
</Project>
|
||||
</CodeBlocks_project_file>
|
||||
70
main.cpp
70
main.cpp
@@ -3,25 +3,29 @@
|
||||
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 numbers: ";
|
||||
struct Input {
|
||||
vector<double> numbers;
|
||||
size_t bin_count{};
|
||||
};
|
||||
|
||||
Input
|
||||
input_data() {
|
||||
size_t number_count;
|
||||
cin >> number_count;
|
||||
Input in;
|
||||
in.numbers.resize(number_count);
|
||||
for (size_t i = 0; i < number_count; i++) {
|
||||
cin >> numbers[i];
|
||||
cin >> in.numbers[i];
|
||||
}
|
||||
cin >> in.bin_count;
|
||||
return in;
|
||||
}
|
||||
|
||||
size_t bin_count;
|
||||
cerr << "Enter bin count: ";
|
||||
cin >> bin_count;
|
||||
|
||||
double min = numbers[0];
|
||||
double max = numbers[0];
|
||||
/*
|
||||
void
|
||||
find_minmax(const vector<double>& numbers, double& min, double& max) {
|
||||
min = numbers[0];
|
||||
max = numbers[0];
|
||||
for (size_t x : numbers) {
|
||||
if (x < min) {
|
||||
min = x;
|
||||
@@ -30,18 +34,15 @@ int main()
|
||||
max = x;
|
||||
}
|
||||
}
|
||||
*/
|
||||
for (size_t i=1; i<number_count; i++){
|
||||
if (numbers[i]<min){
|
||||
min=numbers[i];
|
||||
}
|
||||
else if (numbers[i]>max){
|
||||
max=numbers[i];
|
||||
}
|
||||
// (çäåñü êîä ïîèñêà ìèíèìóìà è ìàêñèìóìà)
|
||||
}
|
||||
|
||||
vector<size_t> make_histogram(const vector<double>& numbers, size_t bin_count){
|
||||
vector<size_t> bins(bin_count);
|
||||
double min, max;
|
||||
find_minmax(numbers, min, max);
|
||||
double bin_size = (max-min)/bin_count;
|
||||
for (size_t i = 0; i < number_count; i++) {
|
||||
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;
|
||||
@@ -54,26 +55,30 @@ int main()
|
||||
if (!found) {
|
||||
bins[bin_count-1]++;
|
||||
}
|
||||
|
||||
}
|
||||
return bins;
|
||||
}
|
||||
|
||||
void
|
||||
show_histogram_text(vector<size_t> bins){
|
||||
size_t max_count = bins[0];
|
||||
for(size_t x: bins){
|
||||
if(x > max_count){
|
||||
max_count = x;
|
||||
}
|
||||
}
|
||||
vector<size_t> heights(bin_count);
|
||||
vector<size_t> heights(bins.size());
|
||||
if (max_count>MAX_ASTERISK){
|
||||
for (size_t i = 0; i < bin_count; i++){
|
||||
for (size_t i = 0; i < bins.size(); i++){
|
||||
heights[i] = MAX_ASTERISK * (static_cast<double>(bins[i]) /max_count);
|
||||
}
|
||||
}
|
||||
else{
|
||||
for (size_t i=0; i<bin_count; i++){
|
||||
for (size_t i=0; i<bins.size(); i++){
|
||||
heights[i] = bins[i];
|
||||
}
|
||||
}
|
||||
for(size_t i=0; i<bin_count; i++){
|
||||
for(size_t i=0; i<bins.size(); i++){
|
||||
if (bins[i] < 10){
|
||||
cout << " " << bins[i] << "|";
|
||||
}
|
||||
@@ -88,5 +93,12 @@ int main()
|
||||
}
|
||||
cout<<"\n";
|
||||
}
|
||||
}
|
||||
|
||||
int
|
||||
main(){
|
||||
auto in = input_data();
|
||||
auto bins = make_histogram(in.numbers, in.bin_count);
|
||||
show_histogram_text(bins);
|
||||
return 0;
|
||||
}
|
||||
|
||||
Ссылка в новой задаче
Block a user