FilippovDY 11 месяцев назад
Родитель 22e08626cb
Сommit b9dd8e9a21

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

@ -0,0 +1,4 @@
/bin
/obj
/test.layout

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

Разница между файлами не показана из-за своего большого размера Загрузить разницу

@ -0,0 +1,46 @@
#include <iostream>
#include <vector>
#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,5 @@
#pragma once
#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,4 @@
#pragma once
#include <vector>
void find_minmax(const std::vector<double>& numbers, double& min, double& max);

@ -0,0 +1,51 @@
#include <iostream>
#include <vector>
#include "histogram.h"
#include "text.h"
#include "svg.h"
using namespace std;
struct Input {
vector<double> numbers;
size_t bin_count{};
size_t number_count{};
size_t max_count{};
};
Input
input_data() {
int x, y, z;
int* p = &x;
*p = 1;
p = &y;
Input in;
cerr << "Enter number count: ";
cin >> in.number_count;
vector<double> numbers(in.number_count);
in.numbers.resize(in.number_count);
for (size_t i = 0; i < in.number_count; i++) {
cin >> in.numbers[i];
}
size_t bin_count;
cerr << "Enter bin count: ";
cin >> in.bin_count;
size_t max_count;
in.max_count = 0;
return in;
}
int main()
{
auto in = input_data();
auto bins = make_histogram(in.numbers, in.bin_count, in.number_count, in.max_count);
vector<double> test;
show_histogram_svg(bins, in.max_count, in.bin_count, test);
return 0;
}

@ -0,0 +1,77 @@
#include "iostream"
#include "svg.h"
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;
void svg_begin(double width, double height, std::vector <double>& test)
{
test.push_back(width);
test.push_back(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 stroke = "black", std::string fill = "red")
{
std::cout<< "<rect x='"<< x <<"' y='"<< y <<"' width='"<< width <<"' height='"<< height<<"' stroke='black' fill='red' ></rect>";
}
void svg_line(double x, double y, double x2, double y2, std::vector <double>& test)
{
test.push_back(x);
test.push_back(y);
test.push_back(x2);
test.push_back(y2);
std::cout<<"<line x1='"<< x <<"' y1='"<< y <<"' x2='"<< x2 <<"' y2='"<< y2 <<"' stroke-dasharray = '10 10' stroke='black';stroke-width:4' />";
}
void
show_histogram_svg(const std::vector<size_t>& bins, size_t & max_count, size_t & bin_count, std::vector <double>& test)
{
const auto BLOCK_WIDTH = (IMAGE_WIDTH - TEXT_WIDTH)/max_count;
svg_begin(IMAGE_WIDTH+4, IMAGE_HEIGHT, test);
double top = 4;
svg_line(TEXT_LEFT, top, IMAGE_WIDTH+5, top, test);
svg_line(TEXT_LEFT-10, top, TEXT_LEFT-10, top+3*BIN_HEIGHT, test);
svg_line(IMAGE_WIDTH+5, top, IMAGE_WIDTH+5, 2*top+3*BIN_HEIGHT, test);
svg_line(TEXT_LEFT, 2*top+3*BIN_HEIGHT, IMAGE_WIDTH+5, 2*top+3*BIN_HEIGHT, test);
for (double t : test)
{
std:: cout << t<< '\n';
}
for (size_t bin : bins)
{
const double bin_width = BLOCK_WIDTH * bin;
svg_text(TEXT_LEFT, top + TEXT_BASELINE, std::to_string(bin));
svg_rect(TEXT_WIDTH+2, top+2, bin_width, BIN_HEIGHT);
top += BIN_HEIGHT;
}
svg_end();
}
void
svg_end()
{
std::cout << "</svg>\n";
}

10
svg.h

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

@ -0,0 +1,34 @@
#include <iostream>
#include <vector>
#include "text.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,44 @@
<?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.h" />
<Unit filename="histogram_internal.h" />
<Unit filename="svg.cpp" />
<Unit filename="svg.h" />
<Unit filename="unittest.cpp" />
<Extensions>
<lib_finder disable_auto="1" />
</Extensions>
</Project>
</CodeBlocks_project_file>

@ -0,0 +1,46 @@
#define DOCTEST_CONFIG_NO_MULTITHREADING
#define DOCTEST_CONFIG_IMPLEMENT_WITH_MAIN
#include "doctest.h"
#include "histogram_internal.h"
#include <vector>
#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("NULL vector") {
double min = 0;
double max = 0;
find_minmax({1,2}, min, max);
CHECK(min == 1);
CHECK(max == 2);
}
TEST_CASE("vector with one elements") {
double min = 0;
double max = 0;
find_minmax({1, 1}, min, max);
CHECK(min == 1);
CHECK(max == 1);
}
TEST_CASE("distinct negative numbers") {
double min = 0;
double max = 0;
find_minmax({-1, -2}, min, max);
CHECK(min == -2);
CHECK(max == -1);
}
TEST_CASE("svg_check") {
size_t max_count = 5;
size_t bin_count = 3;
std::vector <double> test;
std::vector<size_t> bins = {2, 5, 3};
std::vector <double> prover = {404, 300, 20, 4, 405, 4, 10, 4, 10, 94, 405, 4, 405, 98, 20, 98, 405, 98};
show_histogram_svg(bins, max_count, bin_count, test);
CHECK(test==prover);
}
Загрузка…
Отмена
Сохранить