Родитель
15dba8705d
Сommit
29099c717e
@ -0,0 +1,3 @@
|
|||||||
|
10
|
||||||
|
3 3 4 4 4 4 4 5 5 5
|
||||||
|
3
|
@ -0,0 +1,104 @@
|
|||||||
|
#include <iostream>
|
||||||
|
#include <vector>
|
||||||
|
#include <string>
|
||||||
|
#include "svg.h"
|
||||||
|
using namespace std;
|
||||||
|
|
||||||
|
const auto IMAGE_WIDTH = 400;
|
||||||
|
const auto IMAGE_HEIGHT = 700;
|
||||||
|
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;
|
||||||
|
const auto MAX_WIDTH = IMAGE_WIDTH - TEXT_WIDTH;
|
||||||
|
|
||||||
|
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 = "black", string fill = "aaaaaa") {
|
||||||
|
cout << "<rect x='" << x<< "' y='" << y<< "' width='" << width<< "' height='" << height<< "' stroke='" << stroke<< "' fill='" << fill << "' />\n";
|
||||||
|
}
|
||||||
|
|
||||||
|
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
|
||||||
|
show_histogram_svg(const vector<size_t>& bins) {
|
||||||
|
const auto MAX_WIDTH = IMAGE_WIDTH - TEXT_WIDTH;
|
||||||
|
size_t max_count = 0;
|
||||||
|
for (size_t x : bins) {
|
||||||
|
if (x > max_count) {
|
||||||
|
max_count = x;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
if (max_count == 0) {
|
||||||
|
max_count = 1;
|
||||||
|
}
|
||||||
|
auto scale_factor = static_cast<double>(MAX_WIDTH) / (max_count * BLOCK_WIDTH);
|
||||||
|
if (scale_factor > 1) {
|
||||||
|
scale_factor = 1;
|
||||||
|
}
|
||||||
|
svg_begin(400, 300);
|
||||||
|
double top = 0;
|
||||||
|
for (size_t bin : bins) {
|
||||||
|
double bin_width = BLOCK_WIDTH * bin * scale_factor;
|
||||||
|
svg_text(TEXT_LEFT, top + TEXT_BASELINE, to_string(bin));
|
||||||
|
svg_rect(TEXT_WIDTH, top, bin_width, BIN_HEIGHT);
|
||||||
|
top += BIN_HEIGHT;
|
||||||
|
}
|
||||||
|
svg_end();
|
||||||
|
}
|
||||||
|
|
||||||
|
void
|
||||||
|
svg_rect_hight(double x, double y, double width, double height, string stroke = "black", string fill = "hfhfh") {
|
||||||
|
cout << "<rect x='" << x<< "' y='" << y<< "' width='" << width<< "' height='" << height<< "' stroke='" << stroke<< "' fill='" << fill << "' />\n";
|
||||||
|
}
|
||||||
|
|
||||||
|
void
|
||||||
|
COLUMN_hight_function( double &COLUMN_height ,size_t bin_count,const vector<size_t>& bins){
|
||||||
|
const auto MAX_WIDTH = IMAGE_WIDTH - TEXT_WIDTH;
|
||||||
|
size_t max_count = 0;
|
||||||
|
for (size_t x : bins) {
|
||||||
|
if (x > max_count) {
|
||||||
|
max_count = x;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
if (max_count == 0) {
|
||||||
|
max_count = 1;
|
||||||
|
}
|
||||||
|
auto scale_factor = static_cast<double>(MAX_WIDTH) / (max_count * BLOCK_WIDTH);
|
||||||
|
if (scale_factor > 1) {
|
||||||
|
scale_factor = 1;
|
||||||
|
}
|
||||||
|
svg_begin(IMAGE_WIDTH, IMAGE_HEIGHT );
|
||||||
|
if ((bin_count*COLUMN_height) > IMAGE_HEIGHT ){
|
||||||
|
|
||||||
|
COLUMN_height= static_cast<double>(IMAGE_HEIGHT) / bin_count;
|
||||||
|
|
||||||
|
}
|
||||||
|
double top = 0;
|
||||||
|
for (size_t bin : bins) {
|
||||||
|
double bin_width = BLOCK_WIDTH * bin * scale_factor;
|
||||||
|
svg_text(TEXT_LEFT, top + TEXT_BASELINE, to_string(bin));
|
||||||
|
svg_rect(TEXT_WIDTH, top, bin_width, COLUMN_height);
|
||||||
|
top += COLUMN_height;
|
||||||
|
}
|
||||||
|
svg_end();
|
||||||
|
|
||||||
|
}
|
@ -0,0 +1,10 @@
|
|||||||
|
#ifndef SVG_H_INCLUDED
|
||||||
|
#define SVG_H_INCLUDED
|
||||||
|
|
||||||
|
#include <vector>
|
||||||
|
|
||||||
|
void
|
||||||
|
show_histogram_svg(const std::vector<size_t>& bins);
|
||||||
|
void
|
||||||
|
COLUMN_hight_function( double &COLUMN_height ,size_t bin_count,const std::vector<size_t>& bins);
|
||||||
|
#endif // SVG_H_INCLUDED
|
@ -0,0 +1,46 @@
|
|||||||
|
#define DOCTEST_CONFIG_NO_MULTITHREADING
|
||||||
|
#define DOCTEST_CONFIG_IMPLEMENT_WITH_MAIN
|
||||||
|
#include "doctest.h"
|
||||||
|
#include "histogram_internal.h"
|
||||||
|
#include "svg.h"
|
||||||
|
|
||||||
|
TEST_CASE("distinct positive numbers") {
|
||||||
|
double min = 0;
|
||||||
|
double max = 0;
|
||||||
|
find_minmax({1, 2}, min, max);
|
||||||
|
CHECK(min == 1);
|
||||||
|
CHECK(max == 2);
|
||||||
|
}
|
||||||
|
TEST_CASE("distinct negative numbers") {
|
||||||
|
double min = -1;
|
||||||
|
double max = -1;
|
||||||
|
find_minmax({-1, -2}, min, max);
|
||||||
|
CHECK(min == -2);
|
||||||
|
CHECK(max == -1);
|
||||||
|
}
|
||||||
|
TEST_CASE("one number") {
|
||||||
|
double min = 0;
|
||||||
|
double max = 0;
|
||||||
|
find_minmax({1}, min, max);
|
||||||
|
CHECK(min == 1);
|
||||||
|
CHECK(max == 1);
|
||||||
|
}
|
||||||
|
TEST_CASE("same numbers") {
|
||||||
|
double min = 0;
|
||||||
|
double max = 0;
|
||||||
|
find_minmax({2, 2}, min, max);
|
||||||
|
CHECK(min == 2);
|
||||||
|
CHECK(max == 2);
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
TEST_CASE("distinct positive numbers") {
|
||||||
|
double COLUMN_height = 400;
|
||||||
|
COLUMN_hight_function(COLUMN_height ,5,{23,5,54,6,7});
|
||||||
|
CHECK(COLUMN_height == 150 );
|
||||||
|
|
||||||
|
}
|
||||||
|
|
@ -0,0 +1,71 @@
|
|||||||
|
# depslib dependency file v1.0
|
||||||
|
1716799438 source:c:\users\lenov\desktop\lab03\histogram.cpp
|
||||||
|
"histogram.h"
|
||||||
|
<vector>
|
||||||
|
<iostream>
|
||||||
|
|
||||||
|
1716797551 c:\users\lenov\desktop\lab03\histogram.h
|
||||||
|
<vector>
|
||||||
|
|
||||||
|
1716819750 source:c:\users\lenov\desktop\lab03\unittest.cpp
|
||||||
|
"doctest.h"
|
||||||
|
"histogram_internal.h"
|
||||||
|
"svg.h"
|
||||||
|
|
||||||
|
1716800315 c:\users\lenov\desktop\lab03\doctest.h
|
||||||
|
<signal.h>
|
||||||
|
<ciso646>
|
||||||
|
<cstddef>
|
||||||
|
<ostream>
|
||||||
|
<istream>
|
||||||
|
<type_traits>
|
||||||
|
"doctest_fwd.h"
|
||||||
|
<ctime>
|
||||||
|
<cmath>
|
||||||
|
<climits>
|
||||||
|
<math.h>
|
||||||
|
<new>
|
||||||
|
<cstdio>
|
||||||
|
<cstdlib>
|
||||||
|
<cstring>
|
||||||
|
<limits>
|
||||||
|
<utility>
|
||||||
|
<fstream>
|
||||||
|
<sstream>
|
||||||
|
<iostream>
|
||||||
|
<algorithm>
|
||||||
|
<iomanip>
|
||||||
|
<vector>
|
||||||
|
<atomic>
|
||||||
|
<mutex>
|
||||||
|
<set>
|
||||||
|
<map>
|
||||||
|
<unordered_set>
|
||||||
|
<exception>
|
||||||
|
<stdexcept>
|
||||||
|
<csignal>
|
||||||
|
<cfloat>
|
||||||
|
<cctype>
|
||||||
|
<cstdint>
|
||||||
|
<string>
|
||||||
|
<sys/types.h>
|
||||||
|
<unistd.h>
|
||||||
|
<sys/sysctl.h>
|
||||||
|
<AfxWin.h>
|
||||||
|
<windows.h>
|
||||||
|
<io.h>
|
||||||
|
<sys/time.h>
|
||||||
|
<unistd.h>
|
||||||
|
|
||||||
|
1716799551 c:\users\lenov\desktop\lab03\histogram_internal.h
|
||||||
|
<vector>
|
||||||
|
|
||||||
|
1716819750 source:c:\users\lenov\desktop\lab03\svg.cpp
|
||||||
|
<iostream>
|
||||||
|
<vector>
|
||||||
|
<string>
|
||||||
|
"svg.h"
|
||||||
|
|
||||||
|
1716819918 c:\users\lenov\desktop\lab03\svg.h
|
||||||
|
<vector>
|
||||||
|
|
Загрузка…
Ссылка в новой задаче