Dana 1 год назад
Родитель b0f8939ed6
Сommit 30cb7497eb

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

@ -0,0 +1,6 @@
/bin
/obj
/lab03.layout
/unittest2.layout
/unittest1.layout
/unittest.layout

@ -0,0 +1,45 @@
#include "histogram.h"
void
find_minmax(const std::vector<double>& numbers, double& min, double& max) {
min = numbers[0];
max = numbers[0];
for (double x : numbers)
{
if (x < min){
min = x;
}
else if (x > max){
max = x;
}
}
}
std::vector <size_t>
make_histogram(const std::vector<double>& numbers, size_t & bin_count, size_t & number_count, size_t & max_count) {
double min, max;
find_minmax(numbers, min, max);
double bin_size = (max - min) / bin_count;
std::vector<size_t> bins(bin_count);
max_count = bins[0];
for (size_t i = 0; i < number_count; i++){
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 <= numbers[i]) && (numbers[i] < hi)){
bins[j]++;
found = true;
}
if (bins[j] > max_count){
max_count = bins[j];
}
}
if (!found){
bins[bin_count - 1]++;
}
if (bins[bin_count - 1] > max_count){
max_count = bins[bin_count - 1];
}
}
return bins;
}

@ -0,0 +1,6 @@
#pragma once
#include <iostream>
#include <vector>
std::vector<size_t>
make_histogram(const std::vector<double>& numbers, size_t & bin_count, size_t & number_count, size_t & max_count);

@ -0,0 +1,6 @@
#pragma once
#include <iostream>
#include <vector>
void
find_minmax(const std::vector<double>& numbers, double& min, double& max);

@ -32,10 +32,12 @@
<Add option="-Wall" />
<Add option="-fexceptions" />
</Compiler>
<Unit filename=".gitignore" />
<Unit filename="histogram.cpp" />
<Unit filename="histogram.h">
<Option target="&lt;{~None~}&gt;" />
</Unit>
<Unit filename="histogram_internal.h" />
<Unit filename="main.cpp" />
<Unit filename="svg.cpp" />
<Unit filename="svg.h" />

@ -0,0 +1,29 @@
# depslib dependency file v1.0
1713734525 source:c:\users\Äàíà\desktop\lab03\main.cpp
<iostream>
<vector>
"histogram.h"
"text.h"
"svg.h"
1712947093 c:\users\Äàíà\desktop\lab03\histogram.h
<iostream>
<vector>
1713650480 source:c:\users\Äàíà\desktop\lab03\histogram.cpp
"histogram.h"
1713561842 source:c:\users\Äàíà\desktop\lab03\text.cpp
"histogram.h"
1712947492 c:\users\Äàíà\desktop\lab03\text.h
<iostream>
<vector>
1713734318 c:\users\Äàíà\desktop\lab03\svg.h
<iostream>
<vector>
1713714876 source:c:\users\Äàíà\desktop\lab03\svg.cpp
"svg.h"

@ -0,0 +1,48 @@
#include "svg.h"
void
svg_begin(double width, double height) {
std::cout << "<?xml version='1.0' encoding='UTF-8'?>\n";
std::cout << "<svg ";
std::cout << "width='" << width << "' ";
std::cout << "height='" << height << "' ";
std::cout << "viewBox='0 0 " << width << " " << height << "' ";
std::cout << "xmlns='http://www.w3.org/2000/svg'>\n";
}
void
svg_text(double left, double baseline, std::string text) {
std::cout << "<text x='"<< left <<"' y='"<< baseline <<"'>"<< text <<"</text>";
}
void svg_rect(double x, double y, double width, double height, std::string fill_opacity, std::string stroke = "black", std::string fil = "black"){
std::cout<< "<rect x='"<< x <<"' y='"<< y <<"' width='"<< width <<"' height='"<< height <<"' fill-opacity='"<< fill_opacity<<"' />";
}
void
show_histogram_svg(std::vector<size_t>& bins, size_t & max_count,size_t & bin_count) {
const auto IMAGE_WIDTH = 400;
const auto IMAGE_HEIGHT = 300;
const auto TEXT_LEFT = 20;
const auto TEXT_BASELINE = 20;
const auto TEXT_WIDTH = 50;
const auto BIN_HEIGHT = 30;
const auto BLOCK_WIDTH = (IMAGE_WIDTH - TEXT_WIDTH)/max_count;
svg_begin(IMAGE_WIDTH, IMAGE_HEIGHT);
double top = 0;
for (size_t bin : bins) {
const double bin_width = BLOCK_WIDTH * bin;
svg_text(TEXT_LEFT, top + TEXT_BASELINE, std::to_string(bin));
double fill_opacity=double(bin) / max_count;
svg_rect(TEXT_WIDTH, top, bin_width, BIN_HEIGHT, std::to_string(fill_opacity));
top += BIN_HEIGHT;
}
svg_end();
}
void
svg_end() {
std::cout << "</svg>\n";
}

13
svg.h

@ -0,0 +1,13 @@
#pragma once
#include <iostream>
#include <vector>
void
show_histogram_svg(std::vector<size_t>& bins, size_t & max_count,size_t & bin_count);
void
svg_begin(double width, double height);
void
svg_text(double left, double baseline, std::string text);
void
svg_rect(double x, double y, double width, double height, double fill_opacity, std::string stroke, std::string fil);
void
svg_end();

@ -0,0 +1,33 @@
#include "histogram.h"
void
show_histogram_text(std::vector<size_t>& bins, size_t & max_count,size_t & bin_count){
const size_t SCREEN_WIDTH = 80;
const size_t MAX_ASTERISK = SCREEN_WIDTH - 3 - 1;
if (max_count > MAX_ASTERISK){
std::vector<size_t> hights(bin_count);
for (size_t i = 0; i < bin_count; i++){
size_t height = MAX_ASTERISK * (static_cast<double>(bins[i]) / max_count);
hights[i] = height;
}
for (size_t i = 0; i < bin_count; i++){
printf("%3d|", bins[i]);
for (size_t j = 0; j < hights[i]; j++){
std::cout<<"*";
}
std::cout << std::endl;
}
}
else{
for (size_t i = 0; i < bin_count; i++)
{
printf("%3d|", bins[i]);
for (size_t j = 0; j < bins[i]; j++){
std::cout<<"*";
}
std::cout << std::endl;
}
}
}

@ -0,0 +1,6 @@
#pragma once
#include <iostream>
#include <vector>
void
show_histogram_text(std::vector<size_t>& bins, size_t & max_count,size_t & bin_count);

@ -0,0 +1,41 @@
<?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="histogram.cpp" />
<Unit filename="histogram_internal.h" />
<Unit filename="unittest.cpp" />
<Extensions>
<lib_finder disable_auto="1" />
</Extensions>
</Project>
</CodeBlocks_project_file>

@ -0,0 +1,12 @@
#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}, min, max);
CHECK(min == 1);
CHECK(max == 1);
}

@ -0,0 +1,61 @@
# depslib dependency file v1.0
1713650480 source:c:\users\Äàíà\desktop\lab03\histogram.cpp
"histogram.h"
1712947093 c:\users\Äàíà\desktop\lab03\histogram.h
<iostream>
<vector>
1713715686 source:c:\users\Äàíà\desktop\lab03\unittest.cpp
"doctest.h"
"histogram_internal.h"
1713636110 c:\users\Äàíà\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>
1712947904 c:\users\Äàíà\desktop\lab03\histogram_internal.h
<iostream>
<vector>

@ -0,0 +1,41 @@
<?xml version="1.0" encoding="UTF-8" standalone="yes" ?>
<CodeBlocks_project_file>
<FileVersion major="1" minor="6" />
<Project>
<Option title="unittest1" />
<Option pch_mode="2" />
<Option compiler="gcc" />
<Build>
<Target title="Debug">
<Option output="bin/Debug/unittest1" 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/unittest1" 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="histogram.cpp" />
<Unit filename="histogram_internal.h" />
<Unit filename="unittest1.cpp" />
<Extensions>
<lib_finder disable_auto="1" />
</Extensions>
</Project>
</CodeBlocks_project_file>

@ -0,0 +1,12 @@
#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 == -2);
CHECK(max == -1);
}

@ -0,0 +1,41 @@
<?xml version="1.0" encoding="UTF-8" standalone="yes" ?>
<CodeBlocks_project_file>
<FileVersion major="1" minor="6" />
<Project>
<Option title="unittest2" />
<Option pch_mode="2" />
<Option compiler="gcc" />
<Build>
<Target title="Debug">
<Option output="bin/Debug/unittest2" 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/unittest2" 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="histogram.cpp" />
<Unit filename="histogram_internal.h" />
<Unit filename="unittest2.cpp" />
<Extensions>
<lib_finder disable_auto="1" />
</Extensions>
</Project>
</CodeBlocks_project_file>

@ -0,0 +1,12 @@
#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,1}, min, max);
CHECK(min == 1);
CHECK(max == 1);
}
Загрузка…
Отмена
Сохранить