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

..

10 Коммитов

14 изменённых файлов: 447 добавлений и 42 удалений

3
.gitignore поставляемый
Просмотреть файл

@@ -1,2 +1,3 @@
/bin
/obj
/obj
/curl

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

@@ -6,21 +6,28 @@
#include <conio.h>
using namespace std;
void find_minmax(vector<double> numbers, double& min, double& max) {
min = numbers[0];
for (auto i = 0; i < numbers.size(); i++) {
if (numbers[i] < min) {
min = numbers[i];
/*bool find_minmax(vector<double> numbers, double& min, double& max) {
if (numbers.empty())
{
return true;
}
else
{
min = numbers[0];
for (auto i = 0; i < numbers.size(); i++) {
if (numbers[i] < min) {
min = numbers[i];
}
}
max = numbers[0];
for (auto i = 0; i < numbers.size(); i++) {
if (numbers[i] > max) {
max = numbers[i];
}
}
}
max = numbers[0];
for (auto i = 0; i < numbers.size(); i++) {
if (numbers[i] > max) {
max = numbers[i];
}
}
return false;
}
vector<size_t> make_histogram(const vector<double>& numbers, size_t bin_count) {
@@ -64,3 +71,53 @@ vector<size_t> make_histogram(const vector<double>& numbers, size_t bin_count) {
}
return bins;
}
*/
void find_minmax(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;
}
}
return;
}
vector<size_t> make_histogram (vector<double> numbers, size_t bin_count)
{
double min;
double max;
find_minmax (numbers, min, max);
double bin_size = (max - min) / bin_count;
vector<size_t> bins(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]) && (numbers[i] < hi))
{
bins[j]++;
found = true;
}
}
if (!found)
{
bins[bin_count - 1]++;
}
}
return bins;
}

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

@@ -2,8 +2,10 @@
#define HISTOGAM_H_INCLUDED
#include <vector>
std::vector<size_t>
/*std::vector<size_t>
make_histogram(const std::vector<double>& numbers, size_t bin_count);
*/
std::vector<size_t>
make_histogram(const std::vector<double> numbers, size_t bin_count);
#endif // HISTOGAM_H_INCLUDED

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

@@ -2,6 +2,9 @@
#define HISTOGAM_INTERNAL_H_INCLUDED
#include <vector>
void find_minmax(std::vector<double> numbers, double& min, double& max);
/*bool find_minmax(std::vector<double> numbers, double& min, double& max);
*/
#endif // HISTOGAM_INTERNAL_H_INCLUDED

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

@@ -3,20 +3,24 @@
<iostream>
<vector>
1682281166 source:c:\users\admin\onedrive\Ðàáî÷èé ñòîë\lab1\main.cpp
1685968543 source:c:\users\admin\onedrive\Ðàáî÷èé ñòîë\lab1\main.cpp
<iostream>
<vector>
<cmath>
<string>
"histogam.h"
"text.h"
"svg.h"
<conio.h>
"histogam_internal.h"
<curl/curl.h>
<sstream>
<string>
"svg.h"
1682273839 c:\users\admin\onedrive\Ðàáî÷èé ñòîë\lab1\histogam.h
1685967301 c:\users\admin\onedrive\Ðàáî÷èé ñòîë\lab1\histogam.h
<vector>
1682280773 source:c:\users\admin\onedrive\Ðàáî÷èé ñòîë\lab1\histogam.cpp
1685967301 source:c:\users\admin\onedrive\Ðàáî÷èé ñòîë\lab1\histogam.cpp
<iostream>
<vector>
<cmath>
@@ -24,20 +28,20 @@
"histogam_internal.h"
<conio.h>
1682280773 source:c:\users\admin\onedrive\Ðàáî÷èé ñòîë\lab1\text.cpp
1685967301 source:c:\users\admin\onedrive\Ðàáî÷èé ñòîë\lab1\text.cpp
<iostream>
<vector>
<cmath>
"text.h"
<conio.h>
1682273447 c:\users\admin\onedrive\Ðàáî÷èé ñòîë\lab1\text.h
1685896777 c:\users\admin\onedrive\Ðàáî÷èé ñòîë\lab1\text.h
<vector>
1682273796 c:\users\admin\onedrive\Ðàáî÷èé ñòîë\lab1\histogam_internal.h
1685967301 c:\users\admin\onedrive\Ðàáî÷èé ñòîë\lab1\histogam_internal.h
<vector>
1682280585 source:c:\users\admin\onedrive\Ðàáî÷èé ñòîë\lab1\svg.cpp
1685967301 source:c:\users\admin\onedrive\Ðàáî÷èé ñòîë\lab1\svg.cpp
<math.h>
<iostream>
<conio.h>
@@ -45,5 +49,53 @@
<string>
"svg.h"
1682280301 c:\users\admin\onedrive\Ðàáî÷èé ñòîë\lab1\svg.h
1685967301 c:\users\admin\onedrive\Ðàáî÷èé ñòîë\lab1\svg.h
1685361112 c:\users\admin\onedrive\Ðàáî÷èé ñòîë\lab1\curl\include\curl\curl.h
"curlver.h"
"system.h"
<stdio.h>
<limits.h>
<osreldate.h>
<sys/types.h>
<time.h>
<winsock2.h>
<ws2tcpip.h>
<sys/select.h>
<sys/socket.h>
<sys/time.h>
"easy.h"
"multi.h"
"urlapi.h"
"options.h"
"header.h"
"websockets.h"
"typecheck-gcc.h"
1685416610 c:\users\admin\onedrive\Ðàáî÷èé ñòîë\lab1\curl\include\curl\curlver.h
1684137360 c:\users\admin\onedrive\Ðàáî÷èé ñòîë\lab1\curl\include\curl\system.h
<ConditionalMacros.h>
<winsock2.h>
<windows.h>
<ws2tcpip.h>
<sys/types.h>
<sys/socket.h>
<sys/poll.h>
1685004688 c:\users\admin\onedrive\Ðàáî÷èé ñòîë\lab1\curl\include\curl\easy.h
1684137360 c:\users\admin\onedrive\Ðàáî÷èé ñòîë\lab1\curl\include\curl\multi.h
"curl.h"
1684137360 c:\users\admin\onedrive\Ðàáî÷èé ñòîë\lab1\curl\include\curl\urlapi.h
"curl.h"
1684137360 c:\users\admin\onedrive\Ðàáî÷èé ñòîë\lab1\curl\include\curl\options.h
1684137360 c:\users\admin\onedrive\Ðàáî÷èé ñòîë\lab1\curl\include\curl\header.h
1684137360 c:\users\admin\onedrive\Ðàáî÷èé ñòîë\lab1\curl\include\curl\websockets.h
1684137360 c:\users\admin\onedrive\Ðàáî÷èé ñòîë\lab1\curl\include\curl\typecheck-gcc.h

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

@@ -4,7 +4,7 @@
<ActiveTarget name="Debug" />
<File name="main.cpp" open="1" top="1" tabpos="1" split="0" active="1" splitpos="0" zoom_1="0" zoom_2="0">
<Cursor>
<Cursor1 position="296" topLine="0" />
<Cursor1 position="712" topLine="0" />
</Cursor>
</File>
</CodeBlocks_layout_file>

100
main.cpp
Просмотреть файл

@@ -4,13 +4,17 @@
#include <string>
#include "histogam.h"
#include "text.h"
#include "svg.h"
#include <conio.h>
#include "histogam_internal.h"
#include <curl/curl.h>
#include <sstream>
#include <string>
#include "svg.h"
using namespace std;
struct Input {
/*struct Input {
vector<double> numbers;
size_t bin_count{};
};
@@ -26,7 +30,7 @@ input_data() {
cin >> in.numbers[i];
}
cout << "Enter number of bins";
cerr<< "Enter number of bins";
cin >> in.bin_count;
return in;
}
@@ -35,6 +39,94 @@ int main()
{
Input in = input_data();
auto bins = make_histogram(in.numbers, in.bin_count);
show_histogram_svg(bins);
double min, max;
find_minmax(in.numbers,min,max);
show_histogram_svg(bins,min,max);
return 0;
}
*/
struct Input
{
vector<double> numbers;
size_t bin_count{};
};
Input
input_data(istream& in, bool promt)
{
size_t number_count;
if (promt)
{
cerr << "Enter number count: ";
}
in >> number_count;
Input ik;
ik.numbers.resize(number_count);
for (size_t i = 0; i < number_count; i++)
{
in >> ik.numbers[i];
}
if (promt)
{
cerr << "Enter bin count: ";
}
in>> ik.bin_count;
return ik;
}
size_t write_data(void* items, size_t item_size, size_t item_count, void* ctx)
{
size_t data_size = item_size * item_count;
stringstream* buffer = reinterpret_cast<stringstream*>(ctx);
buffer->write(reinterpret_cast<const char*>(items), data_size);
return data_size;
}
Input
download(const string& address)
{
stringstream buffer;
CURL *curl = curl_easy_init();
if(curl)
{
CURLcode res;
curl_easy_setopt(curl, CURLOPT_URL, address.c_str());
curl_easy_setopt(curl, CURLOPT_WRITEFUNCTION, write_data);
curl_easy_setopt(curl, CURLOPT_WRITEDATA, &buffer);
res = curl_easy_perform(curl);
if (res != CURLE_OK)
{
fprintf(stderr, "curl_easy_perform() failed: %s\n",curl_easy_strerror(res));
exit(1);
}
if(res == CURLE_OK)
{
long req;
res = curl_easy_getinfo(curl, CURLINFO_REQUEST_SIZE, &req);
if(!res)
cerr<<"Request size: %ld bytes: "<< req;
}
curl_easy_cleanup(curl);
}
return input_data(buffer, false);
}
int main(int argc, char* argv[])
{
Input input;
if (argc > 1)
{
input = download(argv[1]);
}
else
{
input = input_data(cin, true);
}
const auto bins = make_histogram(input.numbers, input.bin_count);
show_histogram_svg(bins);
}

113
svg.cpp
Просмотреть файл

@@ -6,7 +6,17 @@
#include "svg.h"
using namespace std;
/*size_t k=0;
float
step(size_t& k, size_t bin_count, double min,double max){
float interval;
double bin_size= (max-min)/bin_count;
if (k < bin_count - 1){
interval = min + (k + 1) * bin_size;
k+=1;
}
return interval;
}
void
svg_begin(double width, double height) {
cout << "<?xml version='1.0' encoding='UTF-8'?>\n";
@@ -26,12 +36,107 @@ 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 colour = "black", string fill = "black"){
cout << "<rect x='"<<x<<"' y='"<<y<<"' width='"<<width<<"' height='"<<height<<"' stroke = '"<<colour <<"' fill = '"<<fill<<"' />";
}
void
show_histogram_svg(const vector<size_t>& bins, double min,double max) {
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 auto INTER_WIDTH=30;
double top = 0;
double max_count = bins[0];
for (size_t i = 0; i < bins.size(); i++) {
if (bins[i] > max_count)
max_count = bins[i];
}
svg_begin(400, 300);
for (size_t bin : bins) {
const double bin_width = (IMAGE_WIDTH - TEXT_WIDTH)*(bin/max_count);
svg_text(TEXT_LEFT+INTER_WIDTH, top + TEXT_BASELINE, to_string(bin));
svg_rect(TEXT_WIDTH+INTER_WIDTH, top, bin_width, BIN_HEIGHT, "blue", "#FF00FF");
top += BIN_HEIGHT;
if (bin!=bins[bins.size()-1]){
float inter=step(k,bins.size(),min,max);
svg_text(TEXT_LEFT, top + TEXT_BASELINE, to_string(inter));
top += BIN_HEIGHT;
}
}
svg_end();
}
*/
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) {
svg_begin(400, 300);
svg_text(20, 20, to_string(bins[0]));
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;
const auto BLOCK_WIDTH = 10;
const auto BLACK = "black";
const auto RED = "red";
const auto MAX_WIDTH = IMAGE_WIDTH-TEXT_WIDTH;
svg_begin(IMAGE_WIDTH,IMAGE_HEIGHT);
double top = 0;
double max_count = bins[0];
for (size_t i = 0; i < bins.size(); i++)
{
if (max_count<bins[i])
{
max_count=bins[i];
}
}
for (size_t bin : bins)
{
double bin_width = (MAX_WIDTH)*(bin/max_count);
svg_text(TEXT_LEFT, top + TEXT_BASELINE, to_string(bin));
svg_rect(TEXT_WIDTH, top, bin_width, BIN_HEIGHT, BLACK, RED);
top += BIN_HEIGHT;
}
svg_end();
}

3
svg.h
Просмотреть файл

@@ -1,6 +1,9 @@
#ifndef SVG_H_INCLUDED
#define SVG_H_INCLUDED
/*void
show_histogram_svg(const std::vector<size_t>& bins,double min, double max);
*/
void
show_histogram_svg(const std::vector<size_t>& bins);

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

@@ -4,7 +4,7 @@
#include "text.h"
#include <conio.h>
using namespace std;
void show_histogram_text(vector <size_t> bins, size_t bin_count ) {
/*void show_histogram_text(vector <size_t> bins, size_t bin_count ) {
for (size_t i = 0; i < bin_count; i++) {
if (bins[i] < 100) {
@@ -20,3 +20,53 @@ void show_histogram_text(vector <size_t> bins, size_t bin_count ) {
cout << "\n";
}
}
*/
void show_histogram_text(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 i,j;
double max_count;
max_count = bins[0];
for (i=0; i< bin_count; i++)
{
if (max_count<bins[i])
{
max_count=bins[i];
}
}
size_t height;
bool flag = false;
if(max_count>max_asterisk)
{
flag=true;
}
for (j = 0; j < bin_count; j++)
{
if (bins[j] < 100)
{
cout << " ";
}
if (bins[j] < 10)
{
cout << " ";
}
cout << bins[j] << "|";
if (flag)
{
height = max_asterisk * (static_cast<double>(bins[j]) / max_count);
}
else
{
height=bins[j];
}
for (i = 0; i < height; i++)
{
cout << "*";
}
cout << endl;
}
}

6
text.h
Просмотреть файл

@@ -2,7 +2,9 @@
#define TEXT_H_INCLUDED
#include <vector>
void show_histogram_text(std::vector <size_t> bins, size_t bin_count );
/*void show_histogram_text(std::vector <size_t> bins, size_t bin_count );
*/
void
show_histogram_text(std::vector<size_t> bins, size_t bin_count);
#endif // TEXT_H_INCLUDED

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

@@ -8,10 +8,42 @@ TEST_CASE("distinct positive numbers") {
double min = 0;
double max = 0;
std::vector<double>f{1,2};
CHECK(f.size() != 0);
find_minmax(f, min, max);
CHECK(min == 1);
CHECK(max == 2);
CHECK(min != max);
}
TEST_CASE("distinct positive numbers") {
double min = 0;
double max = 0;
std::vector<double>f{};
CHECK(find_minmax(f, min, max) != 0);
CHECK(f.size() != 1);
find_minmax(f, min, max);
CHECK(min == 1);
CHECK(max == 2);
CHECK(min != max);
}
TEST_CASE("void vector") {
double min = 3;
double max = 2;
std::vector<double> numbers {};
bool check = find_minmax(numbers, min, max);
CHECK(check == true);
}
TEST_CASE("vector with same elements") {
double min = 0;
double max = 0;
find_minmax({2,2,2}, min, max);
CHECK(min == 2);
CHECK(max == 2);
}
TEST_CASE("vector with one element") {
double min = 0;
double max = 0;
find_minmax({1}, min, max);
CHECK(min == 1);
CHECK(max == 1);
}

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

@@ -1,18 +1,19 @@
# depslib dependency file v1.0
1682273864 source:c:\users\admin\onedrive\Ðàáî÷èé ñòîë\lab1\histogam.cpp
1685962483 source:c:\users\admin\onedrive\Ðàáî÷èé ñòîë\lab1\histogam.cpp
<iostream>
<vector>
<cmath>
"histogam.h"
"histogam_internal.h"
<conio.h>
1682273839 c:\users\admin\onedrive\Ðàáî÷èé ñòîë\lab1\histogam.h
1685896776 c:\users\admin\onedrive\Ðàáî÷èé ñòîë\lab1\histogam.h
<vector>
1682273796 c:\users\admin\onedrive\Ðàáî÷èé ñòîë\lab1\histogam_internal.h
1685896777 c:\users\admin\onedrive\Ðàáî÷èé ñòîë\lab1\histogam_internal.h
<vector>
1682276727 source:c:\users\admin\onedrive\Ðàáî÷èé ñòîë\lab1\unittest.cpp
1682276830 source:c:\users\admin\onedrive\Ðàáî÷èé ñòîë\lab1\unittest.cpp
"doctest.h"
"histogam_internal.h"
<vector>

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

@@ -0,0 +1,5 @@
<?xml version="1.0" encoding="UTF-8" standalone="yes" ?>
<CodeBlocks_layout_file>
<FileVersion major="1" minor="0" />
<ActiveTarget name="Debug" />
</CodeBlocks_layout_file>