NemykinNO 2 лет назад
Родитель 1d08c45c2b
Сommit 0a7ae92249

@ -3,58 +3,57 @@
#include <conio.h> #include <conio.h>
#include <vector> #include <vector>
#include "histogram.h" #include "histogram.h"
#include "histogram_internal.h"
using namespace std; using namespace std;
void bool
find_minmax(vector<double> numbers, double &min, double &max) find_minmax(vector<double> numbers, double &min, double &max) {
{
min = numbers[0]; bool notEmpty = true;
max = numbers[0]; if (numbers.size() == 0 || numbers.size() == 1)
for (size_t i = 1; i < numbers.size(); i++) notEmpty = false;
{ else{
if (min > numbers[i]) min = numbers[0];
min = numbers[i]; max = numbers[0];
if (max < numbers[i]) for (size_t i = 1; i < numbers.size(); i++) {
max = numbers[i]; if (min > numbers[i])
min = numbers[i];
if (max < numbers[i])
max = numbers[i];
} }
}
return notEmpty;
} }
vector <size_t> make_histogramm(vector<double>numbers, size_t bin_count) vector <size_t> make_histogramm(vector<double>numbers, size_t bin_count){
{
double min, max; double min, max;
find_minmax(numbers, min, max); bool haveElements = find_minmax(numbers, min, max);
double binSize = (max - min) / bin_count; if (haveElements){
vector<size_t> bins(bin_count); double binSize = (max - min) / bin_count;
for (size_t i = 0; i < numbers.size(); i++) vector<size_t> bins(bin_count);
{ for (size_t i = 0; i < numbers.size(); i++) {
bool found = false; bool found = false;
for (size_t j = 0; (j <= bin_count - 1) && !found; j++) for (size_t j = 0; (j <= bin_count - 1) && !found; j++) {
{ auto lo = min + j * binSize;
auto lo = min + j * binSize; auto hi = min + (j + 1) * binSize;
auto hi = min + (j + 1) * binSize; if ((numbers[i] >= lo) && (numbers[i] < hi)) {
if ((numbers[i] >= lo) && (numbers[i] < hi)) bins[j]++;
{ found = true;
bins[j]++;
found = true;
} }
} }
if (!found) if (!found)
bins[bin_count - 1]++; bins[bin_count - 1]++;
} }
int max_count = bins[0]; return bins;
for (size_t i = 0; i < bin_count; i++)
{
if (bins[i] > max_count)
max_count = bins[i];
} }
if (max_count > 76) else
{ cerr << "Empty massive of numbers";
for (size_t i = 0; i < bin_count; i++) }
{ bool check_interval(size_t interval){
int count = bins[i]; bool need;
size_t height = 76 * (static_cast<double>(count) / max_count); if (interval > 9 || interval < 2)
bins[i] = height; need = false;
} else
} need = true;
return bins; return need;
} }

@ -3,6 +3,6 @@
#include <vector> #include <vector>
void find_minmax(std::vector<double> numbers, double &min, double &max); bool find_minmax(std::vector<double> numbers, double &min, double &max);
#endif // HISTOGRAM_INTERNAL_H_INCLUDED #endif // HISTOGRAM_INTERNAL_H_INCLUDED

@ -14,6 +14,11 @@
<Compiler> <Compiler>
<Add option="-g" /> <Add option="-g" />
</Compiler> </Compiler>
<Linker>
<Add option="-static-libstdc++" />
<Add option="-static-libgcc" />
<Add option="-static" />
</Linker>
</Target> </Target>
<Target title="Release"> <Target title="Release">
<Option output="bin/Release/lab3" prefix_auto="1" extension_auto="1" /> <Option output="bin/Release/lab3" prefix_auto="1" extension_auto="1" />
@ -37,6 +42,8 @@
<Unit filename="histogram.h" /> <Unit filename="histogram.h" />
<Unit filename="histogram_internal.h" /> <Unit filename="histogram_internal.h" />
<Unit filename="main.cpp" /> <Unit filename="main.cpp" />
<Unit filename="svg.cpp" />
<Unit filename="svg.h" />
<Unit filename="text.cpp" /> <Unit filename="text.cpp" />
<Unit filename="text.h" /> <Unit filename="text.h" />
<Extensions> <Extensions>

@ -1,50 +1,42 @@
#include <vector>
#include <math.h> #include <math.h>
#include <iostream> #include <iostream>
#include <conio.h> #include <conio.h>
#include <fstream> #include <vector>
#include "histogram.h" #include "histogram.h"
#include "text.h" #include "text.h"
#include "svg.h"
#include "histogram_internal.h"
using namespace std; using namespace std;
const size_t SCREEN_WIDTH = 80; struct Input {
const size_t MAX_ASTERISK = SCREEN_WIDTH - 3 - 1;
struct Input
{
vector<double> numbers; vector<double> numbers;
size_t bin_count{}; size_t bin_count{};
size_t interval;
}; };
Input Input
input_data(){ input_data(istream &tin, bool prompt){
cerr << "Input numbers count: "; if (prompt)
cerr << "Input numbers count: ";
size_t number_count; size_t number_count;
cin >> number_count; tin >> number_count;
Input in; Input in;
in.numbers.resize(number_count); in.numbers.resize(number_count);
cerr << "Input numbers: "; if (prompt)
for (size_t i = 0; i < number_count; i++) cerr << "Input numbers: ";
{ for (size_t i = 0; i < number_count; i++) {
cin >> in.numbers[i]; tin >> in.numbers[i];
} }
cerr << "Input bin count: "; if (prompt)
cin >> in.bin_count; cerr << "Input bin count: ";
tin >> in.bin_count;
return in; return in;
} }
int main() int main() {
{ Input in = input_data(cin, false);
auto bins = make_histogramm(in.numbers, in.bin_count);
Input in = input_data(); show_histogram_svg(bins);
return 0;
auto bins = make_histogramm(in.numbers, in.bin_count);
show_histogramm(bins);
return 0;
} }

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