Сравнить коммиты

..

10 Коммитов

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

@ -0,0 +1,6 @@
/bin
/obj
lab03.depend
unittest.depend
unittest.layout
unittest.o

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

@ -0,0 +1,7 @@
#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

@ -2,7 +2,7 @@
#include <vector> #include <vector>
#include "histogram.h" #include "histogram.h"
#include "text.h" #include "text.h"
#include "svg.h"
using namespace std; using namespace std;
@ -35,8 +35,11 @@ input_data() {
int main() { int main() {
std::string fill = "red";
std::string stroke = "#000000";
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_text(bins, in.bin_count); show_histogram_svg(bins, fill, stroke);
return 0; return 0;
} }

@ -0,0 +1,76 @@
#include "svg.h"
#include <iostream>
#include <string>
#include <vector>
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_end() {
std::cout << "</svg>\n";
}
void
svg_text(double left, double baseline, std::string text) {
std::cout << "<text x='" << left << "' y='" << baseline << "'>" << text << "</text>\n";
}
void svg_rect(double x, double y, double width, double height, std::string stroke, std::string fill){
std::cout << "<rect x='" << x << "' y='" << y << "' width='" << width << "' height='" << height
<< "' stroke='" << stroke << "' fill='" << fill << "' />\n";
}
void
show_histogram_svg(const std::vector<size_t>& bins, const std::string& fill, const std::string& stroke) {
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 = 10;
const size_t MAX_ASTERISK = IMAGE_WIDTH - TEXT_WIDTH;
svg_begin(IMAGE_WIDTH, IMAGE_HEIGHT);
size_t maxlen = 0;
for (size_t len : bins)
{
if (maxlen<len) maxlen = len;
}
double top = 0;
for (size_t bin : bins) {
size_t wight = bin;
if ((maxlen*10) > MAX_ASTERISK){
if (maxlen != bin) wight = MAX_ASTERISK * (static_cast <double> (bin/maxlen));
else if (maxlen == bin) wight = MAX_ASTERISK;
}
const double bin_width = BLOCK_WIDTH * wight;
svg_text(TEXT_LEFT, top + TEXT_BASELINE, std::to_string(bin));
svg_rect(TEXT_WIDTH, top, bin_width, BIN_HEIGHT, stroke, fill);
top += BIN_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));
svg_rect(TEXT_WIDTH, top, bin_width, BIN_HEIGHT, stroke, fill);
top += BIN_HEIGHT;
}
//svg_text(20, 20, std::to_string(bins[0]));
//svg_rect(50, 0, bins[0] * 10, 30);*/
svg_end();
}

12
svg.h

@ -0,0 +1,12 @@
#ifndef SVG_H_INCLUDED
#define SVG_H_INCLUDED
#include <string>
#include <vector>
void svg_begin(double width, double height);
void svg_end();
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 show_histogram_svg(const std::vector<size_t>& bins, const std::string& fill, const std::string& stroke);
#endif // SVG_H_INCLUDED

@ -0,0 +1,31 @@
#include <iostream>
#include <vector>
#include "text.h"
void show_histogram_text(std::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 = bins[0];
for (size_t i = 0; i < bin_count; i++)
{
if (bins[i] > max_count)
max_count = bins[i];
}
// Ñîçäàíèå ãèñòîãðàììû ïî îöåíêàì.
size_t height;
for (size_t i = 0; i < bin_count; i++) {
std::cout << bins[i] << "|";
height = bins[i];
if (max_count > MAX_ASTERISK)
height = MAX_ASTERISK * (static_cast<double>(bins[i]) / max_count);
for (size_t j = 1; j <= height; j++)
std::cout << "*";
std::cout << std::endl;
}
}

@ -0,0 +1,8 @@
#ifndef TEXT_H_INCLUDED
#define TEXT_H_INCLUDED
#include <vector>
void show_histogram_text(std::vector<size_t>& bins, size_t bin_count);
#endif // TEXT_H_INCLUDED

@ -0,0 +1,38 @@
<?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>
<Extensions>
<lib_finder disable_auto="1" />
</Extensions>
</Project>
</CodeBlocks_project_file>

@ -0,0 +1,44 @@
#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 == 1);
CHECK(max == 2);
}
TEST_CASE("empty vector") {
double min = 0;
double max = 0;
find_minmax({}, min, max);
CHECK(min == 0);
CHECK(max == 0);
}
TEST_CASE("single element vector") {
double min = 0;
double max = 0;
find_minmax({5}, min, max);
CHECK(min == 5);
CHECK(max == 5);
}
TEST_CASE("negative numbers") {
double min = 0;
double max = 0;
find_minmax({-3, -1}, min, max);
CHECK(min == -3);
CHECK(max == -1);
}
TEST_CASE("identical elements") {
double min = 0;
double max = 0;
find_minmax({4, 4, 4}, min, max);
CHECK(min == 4);
CHECK(max == 4);
}
Загрузка…
Отмена
Сохранить