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

...

10 Коммитов

2
.gitignore поставляемый

@ -2,3 +2,5 @@
/obj
/lab01.depend
/lab01.layout
/curl

@ -1,34 +1,74 @@
#include <iostream>
#include <vector>
#include <curl/curl.h>
#include <sstream>
#include <string>
#include "histogram.h"
#include "text.h"
#include "svg.h"
using namespace std;
struct Input {
struct Input
{
vector<double> numbers;
size_t bin_count{};
};
Input
input_data() {
Input input_data(istream& in, bool prompt = true)
{
size_t number_count;
cerr << "Enter number count: ";
cin >> number_count;
Input in;
in.numbers.resize(number_count);
cerr << "Enter numbers: ";
for (size_t i = 0; i < number_count; i++) {
cin >> in.numbers[i];
if (prompt)
cerr << "Enter number count: ";
in >> number_count;
Input input;
input.numbers.resize(number_count);
for (size_t i = 0; i < number_count; i++)
{
in >> input.numbers[i];
}
if (prompt)
cerr << "Enter bin count: ";
in >> input.bin_count;
return input;
}
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;
}
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){
cerr << curl_easy_strerror(res);
exit(1);
}
curl_easy_cleanup(curl);
}
cerr << "Enter bin count: ";
cin >> in.bin_count;
return in;
return input_data(buffer, false);
}
int
main() {
auto in = input_data();
auto bins = make_histogram(in.numbers, in.bin_count);
show_histogram_text(bins);
int main(int argc, char* argv[])
{
bool show_prompts;
cout << "Show prompts? (1 for yes, 0 for no): ";
cin >> show_prompts;
Input input;
if (argc > 1) {
input = download(argv[1]);
} else {
input = input_data(cin, true);
}
const auto bins = make_histogram(input.numbers, input.bin_count);
show_histogram_svg(bins);
}

@ -32,7 +32,14 @@
<Add option="-Wall" />
<Add option="-fexceptions" />
</Compiler>
<Unit filename=".gitignore" />
<Unit filename="histogram.cpp" />
<Unit filename="histogram.h" />
<Unit filename="main.cpp" />
<Unit filename="svg.cpp" />
<Unit filename="svg.h" />
<Unit filename="text.cpp" />
<Unit filename="text.h" />
<Extensions>
<lib_finder disable_auto="1" />
</Extensions>

@ -0,0 +1,51 @@
#include <iostream>
#include <vector>
#include <string>
#include "svg.h"
using namespace std;
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) {
cout << "<rect x='" << x<< "' y='" << y<< "' width='" << width<< "' height='" << height<< "' />\n";
}
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) {
const auto IMAGE_WIDTH = 400;
const auto IMAGE_HEIGHT = 300;
const auto TEXT_BASELINE = 20;
const auto BIN_HEIGHT = 30;
const auto BLOCK_WIDTH = 10;
svg_begin(400, 300);
double top = 0;
size_t max_bin = 0;
for (size_t bin : bins) {
if (bin > max_bin) {
max_bin = bin;
}
}
for (size_t bin : bins) {
const double bin_width = BLOCK_WIDTH * bin;
const double text_left = BLOCK_WIDTH * max_bin;
const double rect_left = text_left - bin_width;
svg_rect(rect_left, top, bin_width, BIN_HEIGHT);
svg_text(text_left, top + TEXT_BASELINE, to_string(bin));
top += BIN_HEIGHT;
}
svg_end();
}

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

@ -3,51 +3,56 @@
using namespace std;
void
show_histogram_text(const vector<size_t>& bins){
void show_histogram_text(const vector<size_t>& bins) {
const size_t SCREEN_WIDTH = 80;
const size_t MAX_ASTERISK = SCREEN_WIDTH - 3 - 1;
auto bin_count = bins.size();
size_t max_count = 0;
for (size_t i = 0; i < bin_count; i++){
for (size_t i = 0; i < bin_count; i++) {
size_t Count = bins[i];
if (max_count < Count){
if (max_count < Count) {
max_count = Count;
}
}
if (max_count <= MAX_ASTERISK){
for (size_t i = 0; i < bin_count; i++){
if (max_count <= MAX_ASTERISK) {
for (size_t i = 0; i < bin_count; i++) {
size_t Count = bins[i];
if (Count < 100){
for (size_t j = 0; j < max_count - Count; j++){
cout << " ";
}
if (Count < 10){
cout << " ";
}
cout << Count << "|";
for (size_t j = 0; j < Count; j++){
cout << "*";
}
cout << "\n";
cout << "|";
if (Count < 100) {
cout << " ";
}
if (Count < 10) {
cout << " ";
}
cout << Count << "\n";
}
}
else{
for (size_t i = 0; i < bin_count; i++){
else {
for (size_t i = 0; i < bin_count; i++) {
size_t Count = bins[i];
size_t height = 76 * (static_cast<double>(Count) / max_count);
if (Count < 100){
for (size_t j = 0; j < max_count - height; j++){
cout << " ";
}
if (Count < 10){
cout << " ";
}
cout << Count << "|";
for (size_t j = 0; j < height; j++){
cout << "*";
}
cout << "\n";
cout << "|";
if (Count < 100) {
cout << " ";
}
if (Count < 10) {
cout << " ";
}
cout << Count << "\n";
}
}
}

@ -36,6 +36,8 @@
</Unit>
<Unit filename="histogram.cpp" />
<Unit filename="histogram_internal.h" />
<Unit filename="svg.cpp" />
<Unit filename="svg.h" />
<Unit filename="unittest.cpp" />
<Extensions>
<lib_finder disable_auto="1" />

@ -3,6 +3,8 @@
#include <vector>
#include "doctest.h"
#include "histogram_internal.h"
#include "svg.h"
#include <string>
TEST_CASE("distinct positive numbers") {
double min = 0;
@ -19,15 +21,6 @@ TEST_CASE("negative numbers") {
CHECK(min == -10);
CHECK(max == 10);
}
TEST_CASE("empty numbers") {
double min = 0;
double max = 0;
find_minmax({ }, min, max);
CHECK(min != 0);
CHECK(max != 0);
}
TEST_CASE("one number") {
double min = 0;
double max = 0;
@ -35,3 +28,26 @@ TEST_CASE("one number") {
CHECK(min == 2);
CHECK(max == 2);
}
#include "doctest.h"
#include "svg.h"
#include <sstream>
#include <string>
TEST_CASE("show_histogram_svg") {
std::ostringstream oss;
std::streambuf* p_cout_streambuf = std::cout.rdbuf();
std::cout.rdbuf(oss.rdbuf());
show_histogram_svg({1, 2, 3, 4});
std::cout.rdbuf(p_cout_streambuf);
std::string result = oss.str();
std::string expected = "<?xml version='1.0' encoding='UTF-8'?>"
"<svg width='400' height='300' viewBox='0 0 400 300' xmlns='http://www.w3.org/2000/svg'>"
"<rect x='30' y='0' width='10' height='30' />"
"<text x='40' y='20'>1</text><rect x='20' y='30' width='20' height='30' />"
"<text x='40' y='50'>2</text><rect x='10' y='60' width='30' height='30' />"
"<text x='40' y='80'>3</text><rect x='0' y='90' width='40' height='30' />"
"<text x='40' y='110'>4</text></svg>";
CHECK(result == expected);
}

Загрузка…
Отмена
Сохранить