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

...

28 Коммитов

Автор SHA1 Сообщение Дата
51270d3cea code: индивидуальный вариант 2024-05-20 16:54:36 +03:00
ced73de2f7 build: подключили cURL к проекту 2024-05-20 14:59:58 +03:00
bdc44e502d code: добавили параметр prompt 2024-05-19 00:56:31 +03:00
fa47ef4649 code: переписали ввод и вызов функции ввода данных 2024-05-19 00:47:39 +03:00
1fe50f24cc text: текстовый файл для варианта 2024-05-08 16:03:19 +03:00
ca0634537d code: изменили тест на пустой вектор 2024-05-08 15:57:36 +03:00
0b9e56cf89 code: изменили тип и тело функции 2024-05-08 15:56:35 +03:00
21e87301db code: изменили тип функции 2024-05-08 15:55:18 +03:00
26886daedc code: исправлен вывод предложений 2024-05-08 08:48:00 +03:00
76950f6e99 code: исправлен тест 2024-05-08 08:39:47 +03:00
44949175d5 code: тесты тесты 2024-05-06 16:46:09 +03:00
bf2f0d2977 code: тесты 2024-05-06 16:44:58 +03:00
7b584264f5 code: цвета 2024-05-06 16:27:16 +03:00
cf9c79667b code: цвета 2024-05-06 16:27:06 +03:00
f4321b02ee code: цвета 2024-05-06 16:26:28 +03:00
22fa6361a1 code: тесты 2024-05-06 14:24:21 +03:00
2b7c9fcc18 code: персональный вариант 2.0 2024-04-29 12:39:50 +03:00
13ddf411a6 code: персональный вариант 2024-04-29 12:34:47 +03:00
a796205167 code: персональный вариант 2024-04-29 12:34:17 +03:00
7497551d73 code: персональный вариант 2024-04-29 12:33:53 +03:00
7066d4ac5f code: добавлено масштабирование 2024-04-29 10:41:54 +03:00
1cf2d711e4 code: добавили гимтограмме цвет 2024-04-28 23:36:47 +03:00
5cbad4a303 code: добавили вывод в формате svg 2024-04-28 21:44:30 +03:00
e975c13525 build: файл реализации, формат svg 2024-04-28 21:34:39 +03:00
17e40af170 Merge branch 'main' of http://uit.mpei.ru/git/ShinkarenkoVA/cs-lab34 2024-04-28 21:32:22 +03:00
da9d17d2fd build: формат svg 2024-04-28 21:31:04 +03:00
9a486dec4b build: формат svg 2024-04-22 05:07:15 +03:00
0e944a2c39 build: заголовочный файл 2024-04-22 05:05:59 +03:00
11 изменённых файлов: 270 добавлений и 126 удалений

Просмотреть файл

@@ -2,14 +2,20 @@
#include <conio.h>
#include <vector>
#include <stdlib.h>
using namespace std;
#include "histogram.h"
void
using namespace std;
bool
find_minmax(const vector<double>& numbers, double& min, double& max){
if (numbers.empty()){
return false;
}
min = numbers[0];
min = numbers[0];
for(size_t i=0; i < numbers.size(); i++){
max = numbers[0];
for(size_t i=0; i < numbers.size(); i++)
{
if (numbers[i] < min)
{
min = numbers[i];
@@ -19,6 +25,8 @@ find_minmax(const vector<double>& numbers, double& min, double& max){
max = numbers[i];
}
}
return true;
}

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 Обычный файл
Просмотреть файл

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

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

@@ -1,143 +1,92 @@
#include <iostream>
#include <conio.h>
#include <curl/curl.h>
#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;
const size_t MAX_ELEMENT = 76;
struct Input {
vector<double> numbers;
size_t bin_count{};
size_t bin_count {};
};
Input
input_data(){
input_data(istream& in, bool prompt){
size_t number_count;
if (prompt){
cerr << "Enter number count: ";
cin >> number_count;
}
in >> number_count;
Input in;
in.numbers.resize(number_count);
cerr << "Enter numbers: " << endl;
for (size_t i=0; i < number_count; i++)
Input iinn;
iinn.numbers.resize(number_count);
if (prompt){
cerr << "Enter numbers: ";}
for (size_t i = 0; i < number_count; i++)
{
cin >> in.numbers[i];
in >> iinn.numbers[i];
}
cerr << "Enter bin count: ";
cin >> in.bin_count;
return in;
if (prompt){
cerr << "Enter bin count: ";}
in >> iinn.bin_count;
return iinn;
}
void
find_minmax(const vector<double>& numbers, double& min, double& max){
min = numbers[0];
min = numbers[0];
for(size_t i=0; i < numbers.size(); i++){
if (numbers[i] < min)
{
min = numbers[i];
}
else if (numbers[i] > max)
{
max = numbers[i];
}
}
static 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;
}
vector<size_t>
make_histogram(const vector<double>& numbers, size_t bin_count){
double min, max;
find_minmax(numbers, min, max);
vector<size_t> bins(bin_count);
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 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;
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) {
curl_off_t dl;
res = curl_easy_getinfo(curl, CURLINFO_SIZE_DOWNLOAD_T, &dl);
if(!res) {
cerr<<"Downloaded "<<dl<< " bytes\n";
}
}
if (!found)
{
bins[bin_count - 1]++;
curl_easy_cleanup(curl);
}
}
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;
return input_data(buffer, false);
}
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 << " ";
}
}
cout << bins[j] << "|";
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();
auto bins = make_histogram(in.numbers, in.bin_count);
show_histogram_text(bins, in.bin_count);
return 0;
const auto bins = make_histogram(input.numbers, input.bin_count);
show_histogram_svg(bins);
}

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 Обычный файл
Просмотреть файл

@@ -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 Обычный файл
Просмотреть файл

@@ -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 Обычный файл
Просмотреть файл

@@ -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 Обычный файл
Просмотреть файл

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

Просмотреть файл

@@ -14,9 +14,7 @@ TEST_CASE("distinct positive numbers") {
TEST_CASE("empty vector"){
double min = 0;
double max = 0;
find_minmax({ 0, 0 }, min, max);
CHECK(min == 0);
CHECK(max == 0);
CHECK(!find_minmax({}, min, max));
}
TEST_CASE("same elements"){
@@ -24,5 +22,20 @@ TEST_CASE("same elements"){
double max = 0;
find_minmax({ 1, 1 }, 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 Обычный файл
Просмотреть файл

@@ -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");
}