main
KalenskovaDA 4 дней назад
Родитель 133ecfbbb6
Сommit d11a01d781

@ -2,19 +2,21 @@
#include "histogram_internal.h" #include "histogram_internal.h"
#include <vector> #include <vector>
using std::vector; using std::vector;
void find_minmax(const std::vector<double>& numbers, double& min, double& max) { void find_minmax(const vector<double>& numbers, double& min, double& max) {
min = numbers[0]; if (numbers.empty())
{
min = 0;
max = 0;
return;
}
max = numbers[0]; max = numbers[0];
min = numbers[0];
for (double number : numbers) { for (double x : numbers) {
if (number < min) { if (x < min) min = x;
min = number; else if (x > max) max = x;
}
if (number > max) {
max = number;
}
} }
} }
std::vector<size_t> make_histogram(const std::vector<double>& numbers, size_t bin_count) { std::vector<size_t> make_histogram(const std::vector<double>& numbers, size_t bin_count) {
std::vector<size_t> bins(bin_count, 0); std::vector<size_t> bins(bin_count, 0);

@ -78,3 +78,39 @@
"histogram_internal.h" "histogram_internal.h"
<vector> <vector>
1748875465 source:c:\users\aleks\onedrive\Ðàáî÷èé ñòîë\laba1 — êîïèÿbb\histogram.cpp
"histogram.h"
"histogram_internal.h"
<vector>
1748386910 c:\users\aleks\onedrive\Ðàáî÷èé ñòîë\laba1 — êîïèÿbb\histogram.h
<vector>
1748392232 c:\users\aleks\onedrive\Ðàáî÷èé ñòîë\laba1 — êîïèÿbb\histogram_internal.h
<vector>
1749627705 source:c:\users\aleks\onedrive\Ðàáî÷èé ñòîë\laba1 — êîïèÿbb\main.cpp
<iostream>
<vector>
<string>
"histogram.h"
"text.h"
"svg.h"
"histogram_internal.h"
1748730472 c:\users\aleks\onedrive\Ðàáî÷èé ñòîë\laba1 — êîïèÿbb\text.h
<vector>
1749627429 c:\users\aleks\onedrive\Ðàáî÷èé ñòîë\laba1 — êîïèÿbb\svg.h
<vector>
<cstddef>
1748808306 source:c:\users\aleks\onedrive\Ðàáî÷èé ñòîë\laba1 — êîïèÿbb\text.cpp
"text.h"
<iostream>
<vector>
1748876656 source:c:\users\aleks\onedrive\Ðàáî÷èé ñòîë\laba1 — êîïèÿbb\svg.cpp
"svg.h"
<iostream>

@ -1,24 +1,25 @@
#include <iostream> #include <iostream>
#include <vector> #include <vector>
#include <string>
#include "histogram.h" #include "histogram.h"
#include "text.h" #include "text.h"
#include "svg.h" #include "svg.h"
#include "histogram_internal.h"
using namespace std; using namespace std;
struct Input { struct Input {
std::vector<double> numbers; vector<double> numbers;
size_t bin_count; size_t bin_count;
}; };
// Ââîä äàííûõ
Input input_data() { Input input_data() {
Input in; Input in;
size_t number_count; size_t number_count;
cerr << "Enter number count: "; cerr << "Enter number count: ";
cin >> number_count; cin >> number_count;
in.numbers.resize(number_count); in.numbers.resize(number_count);
cerr << "Enter numbers: "; cerr << "Enter numbers: ";
@ -27,21 +28,11 @@ Input input_data() {
} }
cerr << "Enter bin count: "; cerr << "Enter bin count: ";
cin >> in.bin_count; cin >> in.bin_count;
return in; return in;
} }
// Ïîèñê ìèíèìóìà è ìàêñèìóìà
// Ðàñ÷¸ò ãèñòîãðàììû
// Âûâîä ãèñòîãðàììû
// Îñíîâíàÿ ôóíêöèÿ
int main() { int main() {
auto in = input_data(); auto in = input_data();
auto bins = make_histogram(in.numbers, in.bin_count); auto bins = make_histogram(in.numbers, in.bin_count);

@ -1,66 +1,55 @@
#include "svg.h" #include "svg.h"
#include<iostream> #include <iostream>
using namespace std; using namespace std;
void
svg_begin(double width, double height) { void svg_begin(double width, double height) {
cout << "<?xml version='1.0' encoding='UTF-8'?>\n"; cout << "<?xml version='1.0' encoding='UTF-8'?>\n";
cout << "<svg "; cout << "<svg width='" << width << "' height='" << height << "' "
cout << "width='" << width << "' "; << "viewBox='0 0 " << width << " " << height << "' "
cout << "height='" << height << "' "; << "xmlns='http://www.w3.org/2000/svg'>\n";
cout << "viewBox='0 0 " << width << " " << height << "' ";
cout << "xmlns='http://www.w3.org/2000/svg'>\n"; // Ñòèëè äëÿ SVG
cout << "<style>\n"
<< " .bar { fill: #4CAF50; stroke: #388E3C; stroke-width: 1; }\n"
<< " .text { font: 12px sans-serif; fill: #333; }\n"
<< "</style>\n";
} }
void void svg_end() {
svg_end() {
cout << "</svg>\n"; 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 = "black", string fill = "black")
{
cout << "<rect x='"<<x<<"' y='"<<y<<"' width='"<<width<<"' height='"<<height<<"' stroke='"<<stroke<<"' fill='"<<fill<<"' />";
void svg_text(double left, double baseline, string text) {
cout << "<text class='text' x='" << left << "' y='" << baseline << "'>"
<< text << "</text>";
} }
void svg_rect(double x, double y, double width, double height) {
cout << "<rect class='bar' x='" << x << "' y='" << y << "' width='" << width
<< "' height='" << height << "' />";
}
void show_histogram_svg(const vector<size_t>& bins) {
void
show_histogram_svg(const vector<size_t>& bins)
{
const auto IMAGE_WIDTH = 400; const auto IMAGE_WIDTH = 400;
const auto IMAGE_HEIGHT = 300; const auto IMAGE_HEIGHT = 300;
const auto TEXT_LEFT = 20; const auto TEXT_LEFT = 20;
const auto TEXT_BASELINE = 20; const auto TEXT_BASELINE = 20;
const auto TEXT_WIDTH = 50; const auto TEXT_WIDTH = 50;
const auto BIN_HEIGHT = 30; const auto BIN_HEIGHT = 30;
const auto YELLOW = "yellow"; const auto MAX_WIDTH = IMAGE_WIDTH - TEXT_WIDTH;
const auto PURPLE = "purple";
const auto MAX_WIDTH = IMAGE_WIDTH-TEXT_WIDTH;
svg_begin(IMAGE_WIDTH,IMAGE_HEIGHT); svg_begin(IMAGE_WIDTH, IMAGE_HEIGHT);
double top = 0; double top = 0;
double max_count = bins[0]; size_t max_count = 0;
for (size_t i = 0; i < bins.size(); i++) for (size_t count : bins) {
{ if (count > max_count) max_count = count;
if (max_count<bins[i])
{
max_count=bins[i];
}
} }
for (size_t bin : bins) for (size_t bin : bins) {
{ const double bin_width = MAX_WIDTH * (static_cast<double>(bin) / max_count);
double bin_width = (MAX_WIDTH)*(bin/max_count);
svg_text(TEXT_LEFT, top + TEXT_BASELINE, to_string(bin)); svg_text(TEXT_LEFT, top + TEXT_BASELINE, to_string(bin));
svg_rect(TEXT_WIDTH, top, bin_width, BIN_HEIGHT, YELLOW, PURPLE); svg_rect(TEXT_WIDTH, top, bin_width, BIN_HEIGHT);
top += BIN_HEIGHT; top += BIN_HEIGHT;
} }

@ -53,4 +53,10 @@ TEST_CASE("vector with zero") {
CHECK(min == -1); CHECK(min == -1);
CHECK(max == 1); CHECK(max == 1);
} }
TEST_CASE("empty vector") {
double min = 0;
double max = 0;
find_minmax({}, min, max);
CHECK(min == 0);
CHECK(max == 0);
}

@ -66,3 +66,71 @@
<vector> <vector>
<cstddef> <cstddef>
1748875465 source:c:\users\aleks\onedrive\Ðàáî÷èé ñòîë\laba1 — êîïèÿbb\histogram.cpp
"histogram.h"
"histogram_internal.h"
<vector>
1748386910 c:\users\aleks\onedrive\Ðàáî÷èé ñòîë\laba1 — êîïèÿbb\histogram.h
<vector>
1748392232 c:\users\aleks\onedrive\Ðàáî÷èé ñòîë\laba1 — êîïèÿbb\histogram_internal.h
<vector>
1748876656 source:c:\users\aleks\onedrive\Ðàáî÷èé ñòîë\laba1 — êîïèÿbb\svg.cpp
"svg.h"
<iostream>
1748810901 c:\users\aleks\onedrive\Ðàáî÷èé ñòîë\laba1 — êîïèÿbb\svg.h
<vector>
<cstddef>
1748875527 source:c:\users\aleks\onedrive\Ðàáî÷èé ñòîë\laba1 — êîïèÿbb\unittest.cpp
"doctest.h"
"histogram_internal.h"
1748392747 c:\users\aleks\onedrive\Ðàáî÷èé ñòîë\laba1 — êîïèÿbb\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>

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