изменения 3 лаба
Этот коммит содержится в:
@@ -2,19 +2,21 @@
|
||||
#include "histogram_internal.h"
|
||||
#include <vector>
|
||||
using std::vector;
|
||||
void find_minmax(const std::vector<double>& numbers, double& min, double& max) {
|
||||
min = numbers[0];
|
||||
void find_minmax(const vector<double>& numbers, double& min, double& max) {
|
||||
if (numbers.empty())
|
||||
{
|
||||
min = 0;
|
||||
max = 0;
|
||||
return;
|
||||
}
|
||||
max = numbers[0];
|
||||
min = numbers[0];
|
||||
for (double x : numbers) {
|
||||
if (x < min) min = x;
|
||||
else if (x > max) max = x;
|
||||
}
|
||||
}
|
||||
|
||||
for (double number : numbers) {
|
||||
if (number < min) {
|
||||
min = number;
|
||||
}
|
||||
if (number > max) {
|
||||
max = number;
|
||||
}
|
||||
}
|
||||
}
|
||||
std::vector<size_t> make_histogram(const std::vector<double>& numbers, size_t bin_count) {
|
||||
std::vector<size_t> bins(bin_count, 0);
|
||||
|
||||
|
||||
36
laba1.depend
36
laba1.depend
@@ -78,3 +78,39 @@
|
||||
"histogram_internal.h"
|
||||
<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>
|
||||
|
||||
|
||||
21
main.cpp
21
main.cpp
@@ -1,24 +1,25 @@
|
||||
#include <iostream>
|
||||
#include <vector>
|
||||
#include <string>
|
||||
#include "histogram.h"
|
||||
#include "text.h"
|
||||
#include "svg.h"
|
||||
#include "histogram_internal.h"
|
||||
using namespace std;
|
||||
|
||||
|
||||
|
||||
struct Input {
|
||||
std::vector<double> numbers;
|
||||
vector<double> numbers;
|
||||
|
||||
size_t bin_count;
|
||||
};
|
||||
|
||||
// Ââîä äàííûõ
|
||||
|
||||
Input input_data() {
|
||||
Input in;
|
||||
size_t number_count;
|
||||
|
||||
cerr << "Enter number count: ";
|
||||
cin >> number_count;
|
||||
|
||||
in.numbers.resize(number_count);
|
||||
|
||||
cerr << "Enter numbers: ";
|
||||
@@ -32,16 +33,6 @@ Input input_data() {
|
||||
return in;
|
||||
}
|
||||
|
||||
// Ïîèñê ìèíèìóìà è ìàêñèìóìà
|
||||
|
||||
|
||||
// Ðàñ÷¸ò ãèñòîãðàììû
|
||||
|
||||
|
||||
// Âûâîä ãèñòîãðàììû
|
||||
|
||||
|
||||
// Îñíîâíàÿ ôóíêöèÿ
|
||||
int main() {
|
||||
auto in = input_data();
|
||||
auto bins = make_histogram(in.numbers, in.bin_count);
|
||||
|
||||
63
svg.cpp
63
svg.cpp
@@ -1,66 +1,55 @@
|
||||
#include "svg.h"
|
||||
#include <iostream>
|
||||
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 << "<svg ";
|
||||
cout << "width='" << width << "' ";
|
||||
cout << "height='" << height << "' ";
|
||||
cout << "viewBox='0 0 " << width << " " << height << "' ";
|
||||
cout << "xmlns='http://www.w3.org/2000/svg'>\n";
|
||||
cout << "<svg width='" << width << "' height='" << height << "' "
|
||||
<< "viewBox='0 0 " << width << " " << height << "' "
|
||||
<< "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
|
||||
svg_end() {
|
||||
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_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, string stroke = "black", string fill = "black")
|
||||
{
|
||||
cout << "<rect x='"<<x<<"' y='"<<y<<"' width='"<<width<<"' height='"<<height<<"' stroke='"<<stroke<<"' fill='"<<fill<<"' />";
|
||||
|
||||
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_HEIGHT = 300;
|
||||
const auto TEXT_LEFT = 20;
|
||||
const auto TEXT_BASELINE = 20;
|
||||
const auto TEXT_WIDTH = 50;
|
||||
const auto BIN_HEIGHT = 30;
|
||||
const auto YELLOW = "yellow";
|
||||
const auto PURPLE = "purple";
|
||||
const auto MAX_WIDTH = IMAGE_WIDTH - TEXT_WIDTH;
|
||||
|
||||
|
||||
svg_begin(IMAGE_WIDTH, IMAGE_HEIGHT);
|
||||
|
||||
double top = 0;
|
||||
double max_count = bins[0];
|
||||
for (size_t i = 0; i < bins.size(); i++)
|
||||
{
|
||||
if (max_count<bins[i])
|
||||
{
|
||||
max_count=bins[i];
|
||||
}
|
||||
size_t max_count = 0;
|
||||
for (size_t count : bins) {
|
||||
if (count > max_count) max_count = count;
|
||||
}
|
||||
|
||||
for (size_t bin : bins)
|
||||
{
|
||||
double bin_width = (MAX_WIDTH)*(bin/max_count);
|
||||
for (size_t bin : bins) {
|
||||
const double bin_width = MAX_WIDTH * (static_cast<double>(bin) / max_count);
|
||||
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;
|
||||
}
|
||||
|
||||
|
||||
@@ -53,4 +53,10 @@ TEST_CASE("vector with zero") {
|
||||
CHECK(min == -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>
|
||||
<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>
|
||||
|
||||
|
||||
Ссылка в новой задаче
Block a user