Кристина Васильева 1 неделю назад
Сommit 4752a5527e

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

@ -0,0 +1,11 @@
bin/
obj/
*.exe
*.svg
*.o
*.d
Debug/
Relause/
*cbp.layout
*.depend
build/

Разница между файлами не показана из-за своего большого размера Загрузить разницу

@ -0,0 +1,51 @@
#include <vector>
#include <algorithm>
#include "histogram.h"
#include "histogram_internal.h"
#include "histogram_internal.h"
using namespace std;
void find_minmax(const vector<double>& numbers, double& min, double& max) {
if (numbers.empty()) {
return;
}
min = numbers[0];
max = numbers[0];
for (double number : numbers) {
if (number < min) {
min = number;
}
if (number > max) {
max = number;
}
}
}
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 (double number : numbers) {
bool found = false;
for (size_t i = 0; i < bin_count - 1; i++) {
auto lo = min + i * bin_size;
auto hi = min + (i + 1) * bin_size;
if (lo <= number && number < hi) {
bins[i]++;
found = true;
break;
}
}
if (!found) {
bins[bin_count - 1]++;
}
}
return bins;
}

@ -0,0 +1,13 @@
#ifndef HISTOGRAM_H
#define HISTOGRAM_H
#include <vector>
struct Input {
std::vector<double> numbers;
size_t bin_count{};
};
std::vector<size_t> make_histogram(const std::vector<double>& numbers, size_t bin_count);
#endif

@ -0,0 +1,8 @@
#ifndef HISTOGRAM_INTERNAL_H
#define HISTOGRAM_INTERNAL_H
#include <vector>
void find_minmax(const std::vector<double>& numbers, double& min, double& max);
#endif

@ -0,0 +1,40 @@
<?xml version="1.0" encoding="UTF-8" standalone="yes" ?>
<CodeBlocks_project_file>
<FileVersion major="1" minor="6" />
<Project>
<Option title="lab03" />
<Option pch_mode="2" />
<Option compiler="gcc" />
<Build>
<Target title="Debug">
<Option output="bin/Debug/lab03" prefix_auto="1" extension_auto="1" />
<Option object_output="obj/Debug/" />
<Option type="1" />
<Option compiler="gcc" />
<Compiler>
<Add option="-g" />
</Compiler>
</Target>
<Target title="Release">
<Option output="bin/Release/lab03" prefix_auto="1" extension_auto="1" />
<Option object_output="obj/Release/" />
<Option type="1" />
<Option compiler="gcc" />
<Compiler>
<Add option="-O2" />
</Compiler>
<Linker>
<Add option="-s" />
</Linker>
</Target>
</Build>
<Compiler>
<Add option="-Wall" />
<Add option="-fexceptions" />
</Compiler>
<Unit filename="main.cpp" />
<Extensions>
<lib_finder disable_auto="1" />
</Extensions>
</Project>
</CodeBlocks_project_file>

@ -0,0 +1,39 @@
#include <iostream>
#include <vector>
#include "histogram.h"
#include "text.h"
using namespace std;
Input input_data();
int main_text() { // èëè int main() åñëè ýòî àêòèâíàÿ ïðîãðàììà
auto input = input_data();
auto bins = make_histogram(input.numbers, input.bin_count);
show_histogram_text(bins);
cout << "Press Enter to exit...";
cin.ignore();
cin.get();
return 0;
}
Input input_data() {
Input in;
size_t number_count = 0;
cout << "Enter number count: ";
cin >> number_count;
cout << "Enter numbers: ";
in.numbers.resize(number_count);
for (size_t i = 0; i < number_count; i++) {
cin >> in.numbers[i];
}
cout << "Enter bin count: ";
cin >> in.bin_count;
return in;
}

@ -0,0 +1,29 @@
#include <iostream>
#include <vector>
#include "histogram.h"
#include "svg.h"
using namespace std;
Input input_data();
int main() {
auto input = input_data();
auto bins = make_histogram(input.numbers, input.bin_count);
show_histogram_svg(bins);
return 0;
}
Input input_data() {
Input in;
size_t number_count = 0;
cin >> number_count;
in.numbers.resize(number_count);
for (size_t i = 0; i < number_count; i++) {
cin >> in.numbers[i];
}
cin >> in.bin_count;
return in;
}

@ -0,0 +1,97 @@
#include <iostream>
#include <vector>
#include <string>
#include <algorithm>
#include "svg.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 svg_text(double left, double baseline, const string& text) {
cout << "<text x='" << left << "' y='" << baseline << "' "
<< "font-family='Arial' font-size='12' "
<< "text-anchor='end' dominant-baseline='middle'>"
<< text << "</text>\n";
}
void svg_rect(double x, double y, double width, double height,
string stroke, string fill) {
cout << "<rect x='" << x << "' y='" << y << "' width='" << width
<< "' height='" << height << "' stroke='" << stroke
<< "' fill='" << fill << "' stroke-width='1' />\n";
}
void show_histogram_svg(const vector<size_t>& bins) {
const auto IMAGE_WIDTH = 400;
const auto TEXT_LEFT = 20;
const auto TEXT_BASELINE = 15;
const auto TEXT_WIDTH = 50;
const auto BIN_HEIGHT = 30;
const auto MARGIN = 10;
// Àâòîìàòè÷åñêè ðàññ÷èòûâàåì âûñîòó èçîáðàæåíèÿ
const auto IMAGE_HEIGHT = bins.size() * BIN_HEIGHT + 2 * MARGIN;
svg_begin(IMAGE_WIDTH, IMAGE_HEIGHT);
// Íàõîäèì ìàêñèìàëüíîå çíà÷åíèå äëÿ ìàñøòàáèðîâàíèÿ
size_t max_count = 0;
for (size_t count : bins) {
if (count > max_count) {
max_count = count;
}
}
// Ìàñøòàáèðóåì øèðèíó ñòîëáöîâ
const double max_width = IMAGE_WIDTH - TEXT_WIDTH - MARGIN;
double top = MARGIN;
for (size_t i = 0; i < bins.size(); i++) {
size_t bin = bins[i];
// Ìàñøòàáèðóåì øèðèíó ñòîëáöà
double bin_width = 0;
if (max_count > 0) {
bin_width = (static_cast<double>(bin) / max_count) * max_width;
}
// Âûâîäèì ïîäïèñü
svg_text(TEXT_LEFT, top + BIN_HEIGHT / 2, to_string(bin));
// Âûáèðàåì öâåò â çàâèñèìîñòè îò çíà÷åíèÿ
string fill_color;
if (max_count > 0) {
double ratio = static_cast<double>(bin) / max_count;
if (ratio > 0.8) {
fill_color = "#ff4444";
} else if (ratio > 0.5) {
fill_color = "#ffaa44";
} else if (ratio > 0.2) {
fill_color = "#44ff44";
} else {
fill_color = "#4444ff";
}
} else {
fill_color = "#888888";
}
// Âûâîäèì ñòîëáåö
svg_rect(TEXT_WIDTH, top, bin_width, BIN_HEIGHT, "#333333", fill_color);
top += BIN_HEIGHT;
}
svg_end();
}

14
svg.h

@ -0,0 +1,14 @@
#ifndef SVG_H
#define SVG_H
#include <vector>
#include <string>
void svg_begin(double width, double height);
void svg_end();
void svg_text(double left, double baseline, const std::string& text);
void svg_rect(double x, double y, double width, double height,
std::string stroke = "black", std::string fill = "black");
void show_histogram_svg(const std::vector<size_t>& bins);
#endif

@ -0,0 +1,33 @@
#include <iostream>
#include <vector>
#include "text.h"
using namespace std;
void show_histogram_text(const vector<size_t>& bins) {
const size_t screen_width = 80;
const size_t max_asterisk = screen_width - 4;
size_t max_count = 0;
for (size_t count : bins) {
if (count > max_count) {
max_count = count;
}
}
for (size_t bin : bins) {
if (bin < 100) {
cout << ' ';
}
if (bin < 10) {
cout << ' ';
}
cout << bin << "|";
size_t height = max_asterisk * (static_cast<double>(bin) / max_count);
for (size_t i = 0; i < height; i++) {
cout << '*';
}
cout << '\n';
}
}

@ -0,0 +1,8 @@
#ifndef TEXT_H
#define TEXT_H
#include <vector>
void show_histogram_text(const std::vector<size_t>& bins);
#endif

@ -0,0 +1,50 @@
#define RUN_TESTS
#include <iostream>
#include <vector>
#include <cassert>
#include "histogram_internal.h"
using namespace std;
void test_find_minmax() {
cout << "Testing find_minmax..." << endl;
// Test 1: distinct positive numbers
double min = 0, max = 0;
find_minmax({1, 2}, min, max);
assert(min == 1);
assert(max == 2);
cout << "Test 1 passed" << endl;
// Test 2: single element
min = 0; max = 0;
find_minmax({5}, min, max);
assert(min == 5);
assert(max == 5);
cout << "Test 2 passed" << endl;
// Test 3: negative numbers
min = 0; max = 0;
find_minmax({-1, -2, -3}, min, max);
assert(min == -3);
assert(max == -1);
cout << "Test 3 passed" << endl;
// Test 4: identical numbers
min = 0; max = 0;
find_minmax({4, 4, 4}, min, max);
assert(min == 4);
assert(max == 4);
cout << "Test 4 passed" << endl;
// Test 5: empty vector
min = 0; max = 0;
find_minmax({}, min, max);
cout << "Test 5 passed (should not crash)" << endl;
cout << "All tests passed!" << endl;
}
int test_main() {
test_find_minmax();
return 0;
}
Загрузка…
Отмена
Сохранить