Сравнить коммиты
3 Коммитов
260247b455
...
3e3e24a882
Автор | SHA1 | Дата |
---|---|---|
![]() |
3e3e24a882 | 2 лет назад |
![]() |
6b8ddbef0c | 2 лет назад |
![]() |
3fe4023a61 | 2 лет назад |
@ -1,65 +1,29 @@
|
|||||||
|
#include "text.h"
|
||||||
|
#include "histogram.h"
|
||||||
|
#include "svg.h"
|
||||||
|
#include "histogram_internal.h"
|
||||||
#include <iostream>
|
#include <iostream>
|
||||||
#include <vector>
|
#include <vector>
|
||||||
using namespace std;
|
using namespace std;
|
||||||
|
struct Input {
|
||||||
int main()
|
vector<double> numbers;
|
||||||
|
size_t bin_count{};
|
||||||
|
};
|
||||||
|
Input input_data()
|
||||||
{
|
{
|
||||||
size_t num, po, i;
|
size_t number_count;
|
||||||
double max, min, m = 0, le, ri;
|
cin >> number_count;
|
||||||
cout << "num=";
|
Input in;
|
||||||
cin >> num;
|
in.numbers.resize(number_count);
|
||||||
cout << "po=";
|
for (size_t i = 0; i < number_count; i++) {
|
||||||
cin >> po;
|
cin >> in.numbers[i];
|
||||||
vector <double> mas(num);
|
|
||||||
vector<int> ch(po);
|
|
||||||
for (i = 0; i < num; i++)
|
|
||||||
{
|
|
||||||
cout << "mas [" << i << "]=";
|
|
||||||
cin >> mas[i];
|
|
||||||
}
|
|
||||||
cout << endl;
|
|
||||||
max = mas[0];
|
|
||||||
min = mas[0];
|
|
||||||
for (i = 1; i < num; i++)
|
|
||||||
{
|
|
||||||
if (max < mas[i])
|
|
||||||
max = mas[i];
|
|
||||||
if (min > mas[i])
|
|
||||||
min = mas[i];
|
|
||||||
}
|
|
||||||
le = min;
|
|
||||||
ri = le + (max - min) / po;
|
|
||||||
int kol = 0;
|
|
||||||
while (kol < po - 1)
|
|
||||||
{
|
|
||||||
for (i = 0; i < num; i++)
|
|
||||||
{
|
|
||||||
if (le <= mas[i] && ri > mas[i])
|
|
||||||
ch[kol]++;
|
|
||||||
}
|
|
||||||
if (ch[kol] > m)
|
|
||||||
m = ch[kol];
|
|
||||||
kol++;
|
|
||||||
le = ri;
|
|
||||||
ri = le + (max - min) / po;
|
|
||||||
}
|
|
||||||
for (i = 0; i < num; i++)
|
|
||||||
{
|
|
||||||
if ((ri <= mas[i] && le < mas[i]) || (le <= mas[i] && ri > mas[i]))
|
|
||||||
ch[kol] ++;
|
|
||||||
if (ch[kol] > m)
|
|
||||||
m = ch[kol];
|
|
||||||
}
|
}
|
||||||
for (i = 0; i < po; i++)
|
cin >> in.bin_count;
|
||||||
{
|
return in;
|
||||||
int d = m - ch[i];
|
}
|
||||||
if (d != 0)
|
int main()
|
||||||
for (int j = 0; j < d; j++)
|
{
|
||||||
cout << " ";
|
Input in = input_data();
|
||||||
for (size_t j = 0; j < ch[i]; j++)
|
auto ch = make_histogram(in.numbers, in.bin_count);
|
||||||
cout << "*";
|
show_histogram_svg(ch);
|
||||||
cout << "|" << ch[i];
|
|
||||||
cout << endl;
|
|
||||||
}
|
|
||||||
return 0;
|
|
||||||
}
|
}
|
@ -0,0 +1,43 @@
|
|||||||
|
#include "histogram.h"
|
||||||
|
#include "histogram_internal.h"
|
||||||
|
#include <iostream>
|
||||||
|
#include <vector>
|
||||||
|
using namespace std;
|
||||||
|
|
||||||
|
void find_minmax(const vector <double>& mas, double& min, double& max)
|
||||||
|
{
|
||||||
|
for (size_t i = 0; i < mas.size(); i++)
|
||||||
|
{
|
||||||
|
if (max < mas[i])
|
||||||
|
max = mas[i];
|
||||||
|
if (min > mas[i])
|
||||||
|
min = mas[i];
|
||||||
|
}
|
||||||
|
}
|
||||||
|
vector<int> make_histogram(const vector<double>& mas, size_t bin_count)
|
||||||
|
{
|
||||||
|
size_t i;
|
||||||
|
vector<int> ch(bin_count);
|
||||||
|
double max = mas[0], min = mas[0], le, ri;
|
||||||
|
find_minmax(mas, min, max);
|
||||||
|
le = min;
|
||||||
|
ri = le + (max - min) / bin_count;
|
||||||
|
int kol = 0;
|
||||||
|
while (kol < bin_count - 1)
|
||||||
|
{
|
||||||
|
for (i = 0; i < mas.size(); i++)
|
||||||
|
{
|
||||||
|
if (le <= mas[i] && ri > mas[i])
|
||||||
|
ch[kol]++;
|
||||||
|
}
|
||||||
|
kol++;
|
||||||
|
le = ri;
|
||||||
|
ri = le + (max - min) / bin_count;
|
||||||
|
}
|
||||||
|
for (i = 0; i < mas.size(); i++)
|
||||||
|
{
|
||||||
|
if ((ri <= mas[i] && le < mas[i]) || (le <= mas[i] && ri > mas[i]))
|
||||||
|
ch[kol] ++;
|
||||||
|
}
|
||||||
|
return ch;
|
||||||
|
}
|
@ -0,0 +1,7 @@
|
|||||||
|
#ifndef histogram
|
||||||
|
#define histogram
|
||||||
|
#include <vector>
|
||||||
|
|
||||||
|
std::vector<int> make_histogram(const std::vector<double>& mas, size_t bin_count);
|
||||||
|
|
||||||
|
#endif //histogram
|
@ -0,0 +1,7 @@
|
|||||||
|
#ifndef histogram_internal
|
||||||
|
#define histogram_internal
|
||||||
|
#include <vector>
|
||||||
|
|
||||||
|
void find_minmax(const std::vector <double>& mas, double& min, double& max);
|
||||||
|
|
||||||
|
#endif //histogram_internal
|
@ -0,0 +1,84 @@
|
|||||||
|
#include "svg.h"
|
||||||
|
#include <iostream>
|
||||||
|
#include <vector>
|
||||||
|
#include <string>
|
||||||
|
#include <math.h>
|
||||||
|
using namespace std;
|
||||||
|
|
||||||
|
void
|
||||||
|
svg_begin(double width, double height) {
|
||||||
|
cout << "<?xml version='1.0' encoding='UTF-8'?>\n";
|
||||||
|
cout << "<svg ";
|
||||||
|
cout << "width='" << width << "' ";
|
||||||
|
cout << "height='" << height << "' ";
|
||||||
|
cout << "viewBox='0 0 " << width << " " << height << "' ";
|
||||||
|
cout << "xmlns='http://www.w3.org/2000/svg'>\n";
|
||||||
|
}
|
||||||
|
|
||||||
|
void
|
||||||
|
svg_end() {
|
||||||
|
cout << "</svg>\n";
|
||||||
|
}
|
||||||
|
|
||||||
|
void
|
||||||
|
svg_text(double left, double baseline, string text) {
|
||||||
|
cout << "<text x='" << left << "' y='" << baseline << "'>" << text << "</text>";
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
|
void svg_rect(double x, double y, double width, double height, string stroke, string fill) {
|
||||||
|
cout << "<rect x='" << x << "' y='" << y << "' width='" << width << "' height='" << height << "' stroke = '" << stroke << "' fill = '" << fill << "' />";
|
||||||
|
}
|
||||||
|
|
||||||
|
void pers(int bin, int sum, int& x) {
|
||||||
|
x = round(bin * 100 / sum);
|
||||||
|
}
|
||||||
|
|
||||||
|
void
|
||||||
|
show_histogram_svg(const vector<int>& bins) {
|
||||||
|
|
||||||
|
const auto IMAGE_WIDTH = 400;
|
||||||
|
const auto IMAGE_HEIGHT = 300;
|
||||||
|
const auto TEXT_LEFT = 20;
|
||||||
|
const auto TEXT_BASELINE = 20;
|
||||||
|
const auto TEXT_WIDTH = 50;
|
||||||
|
const auto BIN_HEIGHT = 30;
|
||||||
|
const auto BLOCK_WIDTH = 10;
|
||||||
|
|
||||||
|
svg_begin(IMAGE_WIDTH, IMAGE_HEIGHT);
|
||||||
|
double top = 0;
|
||||||
|
const size_t MAX_ASTERISK = IMAGE_WIDTH - TEXT_WIDTH;
|
||||||
|
int maxb = bins[0], sum= bins[0];
|
||||||
|
for (size_t j = 1; j < bins.size(); j++)
|
||||||
|
{
|
||||||
|
if (bins[j] > maxb) maxb = bins[j];
|
||||||
|
sum += bins[j];
|
||||||
|
}
|
||||||
|
|
||||||
|
int maxb1 = maxb * BLOCK_WIDTH;
|
||||||
|
|
||||||
|
for (size_t bin : bins) {
|
||||||
|
|
||||||
|
double bin_width;
|
||||||
|
|
||||||
|
|
||||||
|
if (maxb1 > MAX_ASTERISK)
|
||||||
|
{
|
||||||
|
bin_width = BLOCK_WIDTH * MAX_ASTERISK * (bin / maxb1);
|
||||||
|
}
|
||||||
|
else bin_width = BLOCK_WIDTH * bin;
|
||||||
|
|
||||||
|
svg_text(TEXT_LEFT, top + TEXT_BASELINE, to_string(bin));
|
||||||
|
svg_rect(TEXT_WIDTH, top, bin_width, BIN_HEIGHT, "red", "#ffeeee");
|
||||||
|
int x = 0;
|
||||||
|
pers(bin, sum, x);
|
||||||
|
svg_text((TEXT_LEFT+ TEXT_WIDTH+ bin_width), top + TEXT_BASELINE, (to_string(x)+ " % "));
|
||||||
|
top += BIN_HEIGHT;
|
||||||
|
}
|
||||||
|
svg_end();
|
||||||
|
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
|
@ -0,0 +1,8 @@
|
|||||||
|
#ifndef svg
|
||||||
|
#define svg
|
||||||
|
#include <vector>
|
||||||
|
|
||||||
|
void pers(int bin, int sum, int& x);
|
||||||
|
void show_histogram_svg(const std::vector <int>& bins);
|
||||||
|
|
||||||
|
#endif //svg
|
@ -0,0 +1,26 @@
|
|||||||
|
#include "text.h"
|
||||||
|
#include <iostream>
|
||||||
|
#include <vector>
|
||||||
|
using namespace std;
|
||||||
|
|
||||||
|
void show_histogram_text(vector<int>& ch, size_t bin_count)
|
||||||
|
{
|
||||||
|
int m = 0, i;
|
||||||
|
for (i = 0; i < bin_count; i++)
|
||||||
|
if (ch[i] > m)
|
||||||
|
m = ch[i];
|
||||||
|
for (i = 0; i < bin_count; i++)
|
||||||
|
{
|
||||||
|
if (ch[i] < 10)
|
||||||
|
cout << " ";
|
||||||
|
else if (ch[i] < 100)
|
||||||
|
cout << " ";
|
||||||
|
cout << ch[i] << "|";
|
||||||
|
size_t k = ch[i];
|
||||||
|
if (m > 76)
|
||||||
|
k = 76 * (static_cast <double> (ch[i]) / m);
|
||||||
|
for (size_t j = 0; j < k; j++)
|
||||||
|
cout << "*";
|
||||||
|
cout << endl;
|
||||||
|
}
|
||||||
|
}
|
@ -0,0 +1,7 @@
|
|||||||
|
#ifndef text
|
||||||
|
#define text
|
||||||
|
#include <vector>
|
||||||
|
|
||||||
|
void show_histogram_text(std::vector<int>& ch);
|
||||||
|
|
||||||
|
#endif //text
|
Разница между файлами не показана из-за своего большого размера
Загрузить разницу
@ -0,0 +1,49 @@
|
|||||||
|
#define DOCTEST_CONFIG_NO_MULTITHREADING
|
||||||
|
#define DOCTEST_CONFIG_IMPLEMENT_WITH_MAIN
|
||||||
|
#include "doctest.h"
|
||||||
|
#include "C:\Users\User\Desktop\cs-lab03\histogram_internal.h"
|
||||||
|
#include "C:\Users\User\Desktop\cs-lab03\svg.h"
|
||||||
|
|
||||||
|
TEST_CASE("distinct positive numbers") {
|
||||||
|
double min = 0;
|
||||||
|
double max = 0;
|
||||||
|
find_minmax({ }, min, max);
|
||||||
|
CHECK(min == 0);
|
||||||
|
CHECK(max == 0);
|
||||||
|
}
|
||||||
|
|
||||||
|
TEST_CASE("distinct identical numbers") {
|
||||||
|
double min = 10000;
|
||||||
|
double max = 0;
|
||||||
|
find_minmax({ 5, 5 }, min, max);
|
||||||
|
CHECK(min == 5);
|
||||||
|
CHECK(max == 5);
|
||||||
|
}
|
||||||
|
|
||||||
|
TEST_CASE("distinct one number") {
|
||||||
|
double min = 10000;
|
||||||
|
double max = 0;
|
||||||
|
find_minmax({ 3 }, min, max);
|
||||||
|
CHECK(min == 3);
|
||||||
|
CHECK(max == 3);
|
||||||
|
}
|
||||||
|
|
||||||
|
TEST_CASE("distinct negative numbers") {
|
||||||
|
double min = 1000;
|
||||||
|
double max = -1000;
|
||||||
|
find_minmax({ -3, -1 }, min, max);
|
||||||
|
CHECK(min == -3);
|
||||||
|
CHECK(max == -1);
|
||||||
|
}
|
||||||
|
|
||||||
|
TEST_CASE("take percent") {
|
||||||
|
int x = 0;
|
||||||
|
pers(1, 20, x);
|
||||||
|
CHECK(x < 10);
|
||||||
|
}
|
||||||
|
|
||||||
|
TEST_CASE("take percent") {
|
||||||
|
int x = 0;
|
||||||
|
pers(5, 10, x);
|
||||||
|
CHECK(x == 50);
|
||||||
|
}
|
Загрузка…
Ссылка в новой задаче