Сравнить коммиты
10 Коммитов
| Автор | SHA1 | Дата | |
|---|---|---|---|
| d7238bd7a7 | |||
| 2a189de275 | |||
| 08cd869057 | |||
| 0a27418aa3 | |||
| b7359ca5e6 | |||
| 003f85e315 | |||
| 5320147504 | |||
| be4ea374a9 | |||
| 8c9fba0c39 | |||
| 7a613b4b47 |
3
.gitignore
поставляемый
3
.gitignore
поставляемый
@@ -1,3 +1,6 @@
|
||||
/curl.h
|
||||
/Debug
|
||||
/curl/
|
||||
/03-scaling.actual.txt
|
||||
/03-scaling.expected.txt
|
||||
/03-scaling.input.txt
|
||||
|
||||
@@ -4,7 +4,7 @@
|
||||
#include "histogram.h"
|
||||
using namespace std;
|
||||
|
||||
void find_minmax(const vector<double>& numbers, double& min, double& max) {
|
||||
bool find_minmax(const vector<double>& numbers, double& min, double& max) {
|
||||
if (numbers.size() != 0)
|
||||
{
|
||||
min = numbers[0];
|
||||
@@ -20,7 +20,29 @@ void find_minmax(const vector<double>& numbers, double& min, double& max) {
|
||||
max = x;
|
||||
}
|
||||
}
|
||||
return true;
|
||||
}
|
||||
return false;
|
||||
}
|
||||
|
||||
void find_max_capacity(const vector<size_t>& bins, size_t& max_bin_capacity)
|
||||
{
|
||||
max_bin_capacity = bins[0];
|
||||
for (size_t x : bins)
|
||||
{
|
||||
if (x > max_bin_capacity)
|
||||
{
|
||||
max_bin_capacity = x;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
void find_range_border(const vector<double>& numbers, size_t bin_count, size_t bin_number, double& range_border)
|
||||
{
|
||||
double min_number, max_number;
|
||||
find_minmax(numbers, min_number, max_number);
|
||||
double bin_size = abs((max_number - min_number) / bin_count);
|
||||
range_border = min_number + bin_number * bin_size;
|
||||
}
|
||||
|
||||
vector<size_t> make_histogram(const vector<double>& numbers, size_t bin_count)
|
||||
@@ -32,7 +54,7 @@ vector<size_t> make_histogram(const vector<double>& numbers, size_t bin_count)
|
||||
{
|
||||
bins[i] = 0;
|
||||
}
|
||||
double bin_size = (max_number - min_number) / bin_count;
|
||||
double bin_size = abs((max_number - min_number) / bin_count);
|
||||
for (size_t i = 0; i < numbers.size(); i++)
|
||||
{
|
||||
bool found = false;
|
||||
|
||||
@@ -1,4 +1,6 @@
|
||||
#pragma once
|
||||
#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);
|
||||
void find_range_border(const std::vector<double>& numbers, size_t bin_count, size_t bin_number, double& range_border);
|
||||
void find_max_capacity(const std::vector<size_t>& bins, size_t& max_bin_capacity);
|
||||
@@ -1,4 +1,4 @@
|
||||
#pragma once
|
||||
#include <vector>
|
||||
|
||||
void find_minmax(const std::vector<double>& numbers, double& min, double& max);
|
||||
bool find_minmax(const std::vector<double>& numbers, double& min, double& max);
|
||||
@@ -29,6 +29,7 @@
|
||||
<ExcludedFromBuild Condition="'$(Configuration)|$(Platform)'=='Release|x64'">false</ExcludedFromBuild>
|
||||
</ClInclude>
|
||||
<ClInclude Include="histogram_internal.h" />
|
||||
<ClInclude Include="range_border.h" />
|
||||
<ClInclude Include="svg.h" />
|
||||
<ClInclude Include="text.h" />
|
||||
</ItemGroup>
|
||||
@@ -84,16 +85,24 @@
|
||||
<Import Project="$(UserRootDir)\Microsoft.Cpp.$(Platform).user.props" Condition="exists('$(UserRootDir)\Microsoft.Cpp.$(Platform).user.props')" Label="LocalAppDataPlatform" />
|
||||
</ImportGroup>
|
||||
<PropertyGroup Label="UserMacros" />
|
||||
<PropertyGroup Condition="'$(Configuration)|$(Platform)'=='Release|x64'">
|
||||
<LibraryPath>$(VC_LibraryPath_x64);$(WindowsSDK_LibraryPath_x64)</LibraryPath>
|
||||
<IncludePath>$(VC_IncludePath);$(WindowsSDK_IncludePath)</IncludePath>
|
||||
<ExternalIncludePath>$(VC_IncludePath);$(WindowsSDK_IncludePath)</ExternalIncludePath>
|
||||
</PropertyGroup>
|
||||
<ItemDefinitionGroup Condition="'$(Configuration)|$(Platform)'=='Debug|Win32'">
|
||||
<ClCompile>
|
||||
<WarningLevel>Level3</WarningLevel>
|
||||
<SDLCheck>true</SDLCheck>
|
||||
<PreprocessorDefinitions>WIN32;_DEBUG;_CONSOLE;%(PreprocessorDefinitions)</PreprocessorDefinitions>
|
||||
<ConformanceMode>true</ConformanceMode>
|
||||
<AdditionalIncludeDirectories>D:/prg/lab03/curl/include;%(AdditionalIncludeDirectories)</AdditionalIncludeDirectories>
|
||||
</ClCompile>
|
||||
<Link>
|
||||
<SubSystem>Console</SubSystem>
|
||||
<GenerateDebugInformation>true</GenerateDebugInformation>
|
||||
<AdditionalLibraryDirectories>D:\prg\lab03\curl\lib;%(AdditionalLibraryDirectories)</AdditionalLibraryDirectories>
|
||||
<AdditionalDependencies>$(CoreLibraryDependencies);libcurl.dll.a;%(AdditionalDependencies)</AdditionalDependencies>
|
||||
</Link>
|
||||
</ItemDefinitionGroup>
|
||||
<ItemDefinitionGroup Condition="'$(Configuration)|$(Platform)'=='Release|Win32'">
|
||||
@@ -104,12 +113,15 @@
|
||||
<SDLCheck>true</SDLCheck>
|
||||
<PreprocessorDefinitions>WIN32;NDEBUG;_CONSOLE;%(PreprocessorDefinitions)</PreprocessorDefinitions>
|
||||
<ConformanceMode>true</ConformanceMode>
|
||||
<AdditionalIncludeDirectories>D:/prg/lab03/curl/include;%(AdditionalIncludeDirectories)</AdditionalIncludeDirectories>
|
||||
</ClCompile>
|
||||
<Link>
|
||||
<SubSystem>Console</SubSystem>
|
||||
<EnableCOMDATFolding>true</EnableCOMDATFolding>
|
||||
<OptimizeReferences>true</OptimizeReferences>
|
||||
<GenerateDebugInformation>true</GenerateDebugInformation>
|
||||
<AdditionalLibraryDirectories>D:\prg\lab03\curl\lib;%(AdditionalLibraryDirectories)</AdditionalLibraryDirectories>
|
||||
<AdditionalDependencies>$(CoreLibraryDependencies);libcurl.dll.a;%(AdditionalDependencies)</AdditionalDependencies>
|
||||
</Link>
|
||||
</ItemDefinitionGroup>
|
||||
<ItemDefinitionGroup Condition="'$(Configuration)|$(Platform)'=='Debug|x64'">
|
||||
@@ -118,10 +130,13 @@
|
||||
<SDLCheck>true</SDLCheck>
|
||||
<PreprocessorDefinitions>_DEBUG;_CONSOLE;%(PreprocessorDefinitions)</PreprocessorDefinitions>
|
||||
<ConformanceMode>true</ConformanceMode>
|
||||
<AdditionalIncludeDirectories>D:/prg/lab03/curl/include;%(AdditionalIncludeDirectories)</AdditionalIncludeDirectories>
|
||||
</ClCompile>
|
||||
<Link>
|
||||
<SubSystem>Console</SubSystem>
|
||||
<GenerateDebugInformation>true</GenerateDebugInformation>
|
||||
<AdditionalLibraryDirectories>D:\prg\lab03\curl\lib;%(AdditionalLibraryDirectories)</AdditionalLibraryDirectories>
|
||||
<AdditionalDependencies>$(CoreLibraryDependencies);libcurl.dll.a;%(AdditionalDependencies)</AdditionalDependencies>
|
||||
</Link>
|
||||
</ItemDefinitionGroup>
|
||||
<ItemDefinitionGroup Condition="'$(Configuration)|$(Platform)'=='Release|x64'">
|
||||
@@ -132,13 +147,25 @@
|
||||
<SDLCheck>true</SDLCheck>
|
||||
<PreprocessorDefinitions>NDEBUG;_CONSOLE;%(PreprocessorDefinitions)</PreprocessorDefinitions>
|
||||
<ConformanceMode>true</ConformanceMode>
|
||||
<AdditionalModuleDependencies>
|
||||
</AdditionalModuleDependencies>
|
||||
<AdditionalIncludeDirectories>D:/prg/lab03/curl/include;%(AdditionalIncludeDirectories)</AdditionalIncludeDirectories>
|
||||
</ClCompile>
|
||||
<Link>
|
||||
<SubSystem>Console</SubSystem>
|
||||
<EnableCOMDATFolding>true</EnableCOMDATFolding>
|
||||
<OptimizeReferences>true</OptimizeReferences>
|
||||
<GenerateDebugInformation>true</GenerateDebugInformation>
|
||||
<AdditionalLibraryDirectories>D:\prg\lab03\curl\lib;%(AdditionalLibraryDirectories)</AdditionalLibraryDirectories>
|
||||
<AdditionalDependencies>$(CoreLibraryDependencies);libcurl.dll.a;%(AdditionalDependencies)</AdditionalDependencies>
|
||||
</Link>
|
||||
<CustomBuildStep>
|
||||
<Inputs>
|
||||
</Inputs>
|
||||
</CustomBuildStep>
|
||||
<ProjectReference>
|
||||
<UseLibraryDependencyInputs>true</UseLibraryDependencyInputs>
|
||||
</ProjectReference>
|
||||
</ItemDefinitionGroup>
|
||||
<Import Project="$(VCTargetsPath)\Microsoft.Cpp.targets" />
|
||||
<ImportGroup Label="ExtensionTargets">
|
||||
|
||||
98
lab1.cpp
98
lab1.cpp
@@ -4,30 +4,108 @@
|
||||
#include "histogram.h"
|
||||
#include "text.h"
|
||||
#include "svg.h"
|
||||
#include <curl/curl.h>
|
||||
#include <sstream>
|
||||
#include <string>
|
||||
using namespace std;
|
||||
struct Input {
|
||||
vector<double> numbers;
|
||||
size_t bin_count{};
|
||||
string fill="black";
|
||||
};
|
||||
|
||||
Input input_data() {
|
||||
Input input_data(istream& in_stream, bool prompt) {
|
||||
Input in;
|
||||
size_t number_count;
|
||||
cin >> number_count;
|
||||
in.numbers.resize(number_count);
|
||||
for (size_t i = 0; i < number_count; i++) {
|
||||
cin >> in.numbers[i];
|
||||
if (prompt) {
|
||||
cerr << "Введите количество значений\n";
|
||||
}
|
||||
cin >> in.bin_count;
|
||||
in_stream >> number_count;
|
||||
in.numbers.resize(number_count);
|
||||
if (prompt) {
|
||||
cerr << "Введите значения\n";
|
||||
}
|
||||
for (size_t i = 0; i < number_count; i++) {
|
||||
in_stream >> in.numbers[i];
|
||||
}
|
||||
if (prompt) {
|
||||
cerr << "Введите количество столбцов гистограммы\n";
|
||||
}
|
||||
in_stream >> in.bin_count;
|
||||
return in;
|
||||
}
|
||||
|
||||
int main()
|
||||
size_t write_data(void* items, size_t item_size, size_t item_count, void* ctx) {
|
||||
stringstream* buffer = reinterpret_cast<stringstream*>(ctx);
|
||||
const char* char_items = reinterpret_cast<const char*>(items);
|
||||
size_t data_size = item_size * item_count;
|
||||
buffer->write(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_WRITEFUNCTION, write_data);
|
||||
curl_easy_setopt(curl, CURLOPT_URL, address.c_str());
|
||||
curl_easy_setopt(curl, CURLOPT_WRITEDATA, &buffer);
|
||||
res = curl_easy_perform(curl);
|
||||
if (res != 0) {
|
||||
cerr << curl_easy_strerror(res);
|
||||
exit(1);
|
||||
}
|
||||
}
|
||||
curl_easy_cleanup(curl);
|
||||
|
||||
return input_data(buffer, false);
|
||||
}
|
||||
|
||||
int main(int argc, char* argv[])
|
||||
{
|
||||
Input input;
|
||||
bool input_created = false;
|
||||
if (argc == 2) {
|
||||
input = download(argv[1]);
|
||||
input_created = true;
|
||||
}
|
||||
if (argc > 2) {
|
||||
for (int i = 0; i < argc; i++) {
|
||||
if (strcmp(argv[i], "-fill") == 0) {
|
||||
if (argc < 4) {
|
||||
cerr << "-fill must be followed by a color name or hex code";
|
||||
exit(1);
|
||||
}
|
||||
else if (i == 3) {
|
||||
cerr << "-fill must be followed by a color name or hex code";
|
||||
exit(1);
|
||||
}
|
||||
else {
|
||||
if (i == 2) {
|
||||
input = download(argv[1]);
|
||||
}
|
||||
else {
|
||||
input = download(argv[3]);
|
||||
}
|
||||
input.fill = argv[i + 1];
|
||||
input_created = true;
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
else {
|
||||
input = input_data(cin, true);
|
||||
}
|
||||
if (!input_created) {
|
||||
cerr << "invalid arguements";
|
||||
exit(1);
|
||||
}
|
||||
curl_global_init(CURL_GLOBAL_ALL);
|
||||
const size_t SCREEN_WIDTH = 80;
|
||||
const size_t MAX_ASTERISK = SCREEN_WIDTH - 3 - 1;
|
||||
auto in = input_data();
|
||||
auto bins = make_histogram(in.numbers, in.bin_count);
|
||||
show_histogram_svg(bins);
|
||||
vector<double> numbers_data = input.numbers;
|
||||
auto bins = make_histogram(input.numbers, input.bin_count);
|
||||
show_histogram_svg(bins, input.numbers, input.bin_count, input.fill);
|
||||
return 0;
|
||||
}
|
||||
|
||||
4
range_border.h
Обычный файл
4
range_border.h
Обычный файл
@@ -0,0 +1,4 @@
|
||||
#pragma once
|
||||
#include<vector>
|
||||
|
||||
void find_range_border(const std::vector<double>& numbers, size_t bin_count, size_t bin_number, double& range_border);
|
||||
19
svg.cpp
19
svg.cpp
@@ -3,7 +3,7 @@
|
||||
#include <vector>
|
||||
#include <string>
|
||||
#include "svg.h"
|
||||
#include "text.h"
|
||||
#include "histogram.h"
|
||||
using namespace std;
|
||||
|
||||
void svg_begin(double width, double height) {
|
||||
@@ -27,7 +27,7 @@ void svg_rect(double x, double y, double width, double height, string stroke = "
|
||||
cout << "<rect x = '" << x << "' y = '" << y << "' width = '" << width << "' height = '" << height << "' stroke = '" << stroke << "' fill = '" << fill << "' />";
|
||||
}
|
||||
|
||||
void show_histogram_svg(const vector<size_t>& bins) {
|
||||
void show_histogram_svg(const vector<size_t>& bins, const vector<double>& numbers, size_t& bin_count, string& fill) {
|
||||
const auto IMAGE_WIDTH = 400;
|
||||
const auto IMAGE_HEIGHT = 300;
|
||||
const auto TEXT_LEFT = 20;
|
||||
@@ -39,10 +39,17 @@ void show_histogram_svg(const vector<size_t>& bins) {
|
||||
double top = 0;
|
||||
size_t max_bin_capacity{};
|
||||
find_max_capacity(bins, max_bin_capacity);
|
||||
for (size_t bin : bins) {
|
||||
const double bin_width = (IMAGE_WIDTH - TEXT_WIDTH) * (static_cast<double>(bin) / max_bin_capacity);
|
||||
svg_text(TEXT_LEFT, top + TEXT_BASELINE, to_string(bin));
|
||||
svg_rect(TEXT_WIDTH, top, bin_width, BIN_HEIGHT, "purple", "#ffcc00");
|
||||
double range_border = 0;
|
||||
for (size_t i = 0; i < bins.size(); i++) {
|
||||
const double bin_width = (IMAGE_WIDTH - TEXT_WIDTH) * (static_cast<double>(bins[i]) / max_bin_capacity);
|
||||
if (i != 0)
|
||||
{
|
||||
find_range_border(numbers, bin_count, i, range_border);
|
||||
svg_text(TEXT_LEFT, top + TEXT_BASELINE, to_string(range_border));
|
||||
top += BIN_HEIGHT;
|
||||
}
|
||||
svg_text(TEXT_LEFT, top + TEXT_BASELINE, to_string(bins[i]));
|
||||
svg_rect(TEXT_WIDTH, top, bin_width, BIN_HEIGHT, "purple", fill);
|
||||
top += BIN_HEIGHT;
|
||||
}
|
||||
svg_end();
|
||||
|
||||
3
svg.h
3
svg.h
@@ -1,4 +1,5 @@
|
||||
#pragma once
|
||||
#include<vector>
|
||||
#include<string>
|
||||
|
||||
void show_histogram_svg(const std::vector<size_t>& bins);
|
||||
void show_histogram_svg(const std::vector<size_t>& bins, const std::vector<double>& numbers, std::size_t& bin_count, std::string& fill);
|
||||
12
text.cpp
12
text.cpp
@@ -4,18 +4,6 @@
|
||||
#include "text.h"
|
||||
using namespace std;
|
||||
|
||||
void find_max_capacity(const vector<size_t>& bins, size_t& max_bin_capacity)
|
||||
{
|
||||
max_bin_capacity = bins[0];
|
||||
for (size_t x : bins)
|
||||
{
|
||||
if (x > max_bin_capacity)
|
||||
{
|
||||
max_bin_capacity = x;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
void show_histogram_text(const vector<size_t>& bins, size_t max_asterisk)
|
||||
{
|
||||
size_t max_bin_capacity{};
|
||||
|
||||
18
unittest.cpp
18
unittest.cpp
@@ -2,6 +2,7 @@
|
||||
#define DOCTEST_CONFIG_IMPLEMENT_WITH_MAIN
|
||||
#include "doctest.h"
|
||||
#include "histogram_internal.h"
|
||||
#include "range_border.h"
|
||||
|
||||
TEST_CASE("distinct positive numbers") {
|
||||
double min = 0;
|
||||
@@ -14,14 +15,25 @@ TEST_CASE("distinct positive numbers") {
|
||||
TEST_CASE("empty vector") {
|
||||
double min = 0;
|
||||
double max = 0;
|
||||
find_minmax({}, min, max);
|
||||
CHECK(min == 0);
|
||||
CHECK(max == 0);
|
||||
CHECK(!find_minmax({}, min, max));
|
||||
}
|
||||
|
||||
TEST_CASE("negative values") {
|
||||
double min = 0;
|
||||
double max = 0;
|
||||
find_minmax({-2, -4}, min, max);
|
||||
CHECK(min == -4);
|
||||
CHECK(max == -2);
|
||||
}
|
||||
|
||||
TEST_CASE("negative borders") {
|
||||
double range_border = 0;
|
||||
find_range_border({-2, -4}, 2, 1, range_border);
|
||||
CHECK(range_border == -3);
|
||||
}
|
||||
|
||||
TEST_CASE("more bins than numbers") {
|
||||
double range_border = 0;
|
||||
find_range_border({3}, 2, 1, range_border);
|
||||
CHECK(range_border == 3);
|
||||
}
|
||||
@@ -133,6 +133,7 @@
|
||||
<ItemGroup>
|
||||
<ClInclude Include="doctest.h" />
|
||||
<ClInclude Include="histogram_internal.h" />
|
||||
<ClInclude Include="range_border.h" />
|
||||
</ItemGroup>
|
||||
<Import Project="$(VCTargetsPath)\Microsoft.Cpp.targets" />
|
||||
<ImportGroup Label="ExtensionTargets">
|
||||
|
||||
Ссылка в новой задаче
Block a user