code: зеркальный вывод

main
cs-lab34 (BushmanovAS) 12 месяцев назад
Родитель a9dbc4eccf
Сommit 712f9245b7

@ -6,20 +6,22 @@
using namespace std;
struct Input {
struct Input
{
vector<double> numbers;
size_t bin_count{};
};
Input
input_data() {
input_data()
{
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++) {
for (size_t i = 0; i < number_count; i++)
{
cin >> in.numbers[i];
}
cerr << "Enter bin count: ";
@ -27,9 +29,9 @@ input_data() {
return in;
}
int
main() {
auto in = input_data();
auto bins = make_histogram(in.numbers, in.bin_count);
int main()
{
Input in = input_data();
vector<size_t> bins = make_histogram(in.numbers, in.bin_count);
show_histogram_svg(bins);
}

@ -4,18 +4,15 @@
#include "svg.h"
using namespace std;
void
svg_text(double left, double baseline, string text) {
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) {
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) {
void svg_begin(double width, double height) {
cout << "<?xml version='1.0' encoding='UTF-8'?>\n";
cout << "<svg ";
cout << "width='" << width << "' ";
@ -24,28 +21,31 @@ svg_begin(double width, double height) {
cout << "xmlns='http://www.w3.org/2000/svg'>\n";
}
void
svg_end() {
void svg_end() {
cout << "</svg>\n";
}
void
show_histogram_svg(const vector<size_t>& bins) {
void show_histogram_svg(const vector<size_t>& bins) {
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(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;
svg_text(TEXT_LEFT, top + TEXT_BASELINE, to_string(bin));
svg_rect(TEXT_WIDTH, top, bin_width, BIN_HEIGHT);
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();
}

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

@ -35,3 +35,10 @@ TEST_CASE("one number") {
CHECK(min == 2);
CHECK(max == 2);
}

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