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

...

3 Коммитов

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

@ -2,3 +2,11 @@
/obj
/cs-lab34.layout
/cs-lab34.depend
/a.out
/start_01.sh
/test
/unittest.depend
/NUL
/unittest.layout
/start.sh
/cs-lab34

@ -32,7 +32,14 @@
<Add option="-Wall" />
<Add option="-fexceptions" />
</Compiler>
<Unit filename=".gitignore" />
<Unit filename="histogram.cpp" />
<Unit filename="histogram.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>

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

@ -0,0 +1,56 @@
#include "histogram.h"
#include "histogram_internal.h"
void
find_minmax (const std::vector<double>& numbers, double& min, double& max) {
if (numbers.size() == 0) { //empty array situation check
return;
} else {
min = numbers[0];
for (double element : numbers) {
if (element < min) {
min = element;
}
}
max = numbers[0];
for (double element : numbers) {
if (element > max) {
max = element;
}
}
return;
}
}
std::vector<std::size_t>
make_histogram (const std::vector<double>& numbers, std::size_t bin_count) {
double max;
double min;
find_minmax(numbers, min, max);
double bin_size = static_cast<double>(max - min) / (bin_count);
std::vector <std::size_t> bins(bin_count);
for (std::size_t i = 0; i < numbers.size(); ++i) { //Checking if a number is in a bin.
bool found = false;
for (std::size_t j = 0; (j < bin_count - 1) && !found; ++j) {
if ( (numbers[i] >= (min + j * bin_size)) && //Where (min + j * bin_size) is equal to the lower border
(numbers[i] < (min + (j + 1) * bin_size)) ) { //and (min + (j + 1) * bin_size) is equal to the higher border.
++bins[j];
found = true;
}
}
if (!found) {
++bins[bin_count - 1]; //A special case when current number is equal to the maximum.
}
}
return bins;
}

@ -0,0 +1,9 @@
#ifndef HISTOGRAM_H_INCLUDED
#define HISTOGRAM_H_INCLUDED
#endif // HISTOGRAM_H_INCLUDED
#include <vector>
std::vector<std::size_t>
make_histogram(const std::vector<double>& numbers, std::size_t bin_count);

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

@ -3,144 +3,49 @@
#include <iostream>
#include <vector>
#include <math.h>
using namespace std;
double find_ext(vector <double> array, short key)
{
double ext = array[0]; //Key "1" keeps the comparison the same.
for(double element : array) //Key "-1" reverses the comparison.
{
if(key * element > key * ext) {ext = element;}
}
return ext;
}
size_t find_ext(vector <size_t> array, short key)
{
size_t ext = array[0]; //Key "1" keeps the comparison the same.
for(double element : array) //Key "-1" reverses the comparison.
{
if(key * element > key * ext) {ext = element;}
}
return ext;
}
#include "histogram.h"
#include "text.h"
#include "svg.h"
using namespace std;
int main() {
const size_t screen_width = 80;
const size_t max_asterisk = screen_width - 3 - 1;
struct Input {
vector<double> numbers;
size_t bin_count{};
size_t interval_task{};
};
size_t number_count; //Input variable.
cerr << "Enter number count: ";
Input
input_data() {
size_t number_count;
cin >> number_count;
vector<double> numbers(number_count);
for (size_t i = 0; i < number_count; ++i) { cin >> numbers[i]; }
size_t bin_count;
cerr << "Enter bin count: ";
cin >> bin_count;
Input in;
double max = find_ext(numbers, 1); //Find min, max and bin size.
double min = find_ext(numbers, -1); //Key 1 stands for maximum, key -1 stands for minimum.
double bin_size = static_cast<double>(max - min) / (bin_count);
vector <size_t> bins(bin_count);
for(size_t i = 0; i < number_count; ++i) //Checking if a number is in a bin.
{
bool found = false;
for(size_t j = 0; (j < bin_count - 1) && !found; ++j)
{
if( (numbers[i] >= (min + j * bin_size)) && //Where (min + j * bin_size) is equal to the lower border
(numbers[i] < (min + (j + 1) * bin_size)) ) //and (min + (j + 1) * bin_size) is equal to the higher border.
{
++bins[j];
found = true;
}
}
if(!found) {++bins[bin_count - 1];} //A special case when current number is equal to the maximum.
in.numbers.resize(number_count);
for (size_t i = 0; i < number_count; i++) {
cin >> in.numbers[i];
}
cin >> in.bin_count;
cin >> in.interval_task;
size_t max_bin = find_ext(bins, 1); //Finds a bin with the maximum size. Key 1 stands for maximum.
double modifier;
/*
* In case when maximum bin > 76, histogram will be scaled according to
* the formula "max_asterisk * (static_cast<double>(bins[i]) / max_bin)"
* or "bins[i] * (static_cast<double>(max_asterisk) / max_bin)".
* In other case, histogram won't be scaled, i.e, asterisk count depends on the current bin number.
*/
if(max_bin > 76) {modifier = static_cast<double>(max_asterisk) / (max_bin);}
else {modifier = 1;}
for(long unsigned int i = 0; i < bin_count; ++i) //Histogram output with alignment, if necessary.
{
if(bins[i] >= 10)
{
if(bins[i] >= 100) {cout << bins[i] << '|';} //Output a three-digit number.
else {cout << ' ' << bins[i] << '|';} //Output a two-digit number with alignment.
if ((in.interval_task >= 2.0) && (in.interval_task <= 9.0)) {
return in;
} else {
std::cout << "ERROR";
exit(1);
}
else {cout << " " << bins[i] << '|';} //Output a single-digit number with alignment.
size_t height = modifier * bins[i]; //Height stands for the number of output asterisks.
for(long unsigned int k = 0; k < height; ++k)
cout << '*';
cout << "\n";
}
//Task 15.
size_t interval_task;
cerr << "Enter interval size: ";
cin >> interval_task;
if((interval_task >= 4) && (interval_task <= 9)) //The scale under the histogram.
{
size_t times;
if(modifier == 1) {times = static_cast<size_t>(ceil(max_bin / interval_task) + 1);}
else {times = static_cast<size_t>(ceil(max_asterisk / interval_task) + 1);}
cout << " |"; //1st row output.
for(long unsigned int i = 0; i < times; ++i)
{
for(long unsigned int k = 0; k < interval_task - 1; ++k)
cout << '*';
cout << '|';
}
cout << '\n';
cout << " " << 0; //2nd row output.
for(long unsigned int i = 0; i < (interval_task - 1); ++i) //Distance from the first axis to the second.
cout << ' ';
cout << interval_task;
// (Number of intervals between the second and last * Interval size) - Last axis.
if(times > 1)
{
for(long unsigned int i = 0; i < (times - 1) * (interval_task) - 1; ++i)
cout << ' ';
cout << times * interval_task;
}
}
else {cout << "ERROR";}
int main() {
auto in = input_data();
auto bins = make_histogram(in.numbers, in.bin_count);
show_histogram_svg(bins, in.interval_task);
return 0;
}

@ -0,0 +1,3 @@
<?xml version='1.0' encoding='UTF-8'?>
<svg width='410' height='300' viewBox='0 0 410 300' xmlns='http://www.w3.org/2000/svg'>
<text x='20' y='20'>9</text><rect x='50' y='0' width='31.5' height='30' stroke='#483D8B' fill='#9370DB' /><text x='20' y='50'>33</text><rect x='50' y='30' width='115.5' height='30' stroke='#483D8B' fill='#9370DB' /><text x='20' y='80'>100</text><rect x='50' y='60' width='350' height='30' stroke='#483D8B' fill='#9370DB' /><rect x='50' y='100' width='60' height='8' stroke='chocolate' fill='tan' /><rect x='122.5' y='100' width='60' height='8' stroke='chocolate' fill='tan' /><rect x='195' y='100' width='60' height='8' stroke='chocolate' fill='tan' /><rect x='267.5' y='100' width='60' height='8' stroke='chocolate' fill='tan' /><rect x='340' y='100' width='60' height='8' stroke='chocolate' fill='tan' /><text x='20' y='130'>0</text><text x='112.25' y='130'>6</text><text x='392' y='130'>30</text></svg>

После

Ширина:  |  Высота:  |  Размер: 933 B

@ -0,0 +1,6 @@
142
1 1 1 1 1 1 1 1 1
2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2
3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3
3
6

@ -0,0 +1,100 @@
#include <iostream>
#include "svg.h"
#include <string.h>
#include <math.h>
static void
find_max (const std::vector<std::size_t>& numbers, std::size_t& max) {
max = numbers[0];
for (double element : numbers) {
if (element > max) {
max = element;
}
}
}
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>";
}
void
svg_rect (double x, double y, double width, double height, std::string stroke = "black", std::string fill = "black") {
std::cout << "<rect x='" << x << "' y='" << y << "' width='" << width << "' height='" << height << "' stroke='" << stroke << "' fill='" << fill << "' />";
}
void
show_histogram_svg (const std::vector<size_t>& bins, std::size_t& interval_task) {
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 = 50;
const auto MAX_WIDTH = IMAGE_WIDTH - TEXT_WIDTH;
//const auto SPACE_BETWEEN_INTERVALS = 10;
const auto INTERVAL_SCALED = interval_task * 10;
const auto SCALE_WIDTH = 8;
//const auto HALF_SPACE_BETWEEN_INTERVALS = static_cast<double>(SPACE_BETWEEN_INTERVALS) / (2);
const auto HALF_DEFAULT_FONT = 4;
const auto MINIMAL_FONT_SPACE = 4 * 4;
std::size_t max_bin;
find_max(bins, max_bin);
double modifier;
modifier = static_cast<double>(MAX_WIDTH) / (max_bin);
size_t times;
for (times = 0; (times * INTERVAL_SCALED) < (MAX_WIDTH - MINIMAL_FONT_SPACE); ++times);
double interval_space = static_cast<double>(MAX_WIDTH - INTERVAL_SCALED * (times - 1)) / (times - 2);
double half_interval_space = interval_space / 2;
svg_begin(410, 300);
double top = 0;
for (size_t bin : bins) {
const double bin_width = modifier * bin;
svg_text(TEXT_LEFT, top + TEXT_BASELINE, std::to_string(bin));
svg_rect(TEXT_WIDTH, top, bin_width, BIN_HEIGHT, "#483D8B", "#9370DB");
top += BIN_HEIGHT;
}
top += 10;
times -= 1;
for (size_t i = 0; i < times; ++i) {
svg_rect(TEXT_WIDTH + i * (INTERVAL_SCALED + interval_space), top, INTERVAL_SCALED, SCALE_WIDTH, "chocolate", "tan");
}
top += 10;
svg_text(TEXT_LEFT, top + TEXT_BASELINE, std::to_string(0));
svg_text(TEXT_WIDTH + INTERVAL_SCALED + half_interval_space - HALF_DEFAULT_FONT, top + TEXT_BASELINE, std::to_string(interval_task));
svg_text(TEXT_WIDTH + (INTERVAL_SCALED * times) + interval_space * (times - 1) - (HALF_DEFAULT_FONT * 2), top + TEXT_BASELINE, std::to_string(interval_task * times));
svg_end();
}

@ -0,0 +1,4 @@
#include <vector>
void
show_histogram_svg(const std::vector<std::size_t>& bins, std::size_t& interval_task);

@ -0,0 +1,55 @@
#include <iostream>
#include "text.h"
const size_t screen_width = 80;
const size_t max_asterisk = screen_width - 3 - 1;
static void
find_max (const std::vector<std::size_t>& numbers, std::size_t& max) {
max = numbers[0];
for (double element : numbers) {
if (element > max) {
max = element;
}
}
}
void
show_histogram_text (const std::vector<std::size_t>& bins) {
std::size_t max_bin;
find_max(bins, max_bin);
double modifier;
/*
* In case when maximum bin > 76, histogram will be scaled according to
* the formula "max_asterisk * (static_cast<double>(bins[i]) / max_bin)"
* or "bins[i] * (static_cast<double>(max_asterisk) / max_bin)".
* In other case, histogram won't be scaled, i.e, asterisk count depends on the current bin number.
*/
if (max_bin > max_asterisk) {
modifier = static_cast<double>(max_asterisk) / (max_bin);
} else {
modifier = 1;
}
for (std::size_t i = 0; i < bins.size(); ++i) { //Histogram output with alignment, if necessary.
if (bins[i] >= 10) {
if (bins[i] >= 100) {
std::cout << bins[i] << '|';
} //Output a three-digit number.
else {
std::cout << ' ' << bins[i] << '|';
} //Output a two-digit number with alignment.
} else {
std::cout << " " << bins[i] << '|';
} //Output a single-digit number with alignment.
size_t height = modifier * bins[i]; //Height stands for the number of output asterisks.
for (size_t k = 0; k < height; ++k) {
std::cout << '*';
}
std::cout << "\n";
}
}

@ -0,0 +1,4 @@
#include <vector>
void
show_histogram_text(const std::vector<std::size_t>& bins);

@ -0,0 +1,40 @@
<?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_internal.h" />
<Unit filename="unittest.cpp" />
<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"
TEST_CASE("distinct positive numbers") {
double min = 0;
double max = 0;
find_minmax({1, 3}, min, max);
CHECK(min == 1);
CHECK(max == 3);
}
TEST_CASE("empty vector") {
double min = 0;
double max = 0;
find_minmax({}, min, max);
CHECK(min == 0);
CHECK(max == 0);
}
TEST_CASE("vectors with one element") {
double min = 0;
double max = 0;
find_minmax({9}, min, max);
CHECK(min == 9);
CHECK(max == 9);
}
TEST_CASE("distinct negative numbers") {
double min = 0;
double max = 0;
find_minmax({-3, -5}, min, max);
CHECK(min == -5);
CHECK(max == -3);
}
TEST_CASE("vectors of identical elements") {
double min = 0;
double max = 0;
find_minmax({5, 5, 5, 5}, min, max);
CHECK(min == 5);
CHECK(max == 5);
}
Загрузка…
Отмена
Сохранить