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

@ -3,37 +3,40 @@
#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) {
{
bool notEmpty = true;
if (numbers.size() == 0 || numbers.size() == 1)
notEmpty = false;
else{
min = numbers[0]; min = numbers[0];
max = numbers[0]; max = numbers[0];
for (size_t i = 1; i < numbers.size(); i++) for (size_t i = 1; i < numbers.size(); i++) {
{
if (min > numbers[i]) if (min > numbers[i])
min = numbers[i]; min = numbers[i];
if (max < numbers[i]) if (max < numbers[i])
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);
if (haveElements){
double binSize = (max - min) / bin_count; double binSize = (max - min) / bin_count;
vector<size_t> bins(bin_count); vector<size_t> bins(bin_count);
for (size_t i = 0; i < numbers.size(); i++) 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]++; bins[j]++;
found = true; found = true;
} }
@ -41,20 +44,16 @@ vector <size_t> make_histogramm(vector<double>numbers, size_t bin_count)
if (!found) if (!found)
bins[bin_count - 1]++; bins[bin_count - 1]++;
} }
int max_count = bins[0];
for (size_t i = 0; i < bin_count; i++)
{
if (bins[i] > max_count)
max_count = bins[i];
}
if (max_count > 76)
{
for (size_t i = 0; i < bin_count; i++)
{
int count = bins[i];
size_t height = 76 * (static_cast<double>(count) / max_count);
bins[i] = height;
}
}
return bins; return bins;
}
else
cerr << "Empty massive of numbers";
}
bool check_interval(size_t interval){
bool need;
if (interval > 9 || interval < 2)
need = false;
else
need = true;
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){
if (prompt)
cerr << "Input numbers count: "; 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);
if (prompt)
cerr << "Input numbers: "; cerr << "Input numbers: ";
for (size_t i = 0; i < number_count; i++) for (size_t i = 0; i < number_count; i++) {
{ tin >> in.numbers[i];
cin >> in.numbers[i];
} }
if (prompt)
cerr << "Input bin count: "; cerr << "Input bin count: ";
cin >> in.bin_count; tin >> in.bin_count;
return in; return in;
} }
int main() int main() {
{ Input in = input_data(cin, false);
Input in = input_data();
auto bins = make_histogramm(in.numbers, in.bin_count); auto bins = make_histogramm(in.numbers, in.bin_count);
show_histogram_svg(bins);
show_histogramm(bins);
return 0; return 0;
} }

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