code: зеркальный вывод
Этот коммит содержится в:
18
main.cpp
18
main.cpp
@@ -6,20 +6,22 @@
|
|||||||
|
|
||||||
using namespace std;
|
using namespace std;
|
||||||
|
|
||||||
struct Input {
|
struct Input
|
||||||
|
{
|
||||||
vector<double> numbers;
|
vector<double> numbers;
|
||||||
size_t bin_count{};
|
size_t bin_count{};
|
||||||
};
|
};
|
||||||
|
|
||||||
Input
|
Input
|
||||||
input_data() {
|
input_data()
|
||||||
|
{
|
||||||
size_t number_count;
|
size_t number_count;
|
||||||
cerr << "Enter number count: ";
|
cerr << "Enter number count: ";
|
||||||
cin >> number_count;
|
cin >> number_count;
|
||||||
Input in;
|
Input in;
|
||||||
in.numbers.resize(number_count);
|
in.numbers.resize(number_count);
|
||||||
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];
|
cin >> in.numbers[i];
|
||||||
}
|
}
|
||||||
cerr << "Enter bin count: ";
|
cerr << "Enter bin count: ";
|
||||||
@@ -27,9 +29,9 @@ input_data() {
|
|||||||
return in;
|
return in;
|
||||||
}
|
}
|
||||||
|
|
||||||
int
|
int main()
|
||||||
main() {
|
{
|
||||||
auto in = input_data();
|
Input in = input_data();
|
||||||
auto bins = make_histogram(in.numbers, in.bin_count);
|
vector<size_t> bins = make_histogram(in.numbers, in.bin_count);
|
||||||
show_histogram_svg(bins);
|
show_histogram_svg(bins);
|
||||||
}
|
}
|
||||||
|
|||||||
30
svg.cpp
30
svg.cpp
@@ -4,18 +4,15 @@
|
|||||||
#include "svg.h"
|
#include "svg.h"
|
||||||
using namespace std;
|
using namespace std;
|
||||||
|
|
||||||
void
|
void svg_text(double left, double baseline, string text) {
|
||||||
svg_text(double left, double baseline, string text) {
|
|
||||||
cout << "<text x='" << left << "' y='" << baseline << "'>" << text << "</text>";
|
cout << "<text x='" << left << "' y='" << baseline << "'>" << text << "</text>";
|
||||||
}
|
}
|
||||||
|
|
||||||
void
|
void svg_rect(double x, double y, double width, double height) {
|
||||||
svg_rect(double x, double y, double width, double height) {
|
|
||||||
cout << "<rect x='" << x<< "' y='" << y<< "' width='" << width<< "' height='" << height<< "' />\n";
|
cout << "<rect x='" << x<< "' y='" << y<< "' width='" << width<< "' height='" << height<< "' />\n";
|
||||||
}
|
}
|
||||||
|
|
||||||
void
|
void svg_begin(double width, double height) {
|
||||||
svg_begin(double width, double height) {
|
|
||||||
cout << "<?xml version='1.0' encoding='UTF-8'?>\n";
|
cout << "<?xml version='1.0' encoding='UTF-8'?>\n";
|
||||||
cout << "<svg ";
|
cout << "<svg ";
|
||||||
cout << "width='" << width << "' ";
|
cout << "width='" << width << "' ";
|
||||||
@@ -24,28 +21,31 @@ svg_begin(double width, double height) {
|
|||||||
cout << "xmlns='http://www.w3.org/2000/svg'>\n";
|
cout << "xmlns='http://www.w3.org/2000/svg'>\n";
|
||||||
}
|
}
|
||||||
|
|
||||||
void
|
void svg_end() {
|
||||||
svg_end() {
|
|
||||||
cout << "</svg>\n";
|
cout << "</svg>\n";
|
||||||
}
|
}
|
||||||
|
|
||||||
void
|
void show_histogram_svg(const vector<size_t>& bins) {
|
||||||
show_histogram_svg(const vector<size_t>& bins) {
|
|
||||||
const auto IMAGE_WIDTH = 400;
|
const auto IMAGE_WIDTH = 400;
|
||||||
const auto IMAGE_HEIGHT = 300;
|
const auto IMAGE_HEIGHT = 300;
|
||||||
const auto TEXT_LEFT = 20;
|
|
||||||
const auto TEXT_BASELINE = 20;
|
const auto TEXT_BASELINE = 20;
|
||||||
const auto TEXT_WIDTH = 50;
|
|
||||||
const auto BIN_HEIGHT = 30;
|
const auto BIN_HEIGHT = 30;
|
||||||
const auto BLOCK_WIDTH = 10;
|
const auto BLOCK_WIDTH = 10;
|
||||||
svg_begin(400, 300);
|
svg_begin(400, 300);
|
||||||
double top = 0;
|
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) {
|
for (size_t bin : bins) {
|
||||||
const double bin_width = BLOCK_WIDTH * bin;
|
const double bin_width = BLOCK_WIDTH * bin;
|
||||||
svg_text(TEXT_LEFT, top + TEXT_BASELINE, to_string(bin));
|
const double text_left = BLOCK_WIDTH * max_bin;
|
||||||
svg_rect(TEXT_WIDTH, top, bin_width, BIN_HEIGHT);
|
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;
|
top += BIN_HEIGHT;
|
||||||
}
|
}
|
||||||
svg_end();
|
svg_end();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
29
text.cpp
29
text.cpp
@@ -3,8 +3,7 @@
|
|||||||
|
|
||||||
using namespace std;
|
using namespace std;
|
||||||
|
|
||||||
void
|
void show_histogram_text(const vector<size_t>& bins) {
|
||||||
show_histogram_text(const vector<size_t>& bins){
|
|
||||||
const size_t SCREEN_WIDTH = 80;
|
const size_t SCREEN_WIDTH = 80;
|
||||||
const size_t MAX_ASTERISK = SCREEN_WIDTH - 3 - 1;
|
const size_t MAX_ASTERISK = SCREEN_WIDTH - 3 - 1;
|
||||||
|
|
||||||
@@ -20,34 +19,40 @@ show_histogram_text(const vector<size_t>& bins){
|
|||||||
if (max_count <= MAX_ASTERISK) {
|
if (max_count <= MAX_ASTERISK) {
|
||||||
for (size_t i = 0; i < bin_count; i++) {
|
for (size_t i = 0; i < bin_count; i++) {
|
||||||
size_t Count = bins[i];
|
size_t Count = bins[i];
|
||||||
|
for (size_t j = 0; j < max_count - Count; j++){
|
||||||
|
cout << " ";
|
||||||
|
}
|
||||||
|
for (size_t j = 0; j < Count; j++){
|
||||||
|
cout << "*";
|
||||||
|
}
|
||||||
|
cout << "|";
|
||||||
if (Count < 100) {
|
if (Count < 100) {
|
||||||
cout << " ";
|
cout << " ";
|
||||||
}
|
}
|
||||||
if (Count < 10) {
|
if (Count < 10) {
|
||||||
cout << " ";
|
cout << " ";
|
||||||
}
|
}
|
||||||
cout << Count << "|";
|
cout << Count << "\n";
|
||||||
for (size_t j = 0; j < Count; j++){
|
|
||||||
cout << "*";
|
|
||||||
}
|
|
||||||
cout << "\n";
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
else {
|
else {
|
||||||
for (size_t i = 0; i < bin_count; i++) {
|
for (size_t i = 0; i < bin_count; i++) {
|
||||||
size_t Count = bins[i];
|
size_t Count = bins[i];
|
||||||
size_t height = 76 * (static_cast<double>(Count) / max_count);
|
size_t height = 76 * (static_cast<double>(Count) / max_count);
|
||||||
|
for (size_t j = 0; j < max_count - height; j++){
|
||||||
|
cout << " ";
|
||||||
|
}
|
||||||
|
for (size_t j = 0; j < height; j++){
|
||||||
|
cout << "*";
|
||||||
|
}
|
||||||
|
cout << "|";
|
||||||
if (Count < 100) {
|
if (Count < 100) {
|
||||||
cout << " ";
|
cout << " ";
|
||||||
}
|
}
|
||||||
if (Count < 10) {
|
if (Count < 10) {
|
||||||
cout << " ";
|
cout << " ";
|
||||||
}
|
}
|
||||||
cout << Count << "|";
|
cout << Count << "\n";
|
||||||
for (size_t j = 0; j < height; j++){
|
|
||||||
cout << "*";
|
|
||||||
}
|
|
||||||
cout << "\n";
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -35,3 +35,10 @@ TEST_CASE("one number") {
|
|||||||
CHECK(min == 2);
|
CHECK(min == 2);
|
||||||
CHECK(max == 2);
|
CHECK(max == 2);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|||||||
Ссылка в новой задаче
Block a user