Сравнить коммиты
28 Коммитов
f5e5c7e23b
...
main
| Автор | SHA1 | Дата | |
|---|---|---|---|
| 51270d3cea | |||
| ced73de2f7 | |||
| bdc44e502d | |||
| fa47ef4649 | |||
| 1fe50f24cc | |||
| ca0634537d | |||
| 0b9e56cf89 | |||
| 21e87301db | |||
| 26886daedc | |||
| 76950f6e99 | |||
| 44949175d5 | |||
| bf2f0d2977 | |||
| 7b584264f5 | |||
| cf9c79667b | |||
| f4321b02ee | |||
| 22fa6361a1 | |||
| 2b7c9fcc18 | |||
| 13ddf411a6 | |||
| a796205167 | |||
| 7497551d73 | |||
| 7066d4ac5f | |||
| 1cf2d711e4 | |||
| 5cbad4a303 | |||
| e975c13525 | |||
| 17e40af170 | |||
| da9d17d2fd | |||
| 9a486dec4b | |||
| 0e944a2c39 |
@@ -2,14 +2,20 @@
|
|||||||
#include <conio.h>
|
#include <conio.h>
|
||||||
#include <vector>
|
#include <vector>
|
||||||
#include <stdlib.h>
|
#include <stdlib.h>
|
||||||
using namespace std;
|
|
||||||
#include "histogram.h"
|
#include "histogram.h"
|
||||||
|
|
||||||
void
|
using namespace std;
|
||||||
|
|
||||||
|
bool
|
||||||
find_minmax(const vector<double>& numbers, double& min, double& max){
|
find_minmax(const vector<double>& numbers, double& min, double& max){
|
||||||
|
if (numbers.empty()){
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
|
||||||
min = numbers[0];
|
min = numbers[0];
|
||||||
min = numbers[0];
|
max = numbers[0];
|
||||||
for(size_t i=0; i < numbers.size(); i++){
|
for(size_t i=0; i < numbers.size(); i++)
|
||||||
|
{
|
||||||
if (numbers[i] < min)
|
if (numbers[i] < min)
|
||||||
{
|
{
|
||||||
min = numbers[i];
|
min = numbers[i];
|
||||||
@@ -19,6 +25,8 @@ find_minmax(const vector<double>& numbers, double& min, double& max){
|
|||||||
max = numbers[i];
|
max = numbers[i];
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
return true;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
|
|||||||
8
histogram_internal.h
Обычный файл
8
histogram_internal.h
Обычный файл
@@ -0,0 +1,8 @@
|
|||||||
|
#ifndef HISTOGRAM_INTERNAL_H_INCLUDED
|
||||||
|
#define HISTOGRAM_INTERNAL_H_INCLUDED
|
||||||
|
|
||||||
|
#include <vector>
|
||||||
|
bool
|
||||||
|
find_minmax(const std::vector<double>& numbers, double& min, double& max);
|
||||||
|
|
||||||
|
#endif // HISTOGRAM_INTERNAL_H_INCLUDED
|
||||||
8
input_colors_internal.h
Обычный файл
8
input_colors_internal.h
Обычный файл
@@ -0,0 +1,8 @@
|
|||||||
|
#ifndef INPUT_COLORS_INTERNAL_H_INCLUDED
|
||||||
|
#define INPUT_COLORS_INTERNAL_H_INCLUDED
|
||||||
|
|
||||||
|
void
|
||||||
|
input_colors(std::string &colors_stroke, std::string &colors_fill);
|
||||||
|
|
||||||
|
|
||||||
|
#endif // INPUT_COLORS_INTERNAL_H_INCLUDED
|
||||||
185
main.cpp
185
main.cpp
@@ -1,143 +1,92 @@
|
|||||||
#include <iostream>
|
#include <curl/curl.h>
|
||||||
#include <conio.h>
|
|
||||||
#include <vector>
|
#include <vector>
|
||||||
#include <stdlib.h>
|
#include <sstream>
|
||||||
|
#include <string>
|
||||||
|
#include <iostream>
|
||||||
|
#include <string>
|
||||||
|
|
||||||
|
#include "histogram.h"
|
||||||
|
#include "text.h"
|
||||||
|
#include "svg.h"
|
||||||
|
|
||||||
using namespace std;
|
using namespace std;
|
||||||
|
|
||||||
const size_t MAX_ELEMENT = 76;
|
|
||||||
struct Input {
|
struct Input {
|
||||||
vector<double> numbers;
|
vector<double> numbers;
|
||||||
size_t bin_count{};
|
size_t bin_count {};
|
||||||
};
|
};
|
||||||
|
|
||||||
Input
|
Input
|
||||||
input_data(){
|
input_data(istream& in, bool prompt){
|
||||||
|
|
||||||
size_t number_count;
|
size_t number_count;
|
||||||
cerr << "Enter number count: ";
|
if (prompt){
|
||||||
cin >> number_count;
|
cerr << "Enter number count: ";
|
||||||
|
}
|
||||||
|
in >> number_count;
|
||||||
|
|
||||||
Input in;
|
Input iinn;
|
||||||
|
iinn.numbers.resize(number_count);
|
||||||
in.numbers.resize(number_count);
|
if (prompt){
|
||||||
cerr << "Enter numbers: " << endl;
|
cerr << "Enter numbers: ";}
|
||||||
for (size_t i=0; i < number_count; i++)
|
for (size_t i = 0; i < number_count; i++)
|
||||||
{
|
{
|
||||||
cin >> in.numbers[i];
|
in >> iinn.numbers[i];
|
||||||
}
|
}
|
||||||
|
|
||||||
cerr << "Enter bin count: ";
|
if (prompt){
|
||||||
cin >> in.bin_count;
|
cerr << "Enter bin count: ";}
|
||||||
return in;
|
in >> iinn.bin_count;
|
||||||
|
|
||||||
|
return iinn;
|
||||||
}
|
}
|
||||||
|
|
||||||
void
|
static size_t
|
||||||
find_minmax(const vector<double>& numbers, double& min, double& max){
|
write_data(void* items, size_t item_size, size_t item_count, void* ctx){
|
||||||
min = numbers[0];
|
size_t data_size = item_size * item_count;
|
||||||
min = numbers[0];
|
stringstream* buffer = reinterpret_cast<stringstream*>(ctx);
|
||||||
for(size_t i=0; i < numbers.size(); i++){
|
buffer->write(reinterpret_cast<const char*>(items), data_size);
|
||||||
if (numbers[i] < min)
|
return data_size;
|
||||||
{
|
|
||||||
min = numbers[i];
|
|
||||||
}
|
|
||||||
else if (numbers[i] > max)
|
|
||||||
{
|
|
||||||
max = numbers[i];
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
vector<size_t>
|
Input
|
||||||
make_histogram(const vector<double>& numbers, size_t bin_count){
|
download(const string& address){
|
||||||
|
stringstream buffer;
|
||||||
double min, max;
|
CURL *curl = curl_easy_init();
|
||||||
find_minmax(numbers, min, max);
|
if(curl) {
|
||||||
|
CURLcode res;
|
||||||
vector<size_t> bins(bin_count);
|
curl_easy_setopt(curl, CURLOPT_URL, address.c_str());
|
||||||
|
curl_easy_setopt(curl, CURLOPT_WRITEFUNCTION, write_data);
|
||||||
double bin_size = (max - min) / bin_count;
|
curl_easy_setopt(curl, CURLOPT_WRITEDATA, &buffer);
|
||||||
for (size_t i = 0; i < numbers.size(); i++)
|
res = curl_easy_perform(curl);
|
||||||
{
|
if (res != CURLE_OK) {
|
||||||
bool found = false;
|
fprintf(stderr, "curl_easy_perform() failed: %s\n", curl_easy_strerror(res));
|
||||||
for (size_t j = 0; (j < bin_count - 1) && !found; j++)
|
exit(1);
|
||||||
{
|
|
||||||
auto lower_Bound = min + j * bin_size;
|
|
||||||
auto upper_Bound = min + (j + 1) * bin_size;
|
|
||||||
if ((lower_Bound <= numbers[i]) && (numbers[i] < upper_Bound))
|
|
||||||
{
|
|
||||||
bins[j]++;
|
|
||||||
found = true;
|
|
||||||
}
|
}
|
||||||
}
|
if(!res) {
|
||||||
if (!found)
|
curl_off_t dl;
|
||||||
{
|
res = curl_easy_getinfo(curl, CURLINFO_SIZE_DOWNLOAD_T, &dl);
|
||||||
bins[bin_count - 1]++;
|
if(!res) {
|
||||||
}
|
cerr<<"Downloaded "<<dl<< " bytes\n";
|
||||||
}
|
}
|
||||||
|
|
||||||
double max_count = bins[0];
|
|
||||||
for (size_t j=0; j < bin_count; j++){
|
|
||||||
if (bins[j] > max_count) {
|
|
||||||
max_count = bins[j];
|
|
||||||
}
|
|
||||||
}
|
|
||||||
return bins;
|
|
||||||
}
|
|
||||||
|
|
||||||
void
|
|
||||||
show_histogram_text(const vector<size_t> bins, size_t bin_count){
|
|
||||||
double max_count = bins[0];
|
|
||||||
for (size_t j=0; j < bin_count; j++){
|
|
||||||
if (bins[j] > max_count) {
|
|
||||||
max_count = bins[j];
|
|
||||||
}
|
|
||||||
}
|
|
||||||
vector<size_t> height(bin_count);
|
|
||||||
for (size_t j=0; j < bin_count; j++){
|
|
||||||
height[j] = MAX_ELEMENT * (bins[j]/max_count);
|
|
||||||
}
|
|
||||||
|
|
||||||
if (max_count > MAX_ELEMENT){
|
|
||||||
for (size_t j=0; j < bin_count; j++)
|
|
||||||
{
|
|
||||||
if (bins[j] < 100) {
|
|
||||||
cout << " ";
|
|
||||||
if (bins[j] < 10){
|
|
||||||
cout << " ";
|
|
||||||
}
|
}
|
||||||
}
|
curl_easy_cleanup(curl);
|
||||||
cout << bins[j] << "|";
|
}
|
||||||
|
return input_data(buffer, false);
|
||||||
for (size_t i=0; i < height[j]; i++){
|
|
||||||
cout << "*";
|
|
||||||
}
|
|
||||||
cout << endl;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
else {
|
|
||||||
for (size_t j=0; j < bin_count; j++)
|
|
||||||
{
|
|
||||||
if (bins[j] < 100) {
|
|
||||||
cout << " ";
|
|
||||||
if (bins[j] < 10){
|
|
||||||
cout << " ";
|
|
||||||
}
|
|
||||||
}
|
|
||||||
cout << bins[j] << "|";
|
|
||||||
|
|
||||||
for (size_t i=0; i < bins[j];i++){
|
|
||||||
cout << "*";
|
|
||||||
}
|
|
||||||
cout << endl;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
int main()
|
int
|
||||||
|
main(int argc, char* argv[])
|
||||||
{
|
{
|
||||||
|
#include <curl/curl.h>
|
||||||
|
curl_global_init(CURL_GLOBAL_ALL);
|
||||||
|
Input input;
|
||||||
|
if (argc > 1) {
|
||||||
|
input = download(argv[1]);
|
||||||
|
} else {
|
||||||
|
input = input_data(cin, true);
|
||||||
|
}
|
||||||
|
|
||||||
auto in = input_data();
|
const auto bins = make_histogram(input.numbers, input.bin_count);
|
||||||
auto bins = make_histogram(in.numbers, in.bin_count);
|
show_histogram_svg(bins);
|
||||||
show_histogram_text(bins, in.bin_count);
|
|
||||||
return 0;
|
|
||||||
}
|
}
|
||||||
|
|||||||
6
marks.06.txt
Обычный файл
6
marks.06.txt
Обычный файл
@@ -0,0 +1,6 @@
|
|||||||
|
10
|
||||||
|
3 3 4 4 4 4 4 5 5 5
|
||||||
|
3
|
||||||
|
Yes
|
||||||
|
darkgreen
|
||||||
|
lightcoral
|
||||||
33
svg.cpp
Обычный файл
33
svg.cpp
Обычный файл
@@ -0,0 +1,33 @@
|
|||||||
|
#include <iostream>
|
||||||
|
#include <conio.h>
|
||||||
|
#include <vector>
|
||||||
|
#include <stdlib.h>
|
||||||
|
#include "svg.h"
|
||||||
|
#include "text.h"
|
||||||
|
using namespace std;
|
||||||
|
|
||||||
|
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
|
||||||
|
show_histogram_svg(const vector<size_t>& bins) {
|
||||||
|
svg_begin(400, 300);
|
||||||
|
svg_text(20, 20, to_string(bins[0]));
|
||||||
|
svg_end();
|
||||||
|
}
|
||||||
|
|
||||||
|
void svg_text(double left, double baseline, string text){
|
||||||
|
cout << "text x='" << left << "' y='" << baseline << "'>text</text>";
|
||||||
|
}
|
||||||
8
svg.h
Обычный файл
8
svg.h
Обычный файл
@@ -0,0 +1,8 @@
|
|||||||
|
#ifndef SVG_H_INCLUDED
|
||||||
|
#define SVG_H_INCLUDED
|
||||||
|
|
||||||
|
#include <vector>
|
||||||
|
void
|
||||||
|
show_histogram_svg(const std::vector<size_t>& bins);
|
||||||
|
|
||||||
|
#endif // SVG_H_INCLUDED
|
||||||
82
svg01.cpp
Обычный файл
82
svg01.cpp
Обычный файл
@@ -0,0 +1,82 @@
|
|||||||
|
#include "svg01.h"
|
||||||
|
#include <vector>
|
||||||
|
#include <iostream>
|
||||||
|
#include <string>
|
||||||
|
|
||||||
|
using namespace std;
|
||||||
|
|
||||||
|
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 colors_stroke, string colors_fill){
|
||||||
|
cout << "<rect x='" << x << "' y='" << y << "' width='" << width << "' height='" << height << "' stroke='" << colors_stroke << "' fill='" << colors_fill << "'/>";
|
||||||
|
}
|
||||||
|
|
||||||
|
void
|
||||||
|
input_colors(string &colors_stroke, string &colors_fill){
|
||||||
|
string color_personal;
|
||||||
|
cerr << "Do you want to change colors? Yes/No" << endl;
|
||||||
|
cin >> color_personal;
|
||||||
|
while ((color_personal != "No") && (color_personal != "Yes")){
|
||||||
|
cerr << "WRONG ANSWER! Try again!" << endl;
|
||||||
|
cin >> color_personal;
|
||||||
|
}
|
||||||
|
if (color_personal == "No"){
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
else {
|
||||||
|
cerr << "What color for a stroke do you want?" << endl;
|
||||||
|
cin >> colors_stroke;
|
||||||
|
cerr << "What color to fill do you want?" << endl;
|
||||||
|
cin >> colors_fill;
|
||||||
|
}
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
|
void
|
||||||
|
show_histogram_svg(const vector<size_t>& bins, string colors_stroke, string colors_fill) {
|
||||||
|
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;
|
||||||
|
|
||||||
|
svg_begin(IMAGE_WIDTH, IMAGE_HEIGHT);
|
||||||
|
double top = 0;
|
||||||
|
double maxel = bins[0];
|
||||||
|
for (size_t bin : bins) {
|
||||||
|
if (maxel > bin){
|
||||||
|
maxel = bin;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
for (size_t bin : bins) {
|
||||||
|
const double bin_width = (( IMAGE_WIDTH - TEXT_WIDTH ) / BLOCK_WIDTH ) * ( bin / maxel );
|
||||||
|
svg_text(TEXT_LEFT, top + TEXT_BASELINE, to_string(bin));
|
||||||
|
svg_rect(TEXT_WIDTH, top, bin_width, BIN_HEIGHT, colors_stroke, colors_fill);
|
||||||
|
top += BIN_HEIGHT;
|
||||||
|
}
|
||||||
|
svg_end();
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
9
svg01.h
Обычный файл
9
svg01.h
Обычный файл
@@ -0,0 +1,9 @@
|
|||||||
|
#pragma once
|
||||||
|
#include <string>
|
||||||
|
#include <vector>
|
||||||
|
|
||||||
|
void
|
||||||
|
show_histogram_svg(const std::vector<size_t>& bins, std::string colors_stroke, std::string colors_fill);
|
||||||
|
|
||||||
|
void
|
||||||
|
input_colors(std::string &colors_stroke, std::string &colors_fill);
|
||||||
21
unittest.cpp
21
unittest.cpp
@@ -14,9 +14,7 @@ TEST_CASE("distinct positive numbers") {
|
|||||||
TEST_CASE("empty vector"){
|
TEST_CASE("empty vector"){
|
||||||
double min = 0;
|
double min = 0;
|
||||||
double max = 0;
|
double max = 0;
|
||||||
find_minmax({ 0, 0 }, min, max);
|
CHECK(!find_minmax({}, min, max));
|
||||||
CHECK(min == 0);
|
|
||||||
CHECK(max == 0);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
TEST_CASE("same elements"){
|
TEST_CASE("same elements"){
|
||||||
@@ -24,5 +22,20 @@ TEST_CASE("same elements"){
|
|||||||
double max = 0;
|
double max = 0;
|
||||||
find_minmax({ 1, 1 }, min, max);
|
find_minmax({ 1, 1 }, min, max);
|
||||||
CHECK(min == max);
|
CHECK(min == max);
|
||||||
CHECK(max == min);
|
}
|
||||||
|
|
||||||
|
TEST_CASE("only one number"){
|
||||||
|
double min = 0;
|
||||||
|
double max = 0;
|
||||||
|
find_minmax({1}, min, max);
|
||||||
|
CHECK(min == 1);
|
||||||
|
CHECK(max == 1);
|
||||||
|
}
|
||||||
|
|
||||||
|
TEST_CASE("distinct negative numbers") {
|
||||||
|
double min = 0;
|
||||||
|
double max = 0;
|
||||||
|
find_minmax({-1, -2}, min, max);
|
||||||
|
CHECK(min == -2);
|
||||||
|
CHECK(max == -1);
|
||||||
}
|
}
|
||||||
|
|||||||
20
unittest_personal.cpp
Обычный файл
20
unittest_personal.cpp
Обычный файл
@@ -0,0 +1,20 @@
|
|||||||
|
#define DOCTEST_CONFIG_NO_MULTITHREADING
|
||||||
|
#define DOCTEST_CONFIG_IMPLEMENT_WITH_MAIN
|
||||||
|
#include "doctest.h"
|
||||||
|
#include "input_colors_internal.h"
|
||||||
|
|
||||||
|
TEST_CASE("Mistakes in spelling") {
|
||||||
|
std::string colors_stroke = "black";
|
||||||
|
std::string colors_fill = "green";
|
||||||
|
input_colors(colors_stroke, colors_fill);
|
||||||
|
CHECK(colors_stroke == "yellow");
|
||||||
|
CHECK(colors_fill == "pink");
|
||||||
|
}
|
||||||
|
|
||||||
|
TEST_CASE("The answer is No") {
|
||||||
|
std::string colors_stroke = "black";
|
||||||
|
std::string colors_fill = "green";
|
||||||
|
input_colors(colors_stroke, colors_fill);
|
||||||
|
CHECK(colors_stroke == "black");
|
||||||
|
CHECK(colors_fill == "green");
|
||||||
|
}
|
||||||
Ссылка в новой задаче
Block a user