Code : недоделанный свой вариант

main
BaranovEK 11 месяцев назад
Родитель 15dba8705d
Сommit 29099c717e

@ -1,5 +1,5 @@
# depslib dependency file v1.0
1716798554 source:c:\users\lenov\desktop\lab03\histogram.cpp
1716799438 source:c:\users\lenov\desktop\lab03\histogram.cpp
"histogram.h"
<vector>
<iostream>
@ -7,12 +7,27 @@
1716797551 c:\users\lenov\desktop\lab03\histogram.h
<vector>
1716799012 source:c:\users\lenov\desktop\lab03\main.cpp
1716817439 source:c:\users\lenov\desktop\lab03\main.cpp
<iostream>
<vector>
"histogram.h"
"text.h"
"svg.h"
1716798974 c:\users\lenov\desktop\lab03\text.h
<vector>
1716799035 source:c:\users\lenov\desktop\lab03\text.cpp
<iostream>
"text.h"
<vector>
1716817390 c:\users\lenov\desktop\lab03\svg.h
<vector>
1716817410 source:c:\users\lenov\desktop\lab03\svg.cpp
<iostream>
<vector>
<string>
"svg.h"

@ -2,12 +2,13 @@
#include <vector>
#include "histogram.h"
#include "text.h"
#include "svg.h"
using namespace std;
struct Input {
vector<double> numbers;
size_t bin_count{};
double COLUMN_HIGHT;
};
Input
input_data() {
@ -22,6 +23,8 @@ input_data() {
}
cerr << "Enter bin count: ";
cin>> in.bin_count;
cerr << "Enter column hight: ";
cin>> in.COLUMN_HIGHT;
return in;
}
@ -111,7 +114,8 @@ int
main() {
auto in = input_data();
auto bins = make_histogram(in.numbers, in.bin_count);
show_histogram_text(bins,in.bin_count );
// show_histogram_svg(bins);
COLUMN_hight_function(in.COLUMN_HIGHT,in.bin_count,bins);
/*int main()
{

@ -0,0 +1,3 @@
10
3 3 4 4 4 4 4 5 5 5
3

@ -0,0 +1,104 @@
#include <iostream>
#include <vector>
#include <string>
#include "svg.h"
using namespace std;
const auto IMAGE_WIDTH = 400;
const auto IMAGE_HEIGHT = 700;
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;
const auto MAX_WIDTH = IMAGE_WIDTH - TEXT_WIDTH;
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 stroke = "black", string fill = "aaaaaa") {
cout << "<rect x='" << x<< "' y='" << y<< "' width='" << width<< "' height='" << height<< "' stroke='" << stroke<< "' fill='" << fill << "' />\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 MAX_WIDTH = IMAGE_WIDTH - TEXT_WIDTH;
size_t max_count = 0;
for (size_t x : bins) {
if (x > max_count) {
max_count = x;
}
}
if (max_count == 0) {
max_count = 1;
}
auto scale_factor = static_cast<double>(MAX_WIDTH) / (max_count * BLOCK_WIDTH);
if (scale_factor > 1) {
scale_factor = 1;
}
svg_begin(400, 300);
double top = 0;
for (size_t bin : bins) {
double bin_width = BLOCK_WIDTH * bin * scale_factor;
svg_text(TEXT_LEFT, top + TEXT_BASELINE, to_string(bin));
svg_rect(TEXT_WIDTH, top, bin_width, BIN_HEIGHT);
top += BIN_HEIGHT;
}
svg_end();
}
void
svg_rect_hight(double x, double y, double width, double height, string stroke = "black", string fill = "hfhfh") {
cout << "<rect x='" << x<< "' y='" << y<< "' width='" << width<< "' height='" << height<< "' stroke='" << stroke<< "' fill='" << fill << "' />\n";
}
void
COLUMN_hight_function( double &COLUMN_height ,size_t bin_count,const vector<size_t>& bins){
const auto MAX_WIDTH = IMAGE_WIDTH - TEXT_WIDTH;
size_t max_count = 0;
for (size_t x : bins) {
if (x > max_count) {
max_count = x;
}
}
if (max_count == 0) {
max_count = 1;
}
auto scale_factor = static_cast<double>(MAX_WIDTH) / (max_count * BLOCK_WIDTH);
if (scale_factor > 1) {
scale_factor = 1;
}
svg_begin(IMAGE_WIDTH, IMAGE_HEIGHT );
if ((bin_count*COLUMN_height) > IMAGE_HEIGHT ){
COLUMN_height= static_cast<double>(IMAGE_HEIGHT) / bin_count;
}
double top = 0;
for (size_t bin : bins) {
double bin_width = BLOCK_WIDTH * bin * scale_factor;
svg_text(TEXT_LEFT, top + TEXT_BASELINE, to_string(bin));
svg_rect(TEXT_WIDTH, top, bin_width, COLUMN_height);
top += COLUMN_height;
}
svg_end();
}

10
svg.h

@ -0,0 +1,10 @@
#ifndef SVG_H_INCLUDED
#define SVG_H_INCLUDED
#include <vector>
void
show_histogram_svg(const std::vector<size_t>& bins);
void
COLUMN_hight_function( double &COLUMN_height ,size_t bin_count,const std::vector<size_t>& bins);
#endif // SVG_H_INCLUDED

@ -0,0 +1,46 @@
#define DOCTEST_CONFIG_NO_MULTITHREADING
#define DOCTEST_CONFIG_IMPLEMENT_WITH_MAIN
#include "doctest.h"
#include "histogram_internal.h"
#include "svg.h"
TEST_CASE("distinct positive numbers") {
double min = 0;
double max = 0;
find_minmax({1, 2}, min, max);
CHECK(min == 1);
CHECK(max == 2);
}
TEST_CASE("distinct negative numbers") {
double min = -1;
double max = -1;
find_minmax({-1, -2}, min, max);
CHECK(min == -2);
CHECK(max == -1);
}
TEST_CASE("one number") {
double min = 0;
double max = 0;
find_minmax({1}, min, max);
CHECK(min == 1);
CHECK(max == 1);
}
TEST_CASE("same numbers") {
double min = 0;
double max = 0;
find_minmax({2, 2}, min, max);
CHECK(min == 2);
CHECK(max == 2);
}
TEST_CASE("distinct positive numbers") {
double COLUMN_height = 400;
COLUMN_hight_function(COLUMN_height ,5,{23,5,54,6,7});
CHECK(COLUMN_height == 150 );
}

@ -0,0 +1,71 @@
# depslib dependency file v1.0
1716799438 source:c:\users\lenov\desktop\lab03\histogram.cpp
"histogram.h"
<vector>
<iostream>
1716797551 c:\users\lenov\desktop\lab03\histogram.h
<vector>
1716819750 source:c:\users\lenov\desktop\lab03\unittest.cpp
"doctest.h"
"histogram_internal.h"
"svg.h"
1716800315 c:\users\lenov\desktop\lab03\doctest.h
<signal.h>
<ciso646>
<cstddef>
<ostream>
<istream>
<type_traits>
"doctest_fwd.h"
<ctime>
<cmath>
<climits>
<math.h>
<new>
<cstdio>
<cstdlib>
<cstring>
<limits>
<utility>
<fstream>
<sstream>
<iostream>
<algorithm>
<iomanip>
<vector>
<atomic>
<mutex>
<set>
<map>
<unordered_set>
<exception>
<stdexcept>
<csignal>
<cfloat>
<cctype>
<cstdint>
<string>
<sys/types.h>
<unistd.h>
<sys/sysctl.h>
<AfxWin.h>
<windows.h>
<io.h>
<sys/time.h>
<unistd.h>
1716799551 c:\users\lenov\desktop\lab03\histogram_internal.h
<vector>
1716819750 source:c:\users\lenov\desktop\lab03\svg.cpp
<iostream>
<vector>
<string>
"svg.h"
1716819918 c:\users\lenov\desktop\lab03\svg.h
<vector>
Загрузка…
Отмена
Сохранить