Сравнить коммиты
4 Коммитов
61a244479c
...
bfb3f8cf9c
Автор | SHA1 | Дата |
---|---|---|
![]() |
bfb3f8cf9c | 11 месяцев назад |
![]() |
29099c717e | 11 месяцев назад |
![]() |
15dba8705d | 11 месяцев назад |
![]() |
3cd21db461 | 11 месяцев назад |
Разница между файлами не показана из-за своего большого размера
Загрузить разницу
@ -0,0 +1,52 @@
|
||||
#include "histogram.h"
|
||||
#include <vector>
|
||||
#include <iostream>
|
||||
using namespace std;
|
||||
|
||||
void
|
||||
find_minmax(const vector<double>& numbers, double& min, double& max)
|
||||
{
|
||||
min = numbers[0];
|
||||
max = numbers[0];
|
||||
for (double x : numbers)
|
||||
{
|
||||
if (x > max )
|
||||
{
|
||||
max = x;
|
||||
}
|
||||
if (x < min)
|
||||
{
|
||||
min = x;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
vector<size_t>
|
||||
make_histogram(const vector<double>& numbers, size_t& bin_count)
|
||||
{
|
||||
double min = 0;
|
||||
double max = 0;
|
||||
find_minmax(numbers, min, max);
|
||||
auto bin_size = (max - min)/bin_count;
|
||||
vector<size_t> bins(bin_count);
|
||||
for (double number : numbers)
|
||||
{
|
||||
bool found = false;
|
||||
for (size_t j = 0; (j < bin_count - 1) && !found; j++)
|
||||
{
|
||||
auto lo = min + j * bin_size;
|
||||
auto hi = min + (j + 1) * bin_size;
|
||||
if ((lo <= number) && (number < hi))
|
||||
{
|
||||
bins[j]++;
|
||||
found = true;
|
||||
}
|
||||
}
|
||||
if (!found)
|
||||
{
|
||||
bins[bin_count - 1]++;
|
||||
}
|
||||
}
|
||||
return bins;
|
||||
}
|
@ -0,0 +1,9 @@
|
||||
#ifndef HISTOGRAM_H_INCLUDED
|
||||
#define HISTOGRAM_H_INCLUDED
|
||||
|
||||
#include <vector>
|
||||
|
||||
std::vector<size_t>
|
||||
make_histogram(const std::vector<double>& numbers, size_t& bin_count);
|
||||
|
||||
#endif // HISTOGRAM_H_INCLUDED
|
@ -0,0 +1,9 @@
|
||||
#ifndef HISTOGRAM_INTERNAL_H_INCLUDED
|
||||
#define HISTOGRAM_INTERNAL_H_INCLUDED
|
||||
|
||||
#include <vector>
|
||||
|
||||
void
|
||||
find_minmax(const std::vector<double>& numbers, double& min, double& max);
|
||||
|
||||
#endif // HISTOGRAM_INTERNAL_H_INCLUDED
|
@ -0,0 +1,33 @@
|
||||
# depslib dependency file v1.0
|
||||
1716799438 source:c:\users\lenov\desktop\lab03\histogram.cpp
|
||||
"histogram.h"
|
||||
<vector>
|
||||
<iostream>
|
||||
|
||||
1716797551 c:\users\lenov\desktop\lab03\histogram.h
|
||||
<vector>
|
||||
|
||||
1717268069 source:c:\users\lenov\desktop\lab03\main.cpp
|
||||
<iostream>
|
||||
<vector>
|
||||
"histogram.h"
|
||||
"text.h"
|
||||
"svg.h"
|
||||
|
||||
1716798974 c:\users\lenov\desktop\lab03\text.h
|
||||
<vector>
|
||||
|
||||
1717261318 source:c:\users\lenov\desktop\lab03\text.cpp
|
||||
<iostream>
|
||||
"text.h"
|
||||
<vector>
|
||||
|
||||
1716819918 c:\users\lenov\desktop\lab03\svg.h
|
||||
<vector>
|
||||
|
||||
1716819750 source:c:\users\lenov\desktop\lab03\svg.cpp
|
||||
<iostream>
|
||||
<vector>
|
||||
<string>
|
||||
"svg.h"
|
||||
|
@ -0,0 +1,3 @@
|
||||
10
|
||||
3 3 4 4 4 4 4 5 5 5
|
||||
3
|
@ -0,0 +1,104 @@
|
||||
#include <iostream>
|
||||
#include <vector>
|
||||
#include <string>
|
||||
#include "svg.h"
|
||||
using namespace std;
|
||||
|
||||
const auto IMAGE_WIDTH = 400;
|
||||
const auto IMAGE_HEIGHT = 700;
|
||||
const auto TEXT_LEFT = 20;
|
||||
const auto TEXT_BASELINE = 20;
|
||||
const auto TEXT_WIDTH = 50;
|
||||
const auto BIN_HEIGHT = 30;
|
||||
const auto BLOCK_WIDTH = 10;
|
||||
const auto MAX_WIDTH = IMAGE_WIDTH - TEXT_WIDTH;
|
||||
|
||||
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 = "aaaaaa") {
|
||||
cout << "<rect x='" << x<< "' y='" << y<< "' width='" << width<< "' height='" << height<< "' stroke='" << stroke<< "' fill='" << fill << "' />\n";
|
||||
}
|
||||
|
||||
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";
|
||||
}
|
||||
|
||||
void
|
||||
svg_end() {
|
||||
cout << "</svg>\n";
|
||||
}
|
||||
|
||||
void
|
||||
show_histogram_svg(const vector<size_t>& bins) {
|
||||
const auto MAX_WIDTH = IMAGE_WIDTH - TEXT_WIDTH;
|
||||
size_t max_count = 0;
|
||||
for (size_t x : bins) {
|
||||
if (x > max_count) {
|
||||
max_count = x;
|
||||
}
|
||||
}
|
||||
if (max_count == 0) {
|
||||
max_count = 1;
|
||||
}
|
||||
auto scale_factor = static_cast<double>(MAX_WIDTH) / (max_count * BLOCK_WIDTH);
|
||||
if (scale_factor > 1) {
|
||||
scale_factor = 1;
|
||||
}
|
||||
svg_begin(400, 300);
|
||||
double top = 0;
|
||||
for (size_t bin : bins) {
|
||||
double bin_width = BLOCK_WIDTH * bin * scale_factor;
|
||||
svg_text(TEXT_LEFT, top + TEXT_BASELINE, to_string(bin));
|
||||
svg_rect(TEXT_WIDTH, top, bin_width, BIN_HEIGHT);
|
||||
top += BIN_HEIGHT;
|
||||
}
|
||||
svg_end();
|
||||
}
|
||||
|
||||
void
|
||||
svg_rect_hight(double x, double y, double width, double height, string stroke = "black", string fill = "hfhfh") {
|
||||
cout << "<rect x='" << x<< "' y='" << y<< "' width='" << width<< "' height='" << height<< "' stroke='" << stroke<< "' fill='" << fill << "' />\n";
|
||||
}
|
||||
|
||||
void
|
||||
COLUMN_hight_function( double &COLUMN_height ,size_t bin_count,const vector<size_t>& bins){
|
||||
const auto MAX_WIDTH = IMAGE_WIDTH - TEXT_WIDTH;
|
||||
size_t max_count = 0;
|
||||
for (size_t x : bins) {
|
||||
if (x > max_count) {
|
||||
max_count = x;
|
||||
}
|
||||
}
|
||||
if (max_count == 0) {
|
||||
max_count = 1;
|
||||
}
|
||||
auto scale_factor = static_cast<double>(MAX_WIDTH) / (max_count * BLOCK_WIDTH);
|
||||
if (scale_factor > 1) {
|
||||
scale_factor = 1;
|
||||
}
|
||||
svg_begin(IMAGE_WIDTH, IMAGE_HEIGHT );
|
||||
if ((bin_count*COLUMN_height) > IMAGE_HEIGHT ){
|
||||
|
||||
COLUMN_height= static_cast<double>(IMAGE_HEIGHT) / bin_count;
|
||||
|
||||
}
|
||||
double top = 0;
|
||||
for (size_t bin : bins) {
|
||||
double bin_width = BLOCK_WIDTH * bin * scale_factor;
|
||||
svg_text(TEXT_LEFT, top + TEXT_BASELINE, to_string(bin));
|
||||
svg_rect(TEXT_WIDTH, top, bin_width, COLUMN_height);
|
||||
top += COLUMN_height;
|
||||
}
|
||||
svg_end();
|
||||
|
||||
}
|
@ -0,0 +1,10 @@
|
||||
#ifndef SVG_H_INCLUDED
|
||||
#define SVG_H_INCLUDED
|
||||
|
||||
#include <vector>
|
||||
|
||||
void
|
||||
show_histogram_svg(const std::vector<size_t>& bins);
|
||||
void
|
||||
COLUMN_hight_function( double &COLUMN_height ,size_t bin_count,const std::vector<size_t>& bins);
|
||||
#endif // SVG_H_INCLUDED
|
@ -0,0 +1,43 @@
|
||||
#include <iostream>
|
||||
#include "text.h"
|
||||
#include <vector>
|
||||
|
||||
|
||||
using namespace std;
|
||||
|
||||
void
|
||||
show_histogram_text(const vector<size_t>& bins, size_t& bin_count) {
|
||||
const size_t SCREEN_WIDTH = 80;
|
||||
const size_t MAX_ASTERISK = SCREEN_WIDTH - 3 - 1;
|
||||
size_t max_count = 0;
|
||||
for (size_t s = 0; s < bin_count; s++)
|
||||
{
|
||||
if (bins[s] > max_count)
|
||||
{
|
||||
max_count = bins[s];
|
||||
}
|
||||
}
|
||||
auto kof = static_cast<double>(MAX_ASTERISK) / max_count;
|
||||
if (kof > 1)
|
||||
{
|
||||
kof = 1;
|
||||
}
|
||||
for (size_t bin : bins)
|
||||
{
|
||||
if (bin <100)
|
||||
{
|
||||
cout << " ";
|
||||
}
|
||||
if (bin <10)
|
||||
{
|
||||
cout << " ";
|
||||
}
|
||||
cout << bin << "|";
|
||||
size_t weight = bin * kof;
|
||||
for (size_t t = 0; t < weight; t++)
|
||||
{
|
||||
cout << "*";
|
||||
}
|
||||
cout << endl;
|
||||
}
|
||||
}
|
@ -0,0 +1,9 @@
|
||||
#ifndef TEXT_H_INCLUDED
|
||||
#define TEXT_H_INCLUDED
|
||||
|
||||
#include <vector>
|
||||
|
||||
void
|
||||
show_histogram_text(const std::vector<size_t>& bins, size_t& bin_count);
|
||||
|
||||
#endif // TEXT_H_INCLUDED
|
@ -0,0 +1,45 @@
|
||||
<?xml version="1.0" encoding="UTF-8" standalone="yes" ?>
|
||||
<CodeBlocks_project_file>
|
||||
<FileVersion major="1" minor="6" />
|
||||
<Project>
|
||||
<Option title="unittest" />
|
||||
<Option pch_mode="2" />
|
||||
<Option compiler="gcc" />
|
||||
<Build>
|
||||
<Target title="Debug">
|
||||
<Option output="bin/Debug/unittest" prefix_auto="1" extension_auto="1" />
|
||||
<Option object_output="obj/Debug/" />
|
||||
<Option type="1" />
|
||||
<Option compiler="gcc" />
|
||||
<Compiler>
|
||||
<Add option="-g" />
|
||||
</Compiler>
|
||||
</Target>
|
||||
<Target title="Release">
|
||||
<Option output="bin/Release/unittest" prefix_auto="1" extension_auto="1" />
|
||||
<Option object_output="obj/Release/" />
|
||||
<Option type="1" />
|
||||
<Option compiler="gcc" />
|
||||
<Compiler>
|
||||
<Add option="-O2" />
|
||||
</Compiler>
|
||||
<Linker>
|
||||
<Add option="-s" />
|
||||
</Linker>
|
||||
</Target>
|
||||
</Build>
|
||||
<Compiler>
|
||||
<Add option="-Wall" />
|
||||
</Compiler>
|
||||
<Unit filename="doctest.h" />
|
||||
<Unit filename="histogram.cpp" />
|
||||
<Unit filename="histogram.h" />
|
||||
<Unit filename="svg.cpp" />
|
||||
<Unit filename="svg.h" />
|
||||
<Unit filename="unittest.cbp" />
|
||||
<Unit filename="unittest.cpp" />
|
||||
<Extensions>
|
||||
<lib_finder disable_auto="1" />
|
||||
</Extensions>
|
||||
</Project>
|
||||
</CodeBlocks_project_file>
|
@ -0,0 +1,53 @@
|
||||
#define DOCTEST_CONFIG_NO_MULTITHREADING
|
||||
#define DOCTEST_CONFIG_IMPLEMENT_WITH_MAIN
|
||||
#include "doctest.h"
|
||||
#include "histogram_internal.h"
|
||||
#include "svg.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("distinct negative numbers") {
|
||||
double min = -1;
|
||||
double max = -1;
|
||||
find_minmax({-1, -2}, min, max);
|
||||
CHECK(min == -2);
|
||||
CHECK(max == -1);
|
||||
}
|
||||
TEST_CASE("one number") {
|
||||
double min = 0;
|
||||
double max = 0;
|
||||
find_minmax({1}, min, max);
|
||||
CHECK(min == 1);
|
||||
CHECK(max == 1);
|
||||
}
|
||||
TEST_CASE("same numbers") {
|
||||
double min = 0;
|
||||
double max = 0;
|
||||
find_minmax({2, 2}, min, max);
|
||||
CHECK(min == 2);
|
||||
CHECK(max == 2);
|
||||
}
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
TEST_CASE("Image height < column height") {
|
||||
double COLUMN_height = 220000;
|
||||
COLUMN_hight_function(COLUMN_height ,5,{23,5,54,6,7});
|
||||
CHECK(COLUMN_height == 140 );
|
||||
|
||||
}
|
||||
|
||||
TEST_CASE("Image height > column height ") {
|
||||
double COLUMN_height = 100;
|
||||
COLUMN_hight_function(COLUMN_height ,5,{23,5,54,6,7});
|
||||
CHECK(COLUMN_height == 100 );
|
||||
|
||||
}
|
||||
|
@ -0,0 +1,71 @@
|
||||
# depslib dependency file v1.0
|
||||
1716799438 source:c:\users\lenov\desktop\lab03\histogram.cpp
|
||||
"histogram.h"
|
||||
<vector>
|
||||
<iostream>
|
||||
|
||||
1716797551 c:\users\lenov\desktop\lab03\histogram.h
|
||||
<vector>
|
||||
|
||||
1716828332 source:c:\users\lenov\desktop\lab03\unittest.cpp
|
||||
"doctest.h"
|
||||
"histogram_internal.h"
|
||||
"svg.h"
|
||||
|
||||
1716800315 c:\users\lenov\desktop\lab03\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>
|
||||
|
||||
1716799551 c:\users\lenov\desktop\lab03\histogram_internal.h
|
||||
<vector>
|
||||
|
||||
1716819750 source:c:\users\lenov\desktop\lab03\svg.cpp
|
||||
<iostream>
|
||||
<vector>
|
||||
<string>
|
||||
"svg.h"
|
||||
|
||||
1716819918 c:\users\lenov\desktop\lab03\svg.h
|
||||
<vector>
|
||||
|
@ -0,0 +1,5 @@
|
||||
<?xml version="1.0" encoding="UTF-8" standalone="yes" ?>
|
||||
<CodeBlocks_layout_file>
|
||||
<FileVersion major="1" minor="0" />
|
||||
<ActiveTarget name="Debug" />
|
||||
</CodeBlocks_layout_file>
|
Загрузка…
Ссылка в новой задаче