Сравнить коммиты

...

3 Коммитов

Автор SHA1 Сообщение Дата
EvdochenkoNV 5c80c670c0 code: добавлен bool prompt
2 месяцев назад
EvdochenkoNV 4eb9fb549b code: cin изменен на in, возврат к дозащитному варианту
2 месяцев назад
EvdochenkoNV 5f4d64fd36 code: защита
2 месяцев назад

1
.gitignore поставляемый

@ -4,3 +4,4 @@
/cs-lab34.layout /cs-lab34.layout
/main.exe /main.exe
/main.o /main.o
/unittest.layout

@ -3,4 +3,3 @@
using namespace std; using namespace std;
vector<size_t> make_histogram(size_t number, vector<double> vec); vector<size_t> make_histogram(size_t number, vector<double> vec);

@ -1 +1,2 @@
bool find_minmax(std::vector<double> vec, double& min, double& max); bool find_minmax(std::vector<double> vec, double& min, double& max);
//

@ -5,40 +5,26 @@
struct Input { struct Input {
vector<double> vec; vector<double> vec;
size_t korz{}; size_t korz{};
size_t block_width{};
}; };
Input input_data() { Input input_data(istream& in, bool prompt = false) {
Input in; Input lin;
size_t n, korz; size_t n, korz;
if(prompt)
cerr << "Number of elements: "; cerr << "Number of elements: ";
cin >> n; in >> n;
in.vec.resize(n); lin.vec.resize(n);
cerr << "Elements: ";
for (size_t i = 0; i < n; i++) for (size_t i = 0; i < n; i++)
cin >> in.vec[i]; in >> lin.vec[i];
cerr << "Enter bin count: "; if(prompt)
cin >> in.korz; cerr << "Enter bin count: ";
in >> lin.korz;
return lin;
cerr << "Enter block width in px (3-30): ";
cin >> in.block_width;
while (in.block_width < 3 || in.block_width > 30) {
if (in.block_width < 3) {
cerr << "Block width too small! Minimum is 3px. Please enter again: ";
} else {
cerr << "Block width too large! Maximum is 30px. Please enter again: ";
}
cin >> in.block_width;
}
return in;
} }
int main() { int main() {
auto in = input_data();
auto in = input_data(cin, true);
auto bins = make_histogram(in.korz, in.vec); auto bins = make_histogram(in.korz, in.vec);
show_histogram_svg(bins, in.block_width); show_histogram_svg(bins);
} }

@ -2,7 +2,9 @@
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 ";
cout << "width='" << width << "' "; cout << "width='" << width << "' ";
@ -11,46 +13,59 @@ void svg_begin(double width, double height) {
cout << "xmlns='http://www.w3.org/2000/svg'>\n"; cout << "xmlns='http://www.w3.org/2000/svg'>\n";
} }
void svg_end() { void
svg_end()
{
cout << "</svg>\n"; cout << "</svg>\n";
} }
void svg_text(double left, double baseline, string text) { void
svg_text(double left, double baseline, string text)
{
cout << "<text x='" << left << "' y='" << baseline << "'>" << text << "</text>"; cout << "<text x='" << left << "' y='" << baseline << "'>" << text << "</text>";
} }
void svg_rect(double x, double y, double width, double height, string stroke, string fill) { 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 << "' />"; cout << "<rect x='" << x << "' y='" << y << "' width='" << width << "' height='" << height << "' stroke='" << stroke << "' fill='" << fill << "' />";
} }
void show_histogram_svg(const vector<size_t>& bins, size_t block_width) {
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 BLACK = "black"; const auto BLOCK_WIDTH = 10;
const auto GREEN = "green";
const auto RED = "red"; const auto RED = "red";
const auto MAX_WIDTH = IMAGE_WIDTH - TEXT_WIDTH; 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.empty() ? 0 : bins[0]; double max_count = bins[0];
for (size_t i = 1; i < bins.size(); i++) { for (size_t i = 0; i < bins.size(); i++)
if (bins[i] > max_count) { {
if (max_count < bins[i])
{
max_count = bins[i]; max_count = bins[i];
} }
} }
for (size_t bin : bins) { for (size_t bin : bins)
double bin_width = static_cast<double>(block_width) * bin; {
if (bin_width > MAX_WIDTH) { double bin_width = (MAX_WIDTH) * (bin/max_count);
bin_width = MAX_WIDTH;
}
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, BLACK, RED); svg_rect(TEXT_WIDTH, top, bin_width, BIN_HEIGHT, GREEN, RED);
top += BIN_HEIGHT; top += BIN_HEIGHT;
} }

@ -2,6 +2,6 @@
#include <vector> #include <vector>
#include <string> #include <string>
#include <math.h> #include <math.h>
using namespace std;
void show_histogram_svg(const vector<size_t>& bins, size_t block_width); using namespace std;
void show_histogram_svg(const vector<size_t>& bins);

@ -2,4 +2,3 @@
#include <vector> #include <vector>
void show_histogram(std::vector<size_t> bins); void show_histogram(std::vector<size_t> bins);

@ -21,6 +21,9 @@ TEST_CASE("empty vector"){
CHECK(!result); CHECK(!result);
} }
TEST_CASE("single vector"){ TEST_CASE("single vector"){
double min = 0; double min = 0;
double max = 0; double max = 0;
@ -47,3 +50,17 @@ TEST_CASE("same vector"){
CHECK(min == 1); CHECK(min == 1);
CHECK(max == 1); CHECK(max == 1);
} }
TEST_CASE("block"){
size_t block_width = 15;
bool result = width( block_width);
CHECK(result);
}
TEST_CASE("block"){
size_t block_width = 0;
bool result = width( block_width);
CHECK(!result);
}

@ -1,12 +1,12 @@
# depslib dependency file v1.0 # depslib dependency file v1.0
1746274971 source:c:\users\natasha\desktop\Ïðîãè Ñ2\cs-lab34\histogram.cpp 1746447381 source:c:\users\natasha\desktop\Ïðîãè Ñ2\cs-lab34\histogram.cpp
"histogram.h" "histogram.h"
1746274971 c:\users\natasha\desktop\Ïðîãè Ñ2\cs-lab34\histogram.h 1746446253 c:\users\natasha\desktop\Ïðîãè Ñ2\cs-lab34\histogram.h
<vector> <vector>
<iostream> <iostream>
1746276122 source:c:\users\natasha\desktop\Ïðîãè Ñ2\cs-lab34\unittest.cpp 1746447498 source:c:\users\natasha\desktop\Ïðîãè Ñ2\cs-lab34\unittest.cpp
"doctest.h" "doctest.h"
"histogram_internal.h" "histogram_internal.h"
@ -55,5 +55,5 @@
<sys/time.h> <sys/time.h>
<unistd.h> <unistd.h>
1746276025 c:\users\natasha\desktop\Ïðîãè Ñ2\cs-lab34\histogram_internal.h 1746447065 c:\users\natasha\desktop\Ïðîãè Ñ2\cs-lab34\histogram_internal.h

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