Platon (MiachinPY) 4 недель назад
Родитель 3145fd865b
Сommit a956d37344

@ -3,23 +3,17 @@
#include "histogram.h" #include "histogram.h"
using namespace std; using namespace std;
void find_minmax(const vector<double> &numbers, double &min, double &max) bool find_minmax(const vector<double>& numbers, double& min, double& max) {
{ if (numbers.empty()) {
return false;
}
min = numbers[0]; min = numbers[0];
max = numbers[0]; max = numbers[0];
for(double number : numbers) for (double number : numbers) {
{ if (number < min) min = number;
if(number < min) if (number > max) max = number;
{
min = number;
} }
if(number > max) return true;
{
max = number;
}
}
return;
} }
std::vector<size_t> make_histogram(std::vector<double>& numbers, size_t bin_count) std::vector<size_t> make_histogram(std::vector<double>& numbers, size_t bin_count)

@ -1,6 +1,6 @@
#ifndef HISTOGRAM_INTERNAL_H_INCLUDED #ifndef HISTOGRAM_INTERNAL_H_INCLUDED
#define HISTOGRAM_INTERNAL_H_INCLUDED #define HISTOGRAM_INTERNAL_H_INCLUDED
#include <vector> #include <vector>
void find_minmax(const std::vector<double> &numbers, double &min, double &max); bool find_minmax(const std::vector<double> &numbers, double &min, double &max);
#endif // HISTOGRAM_INTERNAL_H_INCLUDED #endif // HISTOGRAM_INTERNAL_H_INCLUDED

@ -3,23 +3,17 @@
#include "histogram.h" #include "histogram.h"
using namespace std; using namespace std;
void find_minmax(const vector<double> &numbers, double &min, double &max) bool find_minmax(const vector<double>& numbers, double& min, double& max) {
{ if (numbers.empty()) {
return false;
}
min = numbers[0]; min = numbers[0];
max = numbers[0]; max = numbers[0];
for(double number : numbers) for (double number : numbers) {
{ if (number < min) min = number;
if(number < min) if (number > max) max = number;
{
min = number;
} }
if(number > max) return true;
{
max = number;
}
}
return;
} }
std::vector<size_t> make_histogram(std::vector<double>& numbers, size_t bin_count) std::vector<size_t> make_histogram(std::vector<double>& numbers, size_t bin_count)

@ -74,7 +74,7 @@
1745611606 c:\users\ïëàòîí\desktop\project\lab01\histogram_internal.h 1745611606 c:\users\ïëàòîí\desktop\project\lab01\histogram_internal.h
<vector> <vector>
1746093073 source:c:\users\ïëàòîí\desktop\project\lab01\histogram.cpp 1746450679 source:c:\users\ïëàòîí\desktop\project\lab01\histogram.cpp
<iostream> <iostream>
<vector> <vector>
"histogram.h" "histogram.h"

@ -2,24 +2,24 @@
<CodeBlocks_layout_file> <CodeBlocks_layout_file>
<FileVersion major="1" minor="0" /> <FileVersion major="1" minor="0" />
<ActiveTarget name="Debug" /> <ActiveTarget name="Debug" />
<File name="main.cpp" open="1" top="0" tabpos="2" split="0" active="1" splitpos="0" zoom_1="-1" zoom_2="0"> <File name="histogram.h" open="1" top="0" tabpos="0" split="0" active="1" splitpos="0" zoom_1="0" zoom_2="0">
<Cursor> <Cursor>
<Cursor1 position="511" topLine="19" /> <Cursor1 position="0" topLine="0" />
</Cursor> </Cursor>
</File> </File>
<File name="svg.cpp" open="1" top="0" tabpos="4" split="0" active="1" splitpos="0" zoom_1="0" zoom_2="0"> <File name="main.cpp" open="1" top="0" tabpos="2" split="0" active="1" splitpos="0" zoom_1="-1" zoom_2="0">
<Cursor> <Cursor>
<Cursor1 position="140" topLine="0" /> <Cursor1 position="511" topLine="19" />
</Cursor> </Cursor>
</File> </File>
<File name="histogram.h" open="1" top="0" tabpos="0" split="0" active="1" splitpos="0" zoom_1="0" zoom_2="0"> <File name="histogram.cpp" open="1" top="1" tabpos="1" split="0" active="1" splitpos="0" zoom_1="0" zoom_2="0">
<Cursor> <Cursor>
<Cursor1 position="0" topLine="0" /> <Cursor1 position="0" topLine="0" />
</Cursor> </Cursor>
</File> </File>
<File name="histogram.cpp" open="1" top="0" tabpos="0" split="0" active="1" splitpos="0" zoom_1="0" zoom_2="0"> <File name="svg.cpp" open="1" top="0" tabpos="3" split="0" active="1" splitpos="0" zoom_1="0" zoom_2="0">
<Cursor> <Cursor>
<Cursor1 position="0" topLine="0" /> <Cursor1 position="140" topLine="0" />
</Cursor> </Cursor>
</File> </File>
</CodeBlocks_layout_file> </CodeBlocks_layout_file>

@ -9,11 +9,12 @@ using namespace std;
struct Input { struct Input {
vector<double> numbers; vector<double> numbers;
size_t bin_count{}; size_t bin_count{};
size_t bin_height{};//äîáàâèë ïåðåìåííóþ
}; };
Input Input
input_data() { input_data() {
size_t number_count, bin_count; size_t number_count, bin_count, bin_height;
cerr << "Enter number count: "; cerr << "Enter number count: ";
cin >> number_count; cin >> number_count;
Input in; Input in;
@ -25,13 +26,15 @@ input_data() {
} }
cerr << "Enter bin count: "; cerr << "Enter bin count: ";
cin >> in.bin_count; cin >> in.bin_count;
cerr << "Enter bin height: ";//äîáàâèë ââîä
cin >> in.bin_height;
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);
show_histogram_svg(bins); show_histogram_svg(bins, in.bin_height);//äîáàâèë bin_height êàê ïàðàìåòð
return 0; return 0;
} }

@ -1,14 +1,13 @@
#include <math.h> #include <math.h>
#include <iostream> #include <iostream>
#include <conio.h>
#include <vector> #include <vector>
#include <string> #include <string>
#include "svg.h" #include "svg.h"
#include <algorithm>
using namespace std; using namespace std;
void void svg_begin(double width, double height)
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 ";
@ -18,32 +17,26 @@ 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 void svg_end()
svg_end()
{ {
cout << "</svg>\n"; cout << "</svg>\n";
} }
void void svg_text(double left, double baseline, string text)
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 void svg_rect(double x, double y, double width, double height, string stroke = "black", string fill = "black")
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 bin_height)
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 = 700;//ïîìåíÿë
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;
@ -53,29 +46,28 @@ show_histogram_svg(const vector<size_t>& bins)
const auto RED = "red"; const auto RED = "red";
const auto MAX_WIDTH = IMAGE_WIDTH-TEXT_WIDTH; const auto MAX_WIDTH = IMAGE_WIDTH-TEXT_WIDTH;
if (!bins.empty()){
const size_t total_height = bins.size() * bin_height;
if (total_height > IMAGE_HEIGHT) {
bin_height = IMAGE_HEIGHT / bins.size();
}
}
svg_begin(IMAGE_WIDTH,IMAGE_HEIGHT); svg_begin(IMAGE_WIDTH,IMAGE_HEIGHT);
double top = 0; double top = 0;
double max_count = bins[0]; if (!bins.empty()) {
for (size_t i = 0; i < bins.size(); i++) double max_count = *max_element(bins.begin(), bins.end());
{
if (max_count<bins[i])
{
max_count=bins[i];
}
}
for (size_t bin : bins) for (size_t bin : bins) {
{ 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, BLACK, RED); svg_rect(TEXT_WIDTH, top, bin_width, bin_height, BLACK, RED); //äîáàâèë ïàðàìåòð
top += BIN_HEIGHT; top += bin_height; //äîáàâèë ïàðàìåòð
}
} }
svg_end(); svg_end();
} }

@ -2,8 +2,6 @@
#define SVG_H_INCLUDED #define SVG_H_INCLUDED
#include <vector> #include <vector>
void void show_histogram_svg(const std::vector<size_t>& bins, size_t bin_height);//îòðåäà÷èë çàãîëîâîê
show_histogram_svg(const std::vector<size_t>& bins);
#endif // SVG_H_INCLUDED #endif // SVG_H_INCLUDED

@ -24,3 +24,19 @@ TEST_CASE("vector of the same elements"){
CHECK(min == 3); CHECK(min == 3);
CHECK(max == 3); CHECK(max == 3);
} }
TEST_CASE("single element vector") {
double min = 0;
double max = 0;
bool success = find_minmax({1}, min, max);
CHECK(success == true);
CHECK(min == 1);
CHECK(max == 1);
}
TEST_CASE("empty vector") {
double min = 0;
double max = 0;
bool success = find_minmax({}, min, max);
CHECK(success == false);
CHECK(min == 0);
CHECK(max == 0);
}

@ -7,7 +7,7 @@
1745607609 c:\users\ïëàòîí\desktop\project\lab01\histogram.h 1745607609 c:\users\ïëàòîí\desktop\project\lab01\histogram.h
<vector> <vector>
1746092640 source:c:\users\ïëàòîí\desktop\project\lab01\unittest.cpp 1746450558 source:c:\users\ïëàòîí\desktop\project\lab01\unittest.cpp
"doctest.h" "doctest.h"
"histogram_internal.h" "histogram_internal.h"
@ -56,10 +56,10 @@
<sys/time.h> <sys/time.h>
<unistd.h> <unistd.h>
1746092862 c:\users\ïëàòîí\desktop\project\lab01\histogram_internal.h 1746450731 c:\users\ïëàòîí\desktop\project\lab01\histogram_internal.h
<vector> <vector>
1746092126 source:c:\users\ïëàòîí\desktop\project\lab01\histogram.cpp 1746450679 source:c:\users\ïëàòîí\desktop\project\lab01\histogram.cpp
<iostream> <iostream>
<vector> <vector>
"histogram.h" "histogram.h"

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