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

@ -3,23 +3,17 @@
#include "histogram.h"
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];
max = numbers[0];
for(double number : numbers)
{
if(number < min)
{
min = number;
}
if(number > max)
{
max = number;
}
for (double number : numbers) {
if (number < min) min = number;
if (number > max) max = number;
}
return;
return true;
}
std::vector<size_t> make_histogram(std::vector<double>& numbers, size_t bin_count)

@ -1,6 +1,6 @@
#ifndef HISTOGRAM_INTERNAL_H_INCLUDED
#define HISTOGRAM_INTERNAL_H_INCLUDED
#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

@ -3,23 +3,17 @@
#include "histogram.h"
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];
max = numbers[0];
for(double number : numbers)
{
if(number < min)
{
min = number;
}
if(number > max)
{
max = number;
}
for (double number : numbers) {
if (number < min) min = number;
if (number > max) max = number;
}
return;
return true;
}
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
<vector>
1746093073 source:c:\users\ïëàòîí\desktop\project\lab01\histogram.cpp
1746450679 source:c:\users\ïëàòîí\desktop\project\lab01\histogram.cpp
<iostream>
<vector>
"histogram.h"

@ -2,24 +2,24 @@
<CodeBlocks_layout_file>
<FileVersion major="1" minor="0" />
<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>
<Cursor1 position="511" topLine="19" />
<Cursor1 position="0" topLine="0" />
</Cursor>
</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>
<Cursor1 position="140" topLine="0" />
<Cursor1 position="511" topLine="19" />
</Cursor>
</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>
<Cursor1 position="0" 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">
<File name="svg.cpp" open="1" top="0" tabpos="3" split="0" active="1" splitpos="0" zoom_1="0" zoom_2="0">
<Cursor>
<Cursor1 position="0" topLine="0" />
<Cursor1 position="140" topLine="0" />
</Cursor>
</File>
</CodeBlocks_layout_file>

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

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

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

@ -24,3 +24,19 @@ TEST_CASE("vector of the same elements"){
CHECK(min == 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
<vector>
1746092640 source:c:\users\ïëàòîí\desktop\project\lab01\unittest.cpp
1746450558 source:c:\users\ïëàòîí\desktop\project\lab01\unittest.cpp
"doctest.h"
"histogram_internal.h"
@ -56,10 +56,10 @@
<sys/time.h>
<unistd.h>
1746092862 c:\users\ïëàòîí\desktop\project\lab01\histogram_internal.h
1746450731 c:\users\ïëàòîí\desktop\project\lab01\histogram_internal.h
<vector>
1746092126 source:c:\users\ïëàòîí\desktop\project\lab01\histogram.cpp
1746450679 source:c:\users\ïëàòîí\desktop\project\lab01\histogram.cpp
<iostream>
<vector>
"histogram.h"

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