Сравнить коммиты
2 Коммитов
40cc4f7fe1
...
fd47eaab64
| Автор | SHA1 | Дата | |
|---|---|---|---|
| fd47eaab64 | |||
| d11a01d781 |
@@ -2,19 +2,21 @@
|
||||
#include "histogram_internal.h"
|
||||
#include <vector>
|
||||
using std::vector;
|
||||
void find_minmax(const std::vector<double>& numbers, double& min, double& max) {
|
||||
min = numbers[0];
|
||||
void find_minmax(const vector<double>& numbers, double& min, double& max) {
|
||||
if (numbers.empty())
|
||||
{
|
||||
min = 0;
|
||||
max = 0;
|
||||
return;
|
||||
}
|
||||
max = numbers[0];
|
||||
min = numbers[0];
|
||||
for (double x : numbers) {
|
||||
if (x < min) min = x;
|
||||
else if (x > max) max = x;
|
||||
}
|
||||
}
|
||||
|
||||
for (double number : numbers) {
|
||||
if (number < min) {
|
||||
min = number;
|
||||
}
|
||||
if (number > max) {
|
||||
max = number;
|
||||
}
|
||||
}
|
||||
}
|
||||
std::vector<size_t> make_histogram(const std::vector<double>& numbers, size_t bin_count) {
|
||||
std::vector<size_t> bins(bin_count, 0);
|
||||
|
||||
|
||||
42
laba1.depend
42
laba1.depend
@@ -79,6 +79,46 @@
|
||||
"histogram_internal.h"
|
||||
<vector>
|
||||
|
||||
1748808114 D
|
||||
|
||||
1748875465 source:c:\users\aleks\onedrive\Ðàáî÷èé ñòîë\laba1 — êîïèÿbb\histogram.cpp
|
||||
"histogram.h"
|
||||
"histogram_internal.h"
|
||||
<vector>
|
||||
|
||||
1748386910 c:\users\aleks\onedrive\Ðàáî÷èé ñòîë\laba1 — êîïèÿbb\histogram.h
|
||||
<vector>
|
||||
|
||||
1748392232 c:\users\aleks\onedrive\Ðàáî÷èé ñòîë\laba1 — êîïèÿbb\histogram_internal.h
|
||||
<vector>
|
||||
|
||||
1749627705 source:c:\users\aleks\onedrive\Ðàáî÷èé ñòîë\laba1 — êîïèÿbb\main.cpp
|
||||
<iostream>
|
||||
<vector>
|
||||
<string>
|
||||
"histogram.h"
|
||||
"text.h"
|
||||
"svg.h"
|
||||
"histogram_internal.h"
|
||||
|
||||
1748730472 c:\users\aleks\onedrive\Ðàáî÷èé ñòîë\laba1 — êîïèÿbb\text.h
|
||||
<vector>
|
||||
|
||||
1749627429 c:\users\aleks\onedrive\Ðàáî÷èé ñòîë\laba1 — êîïèÿbb\svg.h
|
||||
<vector>
|
||||
<cstddef>
|
||||
|
||||
1748808306 source:c:\users\aleks\onedrive\Ðàáî÷èé ñòîë\laba1 — êîïèÿbb\text.cpp
|
||||
"text.h"
|
||||
<iostream>
|
||||
<vector>
|
||||
|
||||
1749628551 source:c:\users\aleks\onedrive\Ðàáî÷èé ñòîë\laba1 — êîïèÿbb\svg.cpp
|
||||
"svg.h"
|
||||
<iostream>
|
||||
|
||||
1748876656
|
||||
|
||||
1748849797 c:\users\aleks\onedrive\Ðàáî÷èé ñòîë\laba1\curl\include\curl\curl.h
|
||||
"curlver.h"
|
||||
"system.h"
|
||||
@@ -132,3 +172,5 @@
|
||||
|
||||
1748849797 c:\users\aleks\onedrive\Ðàáî÷èé ñòîë\laba1\curl\include\curl\typecheck-gcc.h
|
||||
|
||||
1748849797 c4f7fe14afe3358e7b9d7bc3578a9c995ebd7
|
||||
|
||||
|
||||
68
main.cpp
68
main.cpp
@@ -1,11 +1,10 @@
|
||||
#include <iostream>
|
||||
#include <vector>
|
||||
#include <string>
|
||||
#include "histogram.h"
|
||||
#include "text.h"
|
||||
#include "svg.h"
|
||||
#include <curl/curl.h>
|
||||
#include <sstream>
|
||||
#include <string>
|
||||
#include "histogram_internal.h"
|
||||
using namespace std;
|
||||
|
||||
struct Input {
|
||||
@@ -13,65 +12,28 @@ struct Input {
|
||||
size_t bin_count;
|
||||
};
|
||||
|
||||
Input input_data(istream& in, bool prompt) {
|
||||
Input inp;
|
||||
Input input_data() {
|
||||
Input in;
|
||||
size_t number_count;
|
||||
if (prompt){
|
||||
|
||||
cerr << "Enter number count: ";
|
||||
}
|
||||
in >> number_count;
|
||||
inp.numbers.resize(number_count);
|
||||
if (prompt){
|
||||
cin >> number_count;
|
||||
in.numbers.resize(number_count);
|
||||
|
||||
cerr << "Enter numbers: ";
|
||||
}
|
||||
for (size_t i = 0; i < number_count; i++) {
|
||||
in >> inp.numbers[i];
|
||||
cin >> in.numbers[i];
|
||||
}
|
||||
|
||||
if (prompt) {
|
||||
cerr << "Enter bin count: ";
|
||||
}
|
||||
in >> inp.bin_count;
|
||||
return inp;
|
||||
cin >> in.bin_count; // You were missing this line to read bin_count
|
||||
|
||||
return in;
|
||||
}
|
||||
|
||||
size_t
|
||||
write_data(void* items, size_t item_size, size_t item_count, void* ctx) {
|
||||
stringstream* buffer = reinterpret_cast<stringstream*>(ctx);
|
||||
size_t data_size = item_size * item_count;
|
||||
(*buffer).write(reinterpret_cast<const char*>(items), data_size);
|
||||
return data_size;
|
||||
}
|
||||
|
||||
Input
|
||||
download(const string& adress){
|
||||
stringstream buffer;
|
||||
CURL* curl = curl_easy_init();
|
||||
if(curl) {
|
||||
|
||||
CURLcode res;
|
||||
curl_easy_setopt(curl, CURLOPT_URL, adress.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){
|
||||
cerr << curl_easy_strerror(res);
|
||||
exit(1);
|
||||
}
|
||||
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);
|
||||
}
|
||||
|
||||
auto bins = make_histogram(input.numbers, input.bin_count);
|
||||
int main() {
|
||||
auto in = input_data();
|
||||
auto bins = make_histogram(in.numbers, in.bin_count); // Added second argument
|
||||
show_histogram_svg(bins);
|
||||
return 0;
|
||||
}
|
||||
|
||||
63
svg.cpp
63
svg.cpp
@@ -1,66 +1,55 @@
|
||||
#include "svg.h"
|
||||
#include <iostream>
|
||||
using namespace std;
|
||||
void
|
||||
svg_begin(double width, double height) {
|
||||
|
||||
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";
|
||||
cout << "<svg width='" << width << "' height='" << height << "' "
|
||||
<< "viewBox='0 0 " << width << " " << height << "' "
|
||||
<< "xmlns='http://www.w3.org/2000/svg'>\n";
|
||||
|
||||
// Ñòèëè äëÿ SVG
|
||||
cout << "<style>\n"
|
||||
<< " .bar { fill: #4CAF50; stroke: #388E3C; stroke-width: 1; }\n"
|
||||
<< " .text { font: 12px sans-serif; fill: #333; }\n"
|
||||
<< "</style>\n";
|
||||
}
|
||||
|
||||
void
|
||||
svg_end() {
|
||||
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_text(double left, double baseline, string text) {
|
||||
cout << "<text class='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 svg_rect(double x, double y, double width, double height) {
|
||||
cout << "<rect class='bar' x='" << x << "' y='" << y << "' width='" << width
|
||||
<< "' height='" << height << "' />";
|
||||
}
|
||||
|
||||
|
||||
|
||||
void
|
||||
show_histogram_svg(const vector<size_t>& bins)
|
||||
{
|
||||
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 YELLOW = "yellow";
|
||||
const auto PURPLE = "purple";
|
||||
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];
|
||||
}
|
||||
size_t max_count = 0;
|
||||
for (size_t count : bins) {
|
||||
if (count > max_count) max_count = count;
|
||||
}
|
||||
|
||||
for (size_t bin : bins)
|
||||
{
|
||||
double bin_width = (MAX_WIDTH)*(bin/max_count);
|
||||
for (size_t bin : bins) {
|
||||
const double bin_width = MAX_WIDTH * (static_cast<double>(bin) / max_count);
|
||||
svg_text(TEXT_LEFT, top + TEXT_BASELINE, to_string(bin));
|
||||
svg_rect(TEXT_WIDTH, top, bin_width, BIN_HEIGHT, YELLOW, PURPLE);
|
||||
svg_rect(TEXT_WIDTH, top, bin_width, BIN_HEIGHT);
|
||||
top += BIN_HEIGHT;
|
||||
}
|
||||
|
||||
|
||||
@@ -53,4 +53,10 @@ TEST_CASE("vector with zero") {
|
||||
CHECK(min == -1);
|
||||
CHECK(max == 1);
|
||||
}
|
||||
|
||||
TEST_CASE("empty vector") {
|
||||
double min = 0;
|
||||
double max = 0;
|
||||
find_minmax({}, min, max);
|
||||
CHECK(min == 0);
|
||||
CHECK(max == 0);
|
||||
}
|
||||
|
||||
@@ -67,3 +67,71 @@
|
||||
<vector>
|
||||
<cstddef>
|
||||
|
||||
1748875465 source:c:\users\aleks\onedrive\Ðàáî÷èé ñòîë\laba1 — êîïèÿbb\histogram.cpp
|
||||
"histogram.h"
|
||||
"histogram_internal.h"
|
||||
<vector>
|
||||
|
||||
1748386910 c:\users\aleks\onedrive\Ðàáî÷èé ñòîë\laba1 — êîïèÿbb\histogram.h
|
||||
<vector>
|
||||
|
||||
1748392232 c:\users\aleks\onedrive\Ðàáî÷èé ñòîë\laba1 — êîïèÿbb\histogram_internal.h
|
||||
<vector>
|
||||
|
||||
1748876656 source:c:\users\aleks\onedrive\Ðàáî÷èé ñòîë\laba1 — êîïèÿbb\svg.cpp
|
||||
"svg.h"
|
||||
<iostream>
|
||||
|
||||
1748810901 c:\users\aleks\onedrive\Ðàáî÷èé ñòîë\laba1 — êîïèÿbb\svg.h
|
||||
<vector>
|
||||
<cstddef>
|
||||
|
||||
1748875527 source:c:\users\aleks\onedrive\Ðàáî÷èé ñòîë\laba1 — êîïèÿbb\unittest.cpp
|
||||
"doctest.h"
|
||||
"histogram_internal.h"
|
||||
|
||||
1748392747 c:\users\aleks\onedrive\Ðàáî÷èé ñòîë\laba1 — êîïèÿbb\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>
|
||||
|
||||
|
||||
Ссылка в новой задаче
Block a user