code: cin изменен на in, возврат к дозащитному варианту

main
EvdochenkoNV 3 недель назад
Родитель 5f4d64fd36
Сommit 4eb9fb549b

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

@ -4,3 +4,4 @@
/cs-lab34.layout /cs-lab34.layout
/main.exe /main.exe
/main.o /main.o
/unittest.layout

@ -18,13 +18,6 @@ bool find_minmax(vector<double> vec, double& min, double& max) {
} }
return true; return true;
} }
bool width(size_t block_width){
if (block_width > 3 && block_width<30 ) {
return true;
}
}
vector<size_t> make_histogram(size_t number, vector<double> vec) { vector<size_t> make_histogram(size_t number, vector<double> vec) {
vector<size_t> bins(number); vector<size_t> bins(number);
double mn, mx; double mn, mx;

@ -3,5 +3,3 @@
using namespace std; using namespace std;
vector<size_t> make_histogram(size_t number, vector<double> vec); vector<size_t> make_histogram(size_t number, vector<double> vec);
bool width(size_t block_width);

@ -1,2 +1,2 @@
bool find_minmax(std::vector<double> vec, double& min, double& max); bool find_minmax(std::vector<double> vec, double& min, double& max);
bool width(size_t block_width); //

@ -5,41 +5,24 @@
struct Input { struct Input {
vector<double> vec; vector<double> vec;
size_t korz{}; size_t korz{};
size_t block_width{};
}; };
Input input_data() { Input input_data(istream& in) {
Input in; Input lin;
size_t n, korz; size_t n, korz;
cerr << "Number of elements: "; cerr << "Number of elements: ";
cin >> n; in >> n;
in.vec.resize(n); lin.vec.resize(n);
cerr << "Elements: ";
for (size_t i = 0; i < n; i++) for (size_t i = 0; i < n; i++)
cin >> in.vec[i]; in >> lin.vec[i];
cerr << "Enter bin count: "; cerr << "Enter bin count: ";
cin >> in.korz; in >> lin.korz;
return lin;
cerr << "Enter block width in px (3-30): ";
cin >> in.block_width;
while (in.block_width < 3 || in.block_width > 30) {
if (in.block_width < 3) {
cerr << "Block width too small! Minimum is 3px. Please enter again: ";
} else {
cerr << "Block width too large! Maximum is 30px. Please enter again: ";
}
cin >> in.block_width;
}
return in;
} }
int main() { int main() {
auto in = input_data(); auto in = input_data(cin);
auto bins = make_histogram(in.korz, in.vec); auto bins = make_histogram(in.korz, in.vec);
show_histogram_svg(bins, in.block_width); show_histogram_svg(bins);
} }

@ -1,8 +1,10 @@
#include "svg.h" #include "svg.h"
using namespace std;
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 << "' ";
@ -11,46 +13,59 @@ void 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 svg_end() { void
svg_end()
{
cout << "</svg>\n"; cout << "</svg>\n";
} }
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>"; cout << "<text x='" << left << "' y='" << baseline << "'>" << text << "</text>";
} }
void svg_rect(double x, double y, double width, double height, string stroke, string fill) { void
svg_rect(double x, double y, double width, double height, string stroke = "black", string fill = "black")
{
cout << "<rect x='" << x << "' y='" << y << "' width='" << width << "' height='" << height << "' stroke='" << stroke << "' fill='" << fill << "' />"; cout << "<rect x='" << x << "' y='" << y << "' width='" << width << "' height='" << height << "' stroke='" << stroke << "' fill='" << fill << "' />";
} }
void show_histogram_svg(const vector<size_t>& bins, size_t block_width) {
void
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_LEFT = 20;
const auto TEXT_BASELINE = 20; const auto TEXT_BASELINE = 20;
const auto TEXT_WIDTH = 50; const auto TEXT_WIDTH = 50;
const auto BIN_HEIGHT = 30; const auto BIN_HEIGHT = 30;
const auto BLACK = "black"; const auto BLOCK_WIDTH = 10;
const auto GREEN = "green";
const auto RED = "red"; const auto RED = "red";
const auto MAX_WIDTH = IMAGE_WIDTH - TEXT_WIDTH; const auto MAX_WIDTH = IMAGE_WIDTH - TEXT_WIDTH;
svg_begin(IMAGE_WIDTH, IMAGE_HEIGHT);
svg_begin(IMAGE_WIDTH,IMAGE_HEIGHT);
double top = 0; double top = 0;
double max_count = bins.empty() ? 0 : bins[0]; double max_count = bins[0];
for (size_t i = 1; i < bins.size(); i++) { for (size_t i = 0; i < bins.size(); i++)
if (bins[i] > max_count) { {
if (max_count < bins[i])
{
max_count = bins[i]; max_count = bins[i];
} }
} }
for (size_t bin : bins) { for (size_t bin : bins)
double bin_width = static_cast<double>(block_width) * bin; {
if (bin_width > MAX_WIDTH) { double bin_width = (MAX_WIDTH) * (bin/max_count);
bin_width = MAX_WIDTH;
}
svg_text(TEXT_LEFT, top + TEXT_BASELINE, to_string(bin)); svg_text(TEXT_LEFT, top + TEXT_BASELINE, to_string(bin));
svg_rect(TEXT_WIDTH, top, bin_width, BIN_HEIGHT, BLACK, RED); svg_rect(TEXT_WIDTH, top, bin_width, BIN_HEIGHT, GREEN, RED);
top += BIN_HEIGHT; top += BIN_HEIGHT;
} }

@ -2,6 +2,6 @@
#include <vector> #include <vector>
#include <string> #include <string>
#include <math.h> #include <math.h>
using namespace std;
void show_histogram_svg(const vector<size_t>& bins, size_t block_width); using namespace std;
void show_histogram_svg(const vector<size_t>& bins);

@ -2,4 +2,3 @@
#include <vector> #include <vector>
void show_histogram(std::vector<size_t> bins); void show_histogram(std::vector<size_t> bins);

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