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

...

16 Коммитов

16 изменённых файлов: 7879 добавлений и 77 удалений

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

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

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

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

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

@@ -0,0 +1,123 @@
#include <iostream>
#include <vector>
#include <cmath>
#include "histogam.h"
#include "histogam_internal.h"
#include <conio.h>
using namespace std;
/*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];
}
}
}
return false;
}
vector<size_t> make_histogram(const vector<double>& numbers, size_t bin_count) {
vector<size_t> bins(bin_count);
vector<size_t> binss(bin_count);
double max, min;
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]) && (numbers[i] < hi)) {
bins[j]++;
found = true;
}
}
if (!found) {
bins[bin_count - 1]++;
}
}
int max_count = bins[0];
for (size_t i = 0; i < bin_count; i++) {
if (bins[i] > max_count) {
max_count = bins[i];
}
}
if (max_count > 76) {
for (size_t i = 0; i < bin_count; i++) {
int count = bins[i];
size_t height = 76 * (static_cast<double>(count) / max_count);
bins[i] = height;
}
}
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;
}

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

@@ -0,0 +1,11 @@
#ifndef HISTOGAM_H_INCLUDED
#define HISTOGAM_H_INCLUDED
#include <vector>
/*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

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

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

101
lab1.depend Обычный файл
Просмотреть файл

@@ -0,0 +1,101 @@
# depslib dependency file v1.0
1676294754 source:l:\i êóðñ\À-2-22\Ëåäîâñêîé\ëàá1\lab1\main.cpp
<iostream>
<vector>
1685968543 source:c:\users\admin\onedrive\Ðàáî÷èé ñòîë\lab1\main.cpp
<iostream>
<vector>
<cmath>
<string>
"histogam.h"
"text.h"
<conio.h>
"histogam_internal.h"
<curl/curl.h>
<sstream>
<string>
"svg.h"
1685967301 c:\users\admin\onedrive\Ðàáî÷èé ñòîë\lab1\histogam.h
<vector>
1685967301 source:c:\users\admin\onedrive\Ðàáî÷èé ñòîë\lab1\histogam.cpp
<iostream>
<vector>
<cmath>
"histogam.h"
"histogam_internal.h"
<conio.h>
1685967301 source:c:\users\admin\onedrive\Ðàáî÷èé ñòîë\lab1\text.cpp
<iostream>
<vector>
<cmath>
"text.h"
<conio.h>
1685896777 c:\users\admin\onedrive\Ðàáî÷èé ñòîë\lab1\text.h
<vector>
1685967301 c:\users\admin\onedrive\Ðàáî÷èé ñòîë\lab1\histogam_internal.h
<vector>
1685967301 source:c:\users\admin\onedrive\Ðàáî÷èé ñòîë\lab1\svg.cpp
<math.h>
<iostream>
<conio.h>
<vector>
<string>
"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

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

@@ -0,0 +1,10 @@
<?xml version="1.0" encoding="UTF-8" standalone="yes" ?>
<CodeBlocks_layout_file>
<FileVersion major="1" minor="0" />
<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="712" topLine="0" />
</Cursor>
</File>
</CodeBlocks_layout_file>

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

@@ -1,85 +1,132 @@
#include <iostream> #include <iostream>
#include <vector> #include <vector>
#include <cmath>
#include <string>
#include "histogam.h"
#include "text.h"
#include <conio.h>
#include "histogam_internal.h"
#include <curl/curl.h>
#include <sstream>
#include <string>
#include "svg.h"
using namespace std; using namespace std;
/*struct Input {
vector<double> numbers;
size_t bin_count{};
};
Input
input_data() {
size_t number_count;
cin >> number_count;
Input in;
in.numbers.resize(number_count);
for (size_t i = 0; i < number_count; i++) {
cin >> in.numbers[i];
}
cerr<< "Enter number of bins";
cin >> in.bin_count;
return in;
}
int main() int main()
{ {
double n; Input in = input_data();
size_t number_count, bin_count; auto bins = make_histogram(in.numbers, in.bin_count);
cerr<<"Enter number cout:"; double min, max;
cin>>number_count; find_minmax(in.numbers,min,max);
vector<double> numbers(number_count); show_histogram_svg(bins,min,max);
for (int i =0;i<number_count;i++){
cin>>numbers[i];
}
cerr<<"Enter bin_count";
cin>>bin_count;
vector<size_t> bins(bin_count);
double min = numbers[0];
double max = numbers[0];
for (double x : numbers){
if (x<min){
min=x;
}
else if (x>max){
max=x;
}
}
double bin_size= (max-min)/bin_count;
for (size_t i = 0; i < number_count; 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]++;
}
}
size_t maxbin=0;
for (size_t i=0; i<bin_count; i++){
if (maxbin< bins[i]){
maxbin=bins[i];
}
}
size_t k=0;
for (size_t i=0; i<bin_count; i++){
if (bins[i]<100){
cout<<" ";
}
if (bins[i]<10){
cout<<" ";
}
cout<<" "<<bins[i]<<"|";
size_t count = bins[i];
if (maxbin>=76){
size_t height= 76*(static_cast<double>(count)/maxbin);
for (size_t j=0; j<bins[i]; j++){
if (j<height)
cout<<"*";
else
break;
}
}
else{
for (size_t j=0; j<bins[i]; j++){
cout<<"*";
}
}
cout<<endl;
if (k < bin_count - 1){
auto step = min + (k + 1) * bin_size;
cout.precision(3);
cout<<step;
k+=1;
cout<<endl;
}
}
return 0; 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);
}

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

@@ -0,0 +1,142 @@
#include <math.h>
#include <iostream>
#include <conio.h>
#include <vector>
#include <string>
#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";
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 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)
{
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();
}

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

@@ -0,0 +1,10 @@
#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);
#endif // SVG_H_INCLUDED

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

@@ -0,0 +1,72 @@
#include <iostream>
#include <vector>
#include <cmath>
#include "text.h"
#include <conio.h>
using namespace std;
/*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) {
cout << " ";
}
if (bins[i] < 10) {
cout << " ";
}
cout << bins[i] << "|";
for (size_t j = 0; j < bins[i]; j++) {
cout << "*";
}
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;
}
}

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

@@ -0,0 +1,10 @@
#ifndef TEXT_H_INCLUDED
#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);
#endif // TEXT_H_INCLUDED

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

@@ -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>

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

@@ -0,0 +1,49 @@
#define DOCTEST_CONFIG_NO_MULTITHREADING
#define DOCTEST_CONFIG_IMPLEMENT_WITH_MAIN
#include "doctest.h"
#include "histogam_internal.h"
#include <vector>
TEST_CASE("distinct positive numbers") {
double min = 0;
double max = 0;
std::vector<double>f{1,2};
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);
}

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

@@ -0,0 +1,65 @@
# depslib dependency file v1.0
1685962483 source:c:\users\admin\onedrive\Ðàáî÷èé ñòîë\lab1\histogam.cpp
<iostream>
<vector>
<cmath>
"histogam.h"
"histogam_internal.h"
<conio.h>
1685896776 c:\users\admin\onedrive\Ðàáî÷èé ñòîë\lab1\histogam.h
<vector>
1685896777 c:\users\admin\onedrive\Ðàáî÷èé ñòîë\lab1\histogam_internal.h
<vector>
1682276830 source:c:\users\admin\onedrive\Ðàáî÷èé ñòîë\lab1\unittest.cpp
"doctest.h"
"histogam_internal.h"
<vector>
1682274769 c:\users\admin\onedrive\Ðàáî÷èé ñòîë\lab1\doctest.h
<signal.h>
<ciso646>
<cstddef>
<ostream>
<istream>
<type_traits>
"doctest_fwd.h"
<ctime>
<cmath>
<climits>
<math.h>
<new>
<cstdio>
<cstdlib>
<cstring>
<limits>
<utility>
<fstream>
<sstream>
<iostream>
<algorithm>
<iomanip>
<vector>
<atomic>
<mutex>
<set>
<map>
<unordered_set>
<exception>
<stdexcept>
<csignal>
<cfloat>
<cctype>
<cstdint>
<string>
<sys/types.h>
<unistd.h>
<sys/sysctl.h>
<AfxWin.h>
<windows.h>
<io.h>
<sys/time.h>
<unistd.h>

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>