diff --git a/Lab03.cbp b/Lab03.cbp
index d2f1b8f..cf95eba 100644
--- a/Lab03.cbp
+++ b/Lab03.cbp
@@ -13,6 +13,7 @@
+
@@ -37,7 +38,6 @@
-
diff --git a/histogram.cpp b/histogram.cpp
index 4b45863..999fae5 100644
--- a/histogram.cpp
+++ b/histogram.cpp
@@ -1,46 +1,43 @@
-#include
#include
#include "histogram.h"
- void find_minmax(const std::vector& 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;
+#include "histogram_internal.h"
+using namespace std;
+
+
+void find_minmax(const vector& numbers, double& min, double& max) {
+ max = 0;
+ if (numbers.size() != 0) {
+ min = numbers[0];
+ max = numbers[0];
+ for (auto el : numbers) {
+ if (el > max) {
+ max = el;
+ }
+ if (el < min) {
+ min = el;
+ }
}
- }
+ } else {min = 0;}
}
-
-std::vector make_histogram(const std::vector&numbers, size_t &bin_count, size_t &number_count, size_t &max_count) {
- double min, max;
+vector make_histogram(const vector& numbers, size_t bin_count) {
+ vector bins(bin_count);
+ double max, min;
find_minmax(numbers, min, max);
- double bin_size = (max - min) / bin_count;
- std::vector bins(bin_count);
- max_count = bins[0];
- for (size_t i = 0; i < number_count; i++){
+ double binSize = (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)){
+ for (size_t j = 0; (j < bin_count - 1) && !found; j++) {
+ auto lo = min + j * binSize;
+ auto hi = min + (j + 1) * binSize;
+ if ((lo <= numbers[i]) && (numbers[i] < hi)) {
bins[j]++;
found = true;
}
- if (bins[j] > max_count){
- max_count = bins[j];
- }
}
- if (!found){
+ if (!found) {
bins[bin_count - 1]++;
}
- if (bins[bin_count - 1] > max_count){
- max_count = bins[bin_count - 1];
- }
}
return bins;
}
diff --git a/histogram.h b/histogram.h
index 7b68fba..07979b8 100644
--- a/histogram.h
+++ b/histogram.h
@@ -1,5 +1,4 @@
#pragma once
#include
-std::vector
-make_histogram(const std::vector&numbers, size_t & bin_count, size_t & number_count, size_t & max_count);
+std::vector make_histogram(const std::vector& numbers, size_t bin_count);
diff --git a/main.cpp b/main.cpp
index 8bce6f8..dd6c026 100644
--- a/main.cpp
+++ b/main.cpp
@@ -3,93 +3,70 @@
#include "histogram.h"
#include "text.h"
#include "svg.h"
-#include
#include
-#include
-using namespace std;
+#include
-struct Input
-{
+using namespace std;
+
+struct Input {
vector numbers;
size_t bin_count{};
- size_t number_count{};
- size_t max_count{};
};
-Input
-input_data(istream& stream, bool prompt)
-{
+Input input_data(istream& stream) {
+ Input in;
size_t number_count;
- if (prompt==true)
- {
- cerr << "Enter number count: ";
- }
stream >> number_count;
- Input in;
- if (prompt==true)
- {
- cerr << "Enter bin count: ";
- }
- stream >> in.bin_count;
-
- vector numbers(in.number_count);
- in.numbers.resize(in.number_count);
- for (size_t i = 0; i < in.number_count; i++)
- {
+ in.numbers.resize(number_count);
+ for (size_t i = 0; i < number_count; i++) {
stream >> in.numbers[i];
}
- size_t bin_count;
- size_t max_count;
- in.max_count = 0;
+ stream >> in.bin_count;
return in;
}
-size_t write_data(void* items, size_t item_size, size_t item_count, void* ctx)
-{
+
+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(ctx);
- auto text = reinterpret_cast(items);
- buffer->write(text, data_size);
+ buffer->write(reinterpret_cast(items), data_size);
return data_size;
}
-Input download(const string& address)
-{
+
+Input download(const string& address) {
stringstream buffer;
- curl_global_init(CURL_GLOBAL_ALL);
CURL* curl = curl_easy_init();
- if(curl)
- {
+ 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);
- curl_easy_cleanup(curl);
- if (res != 0)
- {
- cout << res;
+ if (res != CURLE_OK) {
+ cout << curl_easy_strerror(res);
exit(1);
}
- curl_version_info_data *curl_info = curl_version_info(CURLVERSION_NOW);
- for (size_t i = 0; curl_info->protocols != NULL; i++){
- cerr << curl_info->protocols[i] << "\n";
+ auto data = curl_version_info(CURLVERSION_NOW)->protocols;
+ for (auto protocol = data; *protocol; ++protocol) {
+ cerr << *protocol << endl;
+ }
+ curl_easy_cleanup(curl);
}
- curl_global_init(CURL_GLOBAL_ALL);
- return input_data(buffer, false);
+ return input_data(buffer);
}
-}
-
-int main(int argc, char* argv[]){
+int main(int argc, char* argv[]) {
Input in;
if (argc > 1) {
in = download(argv[1]);
} else {
- in = input_data(cin, true);
+ in = input_data(cin);
}
-auto bins = make_histogram(in.numbers, in.bin_count, in.number_count, in.max_count);
-vector test;
-show_histogram_svg(bins, in.max_count, in.bin_count, test);
-return 0;
+ auto bins = make_histogram(in.numbers, in.bin_count);
+ show_histogram_svg(bins);
+ cerr << "cURL version " << curl_version_info(CURLVERSION_NOW)->version << endl;
+ auto ssl_ver = curl_version_info(CURLVERSION_NOW)->ssl_version;
+ cerr << "SSL version " << ssl_ver << "\n";
+ return 0;
}
diff --git a/svg.cpp b/svg.cpp
index 605d96b..d6e2af5 100644
--- a/svg.cpp
+++ b/svg.cpp
@@ -1,77 +1,61 @@
-#include "iostream"
+#include
#include "svg.h"
+using namespace std;
+
const auto IMAGE_WIDTH = 400;
-const auto IMAGE_HEIGHT = 300;
+const auto IMAGE_HEIGHT = 310;
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;
-void svg_begin(double width, double height, std::vector & test)
-{
- test.push_back(width);
- test.push_back(height);
- std::cout << "\n";
- std::cout << "\n";
+ svg_end(top, cout);
}
diff --git a/svg.h b/svg.h
index 73d5e22..3a65e84 100644
--- a/svg.h
+++ b/svg.h
@@ -1,10 +1,3 @@
-#pragma once
-#include
#include
-void show_histogram_svg(const std::vector& bins, size_t & max_count, size_t & bin_count, std::vector & test);
-void svg_begin(double width, double height, std::vector & test);
-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 fill);
-void svg_line(double x, double y, double width, double height, std::vector & test);
-void svg_end();
+void show_histogram_svg(const std::vector& bins);
diff --git a/text.cpp b/text.cpp
index eb30bc8..89cfd24 100644
--- a/text.cpp
+++ b/text.cpp
@@ -1,34 +1,30 @@
#include
#include
-#include "text.h"
-void show_histogram_text(std::vector& bins, size_t & max_count,size_t & bin_count){
+using namespace std;
+
+
const size_t SCREEN_WIDTH = 80;
const size_t MAX_ASTERISK = SCREEN_WIDTH - 3 - 1;
-if (max_count > MAX_ASTERISK){
- std::vector hights(bin_count);
- for (size_t i = 0; i < bin_count; i++){
- size_t height = MAX_ASTERISK * (static_cast(bins[i]) / max_count);
- hights[i] = height;
- }
- for (size_t i = 0; i < bin_count; i++){
- printf("%3d|", bins[i]);
- for (size_t j = 0; j < hights[i]; j++){
- std::cout<<"*";
- }
- std::cout<& bins) {
+ size_t maxCount = bins[0];
+ for (auto bin : bins) {
+ if (maxCount < bin) {
+ maxCount = bin;
}
}
- else{
- for (size_t i = 0; i < bin_count; i++)
+ for (auto bin : bins) {
+ if (bin < 100)
{
- printf("%3d|", bins[i]);
- for (size_t j = 0; j < bins[i]; j++){
- std::cout<<"*";
- }
- std::cout << std::endl;
+ cout << " ";
}
+ if (bin < 10)
+ {
+ cout << " ";
+ }
+ size_t height = maxCount < MAX_ASTERISK ? bin : MAX_ASTERISK * (static_cast(bin) / maxCount);
+ cout << bin << "|" << string(height, '*');
+ cout << "\n";
}
-
}
diff --git a/text.h b/text.h
index 8b13789..0407a22 100644
--- a/text.h
+++ b/text.h
@@ -1 +1,4 @@
+#pragma once
+#include
+void show_histogram_text(const std::vector& bins);