Сравнить коммиты
17 Коммитов
4ce9b8e2e3
...
main
| Автор | SHA1 | Дата | |
|---|---|---|---|
| 81ff18f02d | |||
| f37d1cd25c | |||
| 332a78f708 | |||
| a60b3a0445 | |||
| adcd8e719a | |||
| 76c58a3c2e | |||
| e28c2321bc | |||
| f7e2afa797 | |||
| 30a95a6811 | |||
| c7252e212a | |||
| 3a489a685e | |||
| 8c826cf9d8 | |||
| c53dd6003b | |||
| d387fc82a9 | |||
| f5deacc04b | |||
| 87a1a8b96f | |||
| c681474dbc |
24
Lab1.cbp
24
Lab1.cbp
@@ -13,7 +13,11 @@
|
||||
<Option compiler="gcc" />
|
||||
<Compiler>
|
||||
<Add option="-g" />
|
||||
<Add directory="include" />
|
||||
</Compiler>
|
||||
<Linker>
|
||||
<Add option="-static-libstdc++" />
|
||||
</Linker>
|
||||
</Target>
|
||||
<Target title="Release">
|
||||
<Option output="bin/Release/Lab1" prefix_auto="1" extension_auto="1" />
|
||||
@@ -22,6 +26,7 @@
|
||||
<Option compiler="gcc" />
|
||||
<Compiler>
|
||||
<Add option="-O2" />
|
||||
<Add directory="include" />
|
||||
</Compiler>
|
||||
<Linker>
|
||||
<Add option="-s" />
|
||||
@@ -32,7 +37,26 @@
|
||||
<Add option="-Wall" />
|
||||
<Add option="-fexceptions" />
|
||||
</Compiler>
|
||||
<Unit filename="histogram.cpp">
|
||||
<Option target="Debug" />
|
||||
</Unit>
|
||||
<Unit filename="histogram.h">
|
||||
<Option target="Debug" />
|
||||
</Unit>
|
||||
<Unit filename="histogram_internal.h">
|
||||
<Option target="<{~None~}>" />
|
||||
</Unit>
|
||||
<Unit filename="main.cpp" />
|
||||
<Unit filename="prot.cpp" />
|
||||
<Unit filename="prot.h" />
|
||||
<Unit filename="svg.cpp" />
|
||||
<Unit filename="svg.h" />
|
||||
<Unit filename="text.cpp">
|
||||
<Option target="Debug" />
|
||||
</Unit>
|
||||
<Unit filename="text.h">
|
||||
<Option target="Debug" />
|
||||
</Unit>
|
||||
<Extensions>
|
||||
<lib_finder disable_auto="1" />
|
||||
</Extensions>
|
||||
|
||||
7106
doctest.h
Обычный файл
7106
doctest.h
Обычный файл
Разница между файлами не показана из-за своего большого размера
Загрузить разницу
45
histogram.cpp
Обычный файл
45
histogram.cpp
Обычный файл
@@ -0,0 +1,45 @@
|
||||
#include "histogram.h"
|
||||
|
||||
std::vector<size_t> make_histogram(std::vector<double> numbers, size_t bin_count)
|
||||
{
|
||||
double max;
|
||||
double min;
|
||||
std::vector<size_t> bins;
|
||||
bins.resize(bin_count);
|
||||
find_minmax(numbers, min, max);
|
||||
double bin_size = (max - min) / bin_count;
|
||||
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 = min + j * bin_size;
|
||||
auto hi = min + (j + 1) * bin_size;
|
||||
if ((lo <= numbers[i]) && (hi > numbers[i])) {
|
||||
bins[j]++;
|
||||
found = true;
|
||||
}
|
||||
}
|
||||
if (!found) {
|
||||
bins[bin_count - 1]++;
|
||||
}
|
||||
}
|
||||
return bins;
|
||||
}
|
||||
|
||||
|
||||
bool find_minmax(std::vector<double> numbers, double& min, double& max) {
|
||||
if(numbers.size()==0){
|
||||
return false;
|
||||
}
|
||||
min = numbers[0];
|
||||
max = numbers[0];
|
||||
|
||||
for (double number : numbers) {
|
||||
if (min > number) {
|
||||
min = number;
|
||||
}
|
||||
if (max < number) {
|
||||
max = number;
|
||||
}
|
||||
}
|
||||
return true;
|
||||
}
|
||||
10
histogram.h
Обычный файл
10
histogram.h
Обычный файл
@@ -0,0 +1,10 @@
|
||||
#ifndef HISTOGRAM_H_INCLUDED
|
||||
#define HISTOGRAM_H_INCLUDED
|
||||
|
||||
#include <vector>
|
||||
|
||||
|
||||
bool find_minmax(std::vector<double> numbers, double& min, double& max);
|
||||
std::vector<size_t> make_histogram(std::vector<double> numbers, size_t bin_count);
|
||||
|
||||
#endif // HISTOGRAM_H_INCLUDED
|
||||
124
main.cpp
124
main.cpp
@@ -1,120 +1,52 @@
|
||||
|
||||
#include <vector>
|
||||
#include <iostream>
|
||||
#include <cstdio>
|
||||
|
||||
using namespace std;
|
||||
#include "histogram.h"
|
||||
#include "text.h"
|
||||
#include "svg.h"
|
||||
#include "prot.h"
|
||||
|
||||
void mmV(vector<double> numbers, double& min, double& max);
|
||||
size_t maxBin(vector<size_t> bins);
|
||||
|
||||
const size_t SCREEN_WIDTH = 80;
|
||||
const size_t MAX_ASTERISK = SCREEN_WIDTH - 3 - 1;
|
||||
struct Input {
|
||||
vector<double> numbers;
|
||||
std::vector<double> numbers;
|
||||
size_t bin_count{};
|
||||
int width;
|
||||
};
|
||||
|
||||
Input input_data();
|
||||
|
||||
int main()
|
||||
{
|
||||
|
||||
vector<size_t> bins;
|
||||
size_t countOfNumbers;
|
||||
size_t maxCount;
|
||||
double max;
|
||||
double min;
|
||||
Input in = input_data();
|
||||
std::vector<size_t> bins = make_histogram(in.numbers, in.bin_count);
|
||||
show_histogram_svg(bins, in.width);
|
||||
|
||||
numbers.resize(countOfNumbers);
|
||||
|
||||
cerr << endl;
|
||||
cerr << "Input bin count:\n";
|
||||
cin >> binCount;
|
||||
bins.resize(binCount);
|
||||
mmV(numbers, min, max);
|
||||
double bin_size = (max - min) / binCount;
|
||||
|
||||
|
||||
for (size_t i = 0; i < countOfNumbers; i++) {
|
||||
bool found = false;
|
||||
for (size_t j = 0; (j < binCount - 1) && !found; j++) {
|
||||
auto lo = min + j * bin_size;
|
||||
auto hi = min + (j + 1) * bin_size;
|
||||
if ((lo <= numbers[i]) && (hi > numbers[i])) {
|
||||
bins[j]++;
|
||||
found = true;
|
||||
}
|
||||
}
|
||||
if (!found) {
|
||||
bins[binCount - 1]++;
|
||||
}
|
||||
}
|
||||
maxCount = maxBin(bins);
|
||||
size_t count_stars;
|
||||
for (int i = 0; i < binCount; i++) {
|
||||
|
||||
if (bins[i] < 100) {
|
||||
cout << " ";
|
||||
}
|
||||
if (bins[i] < 10) {
|
||||
cout << " ";
|
||||
}
|
||||
|
||||
cout << bins[i];
|
||||
cout << "|";
|
||||
|
||||
if (maxCount > MAX_ASTERISK) {
|
||||
count_stars = MAX_ASTERISK * (static_cast<double>(bins[i]) / maxCount);
|
||||
}
|
||||
else {
|
||||
count_stars = bins[i];
|
||||
}
|
||||
for (size_t i2 = 0; i2 < count_stars; i2++) {
|
||||
cout << "*";
|
||||
}
|
||||
cout << endl;
|
||||
|
||||
|
||||
}
|
||||
|
||||
return 0;
|
||||
}
|
||||
|
||||
size_t maxBin(vector<size_t> bins) { //ëó÷øå áåç ôóíêöèè
|
||||
size_t max = bins[0];
|
||||
for (int i = 1; i < bins.size(); i++) {
|
||||
if (max < bins[i]) {
|
||||
max = bins[i];
|
||||
}
|
||||
}
|
||||
return max;
|
||||
}
|
||||
void mmV(vector<double> numbers, double& min, double& max) { //ñêàçàë ñäåëàòü áåç ôóíêöèè
|
||||
min = numbers[0];
|
||||
max = numbers[0];
|
||||
for (double number : numbers) {
|
||||
if (min > number) {
|
||||
min = number;
|
||||
}
|
||||
if (max < number) {
|
||||
max = number;
|
||||
}
|
||||
}
|
||||
return;
|
||||
}
|
||||
|
||||
|
||||
Input input_data() {
|
||||
cerr << "Input your count of numbers:\n";
|
||||
|
||||
Input input_struct;
|
||||
size_t countOfNumbers;
|
||||
cerr << "Input your count of numbers:\n";
|
||||
cin >> countOfNumbers;
|
||||
|
||||
input_struct.numbers.resize(countOfNumbers);
|
||||
cerr << "Input numbers:\n";
|
||||
for (int i = 0; i < countOfNumbers; i++) {
|
||||
cerr << i << endl;
|
||||
cin >> numbers[i];
|
||||
cerr << i << ":" << endl;
|
||||
cin >> input_struct.numbers[i];
|
||||
}
|
||||
cerr << endl;
|
||||
cerr << "Input bin count:\n";
|
||||
cin >> input_struct.bin_count;
|
||||
|
||||
input_struct.width = input_width(countOfNumbers);
|
||||
|
||||
|
||||
return input_struct;
|
||||
}
|
||||
|
||||
}
|
||||
//var15 áåç ìàñøòàáèðîâàíèÿ çàùèòà
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
25
prot.cpp
Обычный файл
25
prot.cpp
Обычный файл
@@ -0,0 +1,25 @@
|
||||
#include "prot.h"
|
||||
int input_width(int countOfNumbers){
|
||||
const int BLOCK_WIDTH = 10;
|
||||
bool check = false;
|
||||
int width =0;
|
||||
while(check == false){
|
||||
std::cout << "Input width:"<<std::endl;
|
||||
std::cin >> width;
|
||||
if(width < 70){
|
||||
std::cout << "width less 70" << std::endl;
|
||||
}
|
||||
else if(width > 800){
|
||||
std::cout << "width more 800" << std::endl;
|
||||
}
|
||||
else if(width < countOfNumbers*BLOCK_WIDTH / 3.0){
|
||||
std::cout << "ìåíåå òðåòè êîëè÷åñòâà ÷èñåë, óìíîæåííûõ íà øèðèíó áëîêà " << std::endl;
|
||||
}
|
||||
else{
|
||||
check = true;
|
||||
}
|
||||
|
||||
}
|
||||
return width;
|
||||
|
||||
}
|
||||
8
prot.h
Обычный файл
8
prot.h
Обычный файл
@@ -0,0 +1,8 @@
|
||||
#ifndef PROT_H_INCLUDED
|
||||
#define PROT_H_INCLUDED
|
||||
#include <vector>
|
||||
#include <iostream>
|
||||
|
||||
int input_width(int countOfNumbers);
|
||||
|
||||
#endif // PROT_H_INCLUDED
|
||||
42
svg.cpp
Обычный файл
42
svg.cpp
Обычный файл
@@ -0,0 +1,42 @@
|
||||
#include "svg.h"
|
||||
#include "text.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_end() {
|
||||
std::cout << "</svg>\n";
|
||||
}
|
||||
|
||||
void show_histogram_svg(const std::vector<size_t>& bins, int TEXT_WIDTH) {
|
||||
const auto IMAGE_WIDTH = 400;
|
||||
const auto IMAGE_HEIGHT = 300;
|
||||
const auto TEXT_LEFT = 20;
|
||||
const auto TEXT_BASELINE = 20;
|
||||
const auto BIN_HEIGHT = 30;
|
||||
|
||||
svg_begin(400, 300);
|
||||
|
||||
size_t maxCount = maxBin(bins);
|
||||
double top = 0;
|
||||
for (size_t bin : bins) {
|
||||
const double bin_width = (IMAGE_WIDTH - TEXT_WIDTH) * (bin / double(maxCount));
|
||||
svg_text(TEXT_LEFT, top + TEXT_BASELINE, std::to_string(bin));
|
||||
svg_rect(TEXT_WIDTH, top, bin_width, BIN_HEIGHT,"red","#006B3C");
|
||||
top += BIN_HEIGHT;
|
||||
}
|
||||
svg_end();
|
||||
}
|
||||
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 fills="black"){
|
||||
std::cout << "<rect x='"<<x<<"' y='"<<y<<"' width='"<<width<<"' height='"<<height<<"' stroke='"<<stroke<<"' fill='"<<fills<<"' />";
|
||||
}
|
||||
|
||||
|
||||
12
svg.h
Обычный файл
12
svg.h
Обычный файл
@@ -0,0 +1,12 @@
|
||||
#ifndef SVG_H_INCLUDED
|
||||
#define SVG_H_INCLUDED
|
||||
|
||||
#include <vector>
|
||||
#include <iostream>
|
||||
void svg_begin(double width, double height);
|
||||
void svg_end();
|
||||
void show_histogram_svg(const std::vector<size_t>& bins, int width);
|
||||
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 fills);
|
||||
|
||||
#endif // SVG_H_INCLUDED
|
||||
41
text.cpp
Обычный файл
41
text.cpp
Обычный файл
@@ -0,0 +1,41 @@
|
||||
#include "text.h"
|
||||
|
||||
void show_histogram_text(std::vector<size_t> bins , size_t bin_count) {
|
||||
size_t maxCount = maxBin(bins);
|
||||
size_t count_stars;
|
||||
for (size_t i = 0; i < bin_count; i++) {
|
||||
|
||||
if (bins[i] < 100) {
|
||||
std::cout << " ";
|
||||
}
|
||||
if (bins[i] < 10) {
|
||||
std::cout << " ";
|
||||
}
|
||||
|
||||
std::cout << bins[i];
|
||||
std::cout << "|";
|
||||
|
||||
if (maxCount > MAX_ASTERISK) {
|
||||
count_stars = MAX_ASTERISK * (static_cast<double>(bins[i]) / maxCount);
|
||||
}
|
||||
else {
|
||||
count_stars = bins[i];
|
||||
}
|
||||
for (size_t i2 = 0; i2 < count_stars; i2++) {
|
||||
std::cout << "*";
|
||||
}
|
||||
std::cout << std::endl;
|
||||
|
||||
|
||||
}
|
||||
}
|
||||
|
||||
const size_t maxBin(std::vector<size_t> bins) {
|
||||
size_t max = bins[0];
|
||||
for (int i = 1; i < bins.size(); i++) {
|
||||
if (max < bins[i]) {
|
||||
max = bins[i];
|
||||
}
|
||||
}
|
||||
return max;
|
||||
}
|
||||
12
text.h
Обычный файл
12
text.h
Обычный файл
@@ -0,0 +1,12 @@
|
||||
#ifndef TEXT_H_INCLUDED
|
||||
#define TEXT_H_INCLUDED
|
||||
|
||||
|
||||
#include <iostream>
|
||||
#include <vector>
|
||||
|
||||
const size_t SCREEN_WIDTH = 80;
|
||||
const size_t MAX_ASTERISK = SCREEN_WIDTH - 3 - 1;
|
||||
void show_histogram_text(std::vector<size_t> bins , size_t bin_count);
|
||||
const size_t maxBin(std::vector<size_t> bins);
|
||||
#endif // TEXT_H_INCLUDED
|
||||
44
unittest.cpp
Обычный файл
44
unittest.cpp
Обычный файл
@@ -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") {
|
||||
std::vector<double> numbers{};
|
||||
double min=0, max=0;
|
||||
CHECK(!find_minmax(numbers, min, max));
|
||||
}
|
||||
TEST_CASE("1 val") {
|
||||
std::vector<double> numbers{1};
|
||||
double min=0, max=0;
|
||||
find_minmax(numbers, min, max);
|
||||
CHECK(min == 1);
|
||||
CHECK(max == 1);
|
||||
}
|
||||
TEST_CASE("- val") {
|
||||
std::vector<double> numbers{-1,-2};
|
||||
double min=0, max=0;
|
||||
find_minmax(numbers, min, max);
|
||||
CHECK(min == -2);
|
||||
CHECK(max == -1);
|
||||
}
|
||||
TEST_CASE("same val") {
|
||||
std::vector<double> numbers{2,2};
|
||||
double min=0, max=0;
|
||||
find_minmax(numbers, min, max);
|
||||
CHECK(min == 2);
|
||||
CHECK(max == 2);
|
||||
}
|
||||
TEST_CASE("check prot1") {
|
||||
check(input_width(int countOfNumbers, 10));
|
||||
|
||||
}
|
||||
|
||||
Ссылка в новой задаче
Block a user