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

...

7 Коммитов

Автор SHA1 Сообщение Дата
KhnytchenkovAM
2eb0146779 Финальная версия с инд заданием 2023-04-24 00:25:20 +03:00
KhnytchenkovAM
d8705af2f6 Создан сам тест 2023-04-23 20:06:14 +03:00
KhnytchenkovAM
1ff183266d Модульный тест 2023-04-23 19:40:06 +03:00
KhnytchenkovAM
41401e5a9f Пункт 4 ист 2023-04-23 19:03:24 +03:00
KhnytchenkovAM
12b523ef2f Merge branch 'main' of http://uit.mpei.ru/git/KhnytchenkovAM/cs-lab03 2023-04-23 18:59:10 +03:00
KhnytchenkovAM
e22859b6eb Пункт 4 ист 2023-04-23 18:56:10 +03:00
ef3566b21e revert f67263f063
revert Пункт 4
2023-04-23 15:45:03 +00:00
12 изменённых файлов: 7352 добавлений и 1 удалений

7106
doctest.h Обычный файл

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

48
histogram.cpp Обычный файл
Просмотреть файл

@@ -0,0 +1,48 @@
#include "histogram.h"
#include <iostream>
#include <vector>
using namespace std;
void find_minmax( const vector<double>& numbers, double& minN, double& maxN) {
minN = numbers[0];
maxN = numbers[0];
for (double x: numbers){
if (minN > x){
minN = x;
}
if (maxN < x){
maxN = x;
}
}
}
vector<size_t> make_histogram(const vector<double>& numbers, size_t bin_count){
double minN, maxN;
find_minmax( numbers, minN, maxN);
vector <size_t> bins(bin_count);
double diff = (maxN - minN) / bin_count;
size_t max_count = 0;
for (size_t i = 0; i <numbers.size() ; i++){
bool found = false;
for (size_t j = 0;(j < bin_count - 1) && !found; j++){
auto lo = minN + j * diff;
auto hi = minN + (j + 1) * diff;
if ((lo <= numbers[i]) && (hi > numbers[i])){
bins[j]++;
if (bins[j] > max_count){
max_count = bins[j];
}
found = true;
}
}
if(!found){
bins[bin_count - 1]++;
if (bins[bin_count - 1] > max_count){
max_count = bins[bin_count - 1];
}
}
}
return bins;
}

6
histogram.h Обычный файл
Просмотреть файл

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

6
histogram_internal.h Обычный файл
Просмотреть файл

@@ -0,0 +1,6 @@
#ifndef HISTOGRAM_INTERNAL_H_INCLUDED
#define HISTOGRAM_INTERNAL_H_INCLUDED
#include <vector>
void find_minmax( const std::vector<double>& numbers, double& minN, double& maxN);
#endif // HISTOGRAM_INTERNAL_H_INCLUDED

Просмотреть файл

@@ -33,7 +33,13 @@
<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>

Просмотреть файл

@@ -2,6 +2,7 @@
#include <vector>
#include "histogram.h"
#include "text.h"
#include "svg.h"
using namespace std;
struct Input {
@@ -29,6 +30,6 @@ int main(){
size_t max_count;
auto in = input_data();
auto bins = make_histogram(in.numbers, in.bin_count);
show_histogram_text(bins,in.bin_count, max_count);
show_histogram_svg(bins);
return 0;
}

63
svg.cpp Обычный файл
Просмотреть файл

@@ -0,0 +1,63 @@
#include "svg.h"
#include <string>
using namespace std;
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 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 = "black")
{
cout << "<rect x='" << x << "' y='" << y << "' width='" << width
<< "' height='" << height << "' stroke='" << stroke << "' fill='" << fill << " '/>";
}
void show_histogram_svg(const vector<size_t> &bins){
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;
svg_begin(400, 300);
double top = 0;
size_t max_count = 0;
for (auto bin : bins)
{
if (bin > max_count)
max_count = bin;
}
const auto BLOCK_WIDTH = (IMAGE_WIDTH - TEXT_WIDTH) / max_count;
for (size_t bin : bins)
{
string color;
cout<< "color"<<bin<< "=";
cin>>color;
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, "blue", color);
top += BIN_HEIGHT;
}
svg_end();
}

8
svg.h Обычный файл
Просмотреть файл

@@ -0,0 +1,8 @@
#ifndef SVG_H_INCLUDED
#define SVG_H_INCLUDED
#include <iostream>
#include <vector>
void show_histogram_svg(const std::vector<size_t> &bins);
#endif // SVG_H_INCLUDED

41
text.cpp Обычный файл
Просмотреть файл

@@ -0,0 +1,41 @@
#include "text.h"
#include <iostream>
#include <vector>
using namespace std;
void show_histogram_text(const vector <size_t>& bins,size_t bin_count, size_t max_count){
const size_t SCREEN_WIDTH = 80;
const size_t MAX_ASTERISK = SCREEN_WIDTH - 6 - 1;
bool scaling = false;
if (max_count > MAX_ASTERISK){
scaling = true;
}
for (size_t i = 0; i < bin_count; i++){
cout << " ";
if (bins[i] < 100){
cout << ' ';
}
if (bins[i] < 10){
cout << ' ';
}
cout << bins[i] << '|';
size_t number_of_stars = bins[i];
if (scaling){
if (bins[i] == max_count){
number_of_stars = MAX_ASTERISK * 1.0;
}
else {
number_of_stars = MAX_ASTERISK * (static_cast<double>(bins[i]) / max_count);
}
}
for (size_t j = 0; j < number_of_stars; j++){
cout << '*';
}
cout << endl;
}
}

6
text.h Обычный файл
Просмотреть файл

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

47
unittest.cbp Обычный файл
Просмотреть файл

@@ -0,0 +1,47 @@
<?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="histogram_internal.h" />
<Unit filename="svg.cpp" />
<Unit filename="svg.h">
<Option target="&lt;{~None~}&gt;" />
</Unit>
<Unit filename="unittest.cpp" />
<Extensions>
<lib_finder disable_auto="1" />
</Extensions>
</Project>
</CodeBlocks_project_file>

13
unittest.cpp Обычный файл
Просмотреть файл

@@ -0,0 +1,13 @@
#define DOCTEST_CONFIG_NO_MULTITHREADING
#define DOCTEST_CONFIG_IMPLEMENT_WITH_MAIN
#include "doctest.h"
#include "histogram_internal.h"
#include "histogram.h"
TEST_CASE("distinct positive numbers") {
double minN = 0;
double maxN = 0;
find_minmax({1, 2}, minN, maxN);
CHECK(minN == 1);
CHECK(maxN == 2);
}