TupikovAA 13 часов назад
Родитель 325d6f8fc4
Сommit b086795769

@ -3,17 +3,21 @@
#include "histogram_internal.h" #include "histogram_internal.h"
#include <algorithm> #include <algorithm>
#include <cmath> #include <cmath>
#include <vector>
using namespace std;
int find_minmax(const std::vector<double>& numbers, double& min, double& max) {
void find_minmax(const std::vector<double>& numbers, double& min, double& max) { if(numbers.size()){
if (numbers.empty()) return;
min = numbers[0]; min = numbers[0];
max = numbers[0]; max = numbers[0];
}
for (double num : numbers) { for (double num : numbers) {
if (num < min) min = num; if (num < min) min = num;
if (num > max) max = num; if (num > max) max = num;
} }
return numbers.size();
} }
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) {
@ -56,24 +60,27 @@ Input input_data() {
return in; return in;
} }
void show_histogram_text(const std::vector<size_t>& bins, size_t max_width) { void show_histogram_text(const vector<size_t>& bins, size_t block_width) {
if (bins.empty()) return; if (bins.empty()) return;
size_t max_count = *std::max_element(bins.begin(), bins.end()); const size_t max_count = *max_element(bins.begin(), bins.end());
if (max_count == 0) return; if (max_count == 0) return;
const size_t max_console_width = 60;
const double scale = static_cast<double>(max_console_width) / max_count;
for (size_t count : bins) { for (size_t count : bins) {
if (count < 100) std::cout << " "; size_t stars = static_cast<size_t>(count * scale);
if (count < 10) std::cout << " ";
std::cout << count << "|";
if (count < 100) cout << " ";
if (count < 10) cout << " ";
size_t bar_length = (count * max_width) / max_count; cout << count << "|";
for (size_t i = 0; i < bar_length; ++i) { cout << string(stars, '*') << endl;
std::cout << "*";
}
std::cout << std::endl;
} }
} }

@ -5,11 +5,10 @@
struct Input { struct Input {
std::vector<double> numbers; std::vector<double> numbers;
size_t bin_count{}; size_t bin_count;
size_t number_count; size_t number_count;
}; };
Input input_data(); Input input_data();
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);
void show_histogram_text(const std::vector<size_t>& bins, size_t max_width = 80); void show_histogram_text(const std::vector<size_t>& bins, size_t max_width = 80);

@ -0,0 +1,9 @@
<?xml version='1.0' encoding='UTF-8'?>
<svg width='400' height='300' viewBox='0 0 400 300' xmlns='http://www.w3.org/2000/svg'>
<text x='20' y='20'>4</text>
<rect x='50' y='0' width='155' height='30' stroke='black' fill='#aaffaa' />
<text x='20' y='50'>1</text>
<rect x='50' y='30' width='38.75' height='30' stroke='black' fill='#aaffaa' />
<text x='20' y='80'>1</text>
<rect x='50' y='60' width='38.75' height='30' stroke='black' fill='#aaffaa' />
</svg>

После

Ширина:  |  Высота:  |  Размер: 455 B

@ -3,6 +3,6 @@
#include <vector> #include <vector>
void find_minmax(const std::vector<double>& numbers, double& min, double& max); int find_minmax(const std::vector<double>& numbers, double& min, double& max);
#endif #endif

@ -32,7 +32,11 @@
<Add option="-Wall" /> <Add option="-Wall" />
<Add option="-fexceptions" /> <Add option="-fexceptions" />
</Compiler> </Compiler>
<Unit filename="histogram.cpp" />
<Unit filename="histogram.h" />
<Unit filename="main.cpp" /> <Unit filename="main.cpp" />
<Unit filename="svg.cpp" />
<Unit filename="svg.h" />
<Extensions> <Extensions>
<lib_finder disable_auto="1" /> <lib_finder disable_auto="1" />
</Extensions> </Extensions>

@ -0,0 +1,33 @@
# depslib dependency file v1.0
1750444036 source:c:\users\taa41\desktop\ïðîãà\lab1\main.cpp
<iostream>
"histogram.h"
"svg.h"
<fstream>
1749173822 c:\users\taa41\desktop\ïðîãà\lab1\histogram.h
<vector>
1750443911 source:c:\users\taa41\desktop\ïðîãà\lab1\svg.cpp
"svg.h"
<algorithm>
<string>
<iostream>
1750442384 c:\users\taa41\desktop\ïðîãà\lab1\svg.h
<vector>
<string>
<iostream>
<fstream>
1750443947 source:c:\users\taa41\desktop\ïðîãà\lab1\histogram.cpp
<iostream>
"histogram.h"
"histogram_internal.h"
<algorithm>
<cmath>
<vector>
1749200462 c:\users\taa41\desktop\ïðîãà\lab1\histogram_internal.h
<vector>

@ -0,0 +1,30 @@
<?xml version="1.0" encoding="UTF-8" standalone="yes" ?>
<CodeBlocks_layout_file>
<FileVersion major="1" minor="0" />
<ActiveTarget name="Debug" />
<File name="histogram.cpp" open="1" top="0" tabpos="5" split="0" active="1" splitpos="0" zoom_1="0" zoom_2="0">
<Cursor>
<Cursor1 position="289" topLine="0" />
</Cursor>
</File>
<File name="main.cpp" open="1" top="0" tabpos="1" split="0" active="1" splitpos="0" zoom_1="0" zoom_2="0">
<Cursor>
<Cursor1 position="142" topLine="0" />
</Cursor>
</File>
<File name="svg.cpp" open="1" top="0" tabpos="2" split="0" active="1" splitpos="0" zoom_1="-2" zoom_2="0">
<Cursor>
<Cursor1 position="1151" topLine="0" />
</Cursor>
</File>
<File name="svg.h" open="1" top="1" tabpos="3" split="0" active="1" splitpos="0" zoom_1="0" zoom_2="0">
<Cursor>
<Cursor1 position="671" topLine="0" />
</Cursor>
</File>
<File name="histogram.h" open="1" top="0" tabpos="4" split="0" active="1" splitpos="0" zoom_1="0" zoom_2="0">
<Cursor>
<Cursor1 position="168" topLine="0" />
</Cursor>
</File>
</CodeBlocks_layout_file>

@ -1,9 +1,24 @@
#include "histogram.h"
#include <iostream> #include <iostream>
#include "histogram.h"
#include "svg.h"
#include <fstream>
using namespace std;
int main() { int main() {
auto in = input_data(); Input data = input_data();
auto bins = make_histogram(in.numbers, in.bin_count);
show_histogram_text(bins);
size_t block_width = svg::input_block_width();
auto bins = make_histogram(data.numbers, data.bin_count);
show_histogram_text(bins, block_width);
ofstream svg_file("histogram.svg");
svg::show_histogram_svg(svg_file, bins, block_width);
svg_file.close();
return 0; return 0;
} }

@ -1,10 +1,34 @@
#include "svg.h" #include "svg.h"
#include <algorithm> #include <algorithm>
#include <string> #include <string>
#include <iostream>
using namespace std; using namespace std;
namespace svg { namespace svg {
size_t BLOCK_WIDTH = 10;
size_t input_block_width() {
size_t width;
while (true) {
cout << "Ââåäèòå øèðèíó áëîêà ãèñòîãðàììû (3-30px): ";
cin >> width;
if (width < 3) {
cout << "Øèðèíà ñëèøêîì ìàëà. Ìèíèìàëüíîå çíà÷åíèå: 3px. Ïîæàëóéñòà, ïîâòîðèòå ââîä.\n";
} else if (width > 30) {
cout << "Øèðèíà ñëèøêîì âåëèêà. Ìàêñèìàëüíîå çíà÷åíèå: 30px. Ïîæàëóéñòà, ïîâòîðèòå ââîä.\n";
} else {
break;
}
}
return width;
}
void set_block_width(size_t width) {
BLOCK_WIDTH = width;
}
void begin(ostream& out, double width, double height) { void begin(ostream& out, double width, double height) {
out << "<?xml version='1.0' encoding='UTF-8'?>\n"; out << "<?xml version='1.0' encoding='UTF-8'?>\n";
out << "<svg width='" << width << "' height='" << height << "' " out << "<svg width='" << width << "' height='" << height << "' "
@ -27,7 +51,7 @@ namespace svg {
<< "' fill='" << fill << "' />\n"; << "' fill='" << fill << "' />\n";
} }
void show_histogram_svg(ostream& out, const vector<size_t>& bins) { void show_histogram_svg(ofstream& out, const vector<size_t>& bins, size_t block_width) {
begin(out, IMAGE_WIDTH, IMAGE_HEIGHT); begin(out, IMAGE_WIDTH, IMAGE_HEIGHT);
if (bins.empty()) { if (bins.empty()) {
@ -35,22 +59,29 @@ namespace svg {
return; return;
} }
const size_t max_count = *max_element(bins.begin(), bins.end());
if (max_count == 0) {
end(out);
return;
}
const double max_svg_width = IMAGE_WIDTH - TEXT_WIDTH - 40;
size_t max_count = *max_element(bins.begin(), bins.end()); const double scale = max_svg_width / max_count;
double scale = (max_count > 0) ? (IMAGE_WIDTH - TEXT_WIDTH) / static_cast<double>(max_count) : 1.0;
double top = 0; double top = 0;
for (size_t bin : bins) { for (size_t bin : bins) {
const double bin_width = bin * scale;
text(out, TEXT_LEFT, top + TEXT_BASELINE, to_string(bin));
double width = bin * scale * (block_width / 10.0);
string fill_color = (static_cast<int>(top / BIN_HEIGHT) % 2 == 0) ? "#aaffaa" : "#aaaaff"; text(out, TEXT_LEFT, top + TEXT_BASELINE, to_string(bin));
rect(out, TEXT_WIDTH, top, bin_width, BIN_HEIGHT, "black", fill_color); rect(out, TEXT_WIDTH, top, width, BIN_HEIGHT, "black", "#aaffaa");
top += BIN_HEIGHT; top += BIN_HEIGHT;
} }
end(out); end(out);
} }
} }

@ -4,7 +4,7 @@
#include <vector> #include <vector>
#include <string> #include <string>
#include <iostream> #include <iostream>
#include <fstream>
const size_t IMAGE_WIDTH = 400; const size_t IMAGE_WIDTH = 400;
const size_t IMAGE_HEIGHT = 300; const size_t IMAGE_HEIGHT = 300;
@ -12,7 +12,6 @@ const size_t TEXT_LEFT = 20;
const size_t TEXT_BASELINE = 20; const size_t TEXT_BASELINE = 20;
const size_t TEXT_WIDTH = 50; const size_t TEXT_WIDTH = 50;
const size_t BIN_HEIGHT = 30; const size_t BIN_HEIGHT = 30;
const size_t BLOCK_WIDTH = 10;
namespace svg { namespace svg {
void begin(std::ostream& out, double width, double height); void begin(std::ostream& out, double width, double height);
@ -20,8 +19,9 @@ namespace svg {
void text(std::ostream& out, double left, double baseline, const std::string& text); void text(std::ostream& out, double left, double baseline, const std::string& text);
void rect(std::ostream& out, double x, double y, double width, double height, void rect(std::ostream& out, double x, double y, double width, double height,
const std::string& stroke = "black", const std::string& fill = "#dddddd"); const std::string& stroke = "black", const std::string& fill = "#dddddd");
void show_histogram_svg(std::ofstream& out, const std::vector<size_t>& bins, size_t block_width);
void show_histogram_svg(std::ostream& out, const std::vector<size_t>& bins); size_t input_block_width();
void set_block_width(size_t width);
} }
#endif #endif

@ -31,6 +31,9 @@
<Compiler> <Compiler>
<Add option="-Wall" /> <Add option="-Wall" />
</Compiler> </Compiler>
<Unit filename="histogram.cpp" />
<Unit filename="histogram_internal.h" />
<Unit filename="unittest.cpp" />
<Extensions> <Extensions>
<lib_finder disable_auto="1" /> <lib_finder disable_auto="1" />
</Extensions> </Extensions>

@ -0,0 +1,52 @@
#define DOCTEST_CONFIG_NO_MULTITHREADING
#define DOCTEST_CONFIG_IMPLEMENT_WITH_MAIN
#include "doctest.h"
#include "histogram_internal.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("empty vector") {
double min = 0;
double max = 0;
int sz = find_minmax({}, min, max);
CHECK(sz == 0);
}
TEST_CASE("single element vector") {
double min = 0;
double max = 0;
find_minmax({5}, min, max);
CHECK(min == 5);
CHECK(max == 5);
}
TEST_CASE("negative numbers") {
double min = 0;
double max = 0;
find_minmax({-3, -1, -5}, min, max);
CHECK(min == -5);
CHECK(max == -1);
}
TEST_CASE("identical elements") {
double min = 0;
double max = 0;
find_minmax({7, 7, 7}, min, max);
CHECK(min == 7);
CHECK(max == 7);
}
TEST_CASE("mixed positive and negative numbers") {
double min = 0;
double max = 0;
find_minmax({-2, 3, 0, -5, 4}, min, max);
CHECK(min == -5);
CHECK(max == 4);
}

@ -0,0 +1,63 @@
# depslib dependency file v1.0
1749200479 source:c:\users\taa41\desktop\ïðîãà\lab1\histogram.cpp
<iostream>
"histogram.h"
"histogram_internal.h"
<algorithm>
<cmath>
1749173822 c:\users\taa41\desktop\ïðîãà\lab1\histogram.h
<vector>
1749200462 c:\users\taa41\desktop\ïðîãà\lab1\histogram_internal.h
<vector>
1749200396 source:c:\users\taa41\desktop\ïðîãà\lab1\unittest.cpp
"doctest.h"
"histogram_internal.h"
1749168294 c:\users\taa41\desktop\ïðîãà\lab1\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>

@ -0,0 +1,20 @@
<?xml version="1.0" encoding="UTF-8" standalone="yes" ?>
<CodeBlocks_layout_file>
<FileVersion major="1" minor="0" />
<ActiveTarget name="Debug" />
<File name="histogram_internal.h" open="1" top="1" tabpos="1" split="0" active="1" splitpos="0" zoom_1="0" zoom_2="0">
<Cursor>
<Cursor1 position="171" topLine="0" />
</Cursor>
</File>
<File name="histogram.cpp" open="1" top="0" tabpos="0" split="0" active="1" splitpos="0" zoom_1="0" zoom_2="0">
<Cursor>
<Cursor1 position="0" topLine="0" />
</Cursor>
</File>
<File name="unittest.cpp" open="1" top="0" tabpos="2" split="0" active="1" splitpos="0" zoom_1="0" zoom_2="0">
<Cursor>
<Cursor1 position="225" topLine="0" />
</Cursor>
</File>
</CodeBlocks_layout_file>
Загрузка…
Отмена
Сохранить