code: заготовка кода

master
Ilya (OgarkovIA) 6 месяцев назад
Сommit a23367aaf1

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

@ -0,0 +1,5 @@
cs-lab34.cbp
cs-lab34.depend
unittest.cbp
/bin
/obj

@ -0,0 +1,10 @@
<?xml version="1.0" encoding="UTF-8" standalone="yes" ?>
<CodeBlocks_layout_file>
<FileVersion major="1" minor="0" />
<ActiveTarget name="Debug" />
<File name="main.cpp" open="1" top="0" tabpos="1" split="0" active="1" splitpos="0" zoom_1="0" zoom_2="0">
<Cursor>
<Cursor1 position="143" topLine="0" />
</Cursor>
</File>
</CodeBlocks_layout_file>

@ -0,0 +1,43 @@
#include "histogram.h"
#include <vector>
void find_minmax(const std::vector <double> &numbers, double &min, double &max, bool &res ){
if (numbers.size()==0){
res = true;
return;
}
min = numbers[0];
max = numbers[0];
for ( double x : numbers ){
if ( x < min ){
min = x;
} else if ( x > max ){
max = x;
}
}
}
std::vector<std::size_t> make_histogram(const std::vector<double> &numbers, std::size_t bin_count) {
double min, max;
bool res = false;
find_minmax(numbers, min, max, res);
double bin_size = ( max - min ) / bin_count;
std::vector<std::size_t> bins ( bin_count );
for (std::size_t i=0; i < numbers.size(); i++ ){
bool found = false;
for (std::size_t j = 0; ( j < bin_count - 1 ) && !found; j++ ){
auto lo = min + j * bin_size;
auto hi = min + ( j + 1 ) * bin_size;
if (lo <= numbers[i] && ( numbers[i] < hi )){
bins[j]++;
found = true;
}
}
if ( !found ){
bins[bin_count-1]++;
}
}
return bins;
}

@ -0,0 +1,9 @@
#ifndef HISTOGRAM_H_INCLUDED
#define HISTOGRAM_H_INCLUDED
#include <vector>
std::vector<size_t>
make_histogram(const std::vector<double>& numbers, size_t bin_count);
#endif // HISTOGRAM_H_INCLUDED

@ -0,0 +1,52 @@
#include <iostream>
#include <vector>
#include "histogram.h"
#include "text.h"
#include "svg.h"
using namespace std;
struct Input {
vector<double> numbers;
size_t bin_count{};
};
Input input_data() {
size_t number_count;
cerr << "Enter number count: ";
cin >> number_count;
Input in;
cerr << "Enter number of bins: ";
cin >> in.bin_count;
in.numbers.resize(number_count);
cerr << "Enter numbers: ";
for (size_t i = 0; i < number_count; i++)
{
cin >> in.numbers[i];
}
return in;
}
int main(){
bool res = false;
Input in = input_data();
auto bins = make_histogram(in.numbers, in.bin_count);
show_histogram_svg(bins);
}

@ -0,0 +1,67 @@
#include <iostream>
#include <vector>
#include "svg.h"
void
svg_begin(double width, double height) {
std::cout << "<?xml version='1.0' encoding='UTF-8'?>\n";
std::cout << "<svg ";
std::cout << "width='" << width << "' ";
std::cout << "height='" << height << "' ";
std::cout << "viewBox='0 0 " << width << " " << height << "' ";
std::cout << "xmlns='http://www.w3.org/2000/svg'>\n";
}
void
svg_end() {
std::cout << "</svg>\n";
}
void
svg_text(double left, double baseline, std::string text) {
std::cout << "<text x='" << left << "' y='"<< baseline << "' > " << text << " </text>";
}
void svg_rect(double x, double y, double width, double height){
std::cout << "<rect x='" << x << "' y='" << y << "' width='" << width << "' height='" << height << " ' stroke='black' fill='#33A220' ></rect>";
}
void
show_histogram_svg(const std::vector<std::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;
const std::size_t MAX_ASTERISK = IMAGE_WIDTH - TEXT_WIDTH;
svg_begin(IMAGE_WIDTH, IMAGE_HEIGHT);
double top = 0;
std::size_t maxbin=bins[0];
for (std::size_t i=0; i < bins.size(); i++){
if (maxbin < bins[i]){
maxbin=bins[i];
}
}
if (maxbin<=76){
for (std::size_t i=0; i < bins.size(); i++) {
double bin_width = BLOCK_WIDTH * bins[i];
svg_text(TEXT_LEFT, top + TEXT_BASELINE, std::to_string(bins[i]));
svg_rect(TEXT_WIDTH, top, bin_width, BIN_HEIGHT);
top += BIN_HEIGHT;
}
svg_end();
} else {
for (std::size_t i=0; i < bins.size(); i++) {
double bin_width= MAX_ASTERISK * (static_cast<double>(bins[i]) / maxbin);
svg_text(TEXT_LEFT, top + TEXT_BASELINE, std::to_string(bins[i]));
svg_rect(TEXT_WIDTH, top, bin_width, BIN_HEIGHT);
top += BIN_HEIGHT;
}
svg_end();
}
}

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

@ -0,0 +1,45 @@
#include <iostream>
#include <vector>
#include "text.h"
const std::size_t SCREEN_WIDTH = 80;
const std::size_t MAX_ASTERISK = SCREEN_WIDTH - 3 - 1;
void show_histogram_text(const std::vector<std::size_t> &bins){
std::size_t maxbin = bins[0];
for (std::size_t i=1; i < bins.size(); i++){
if (maxbin < bins[i]){
maxbin = bins[i];
}
}
if (maxbin <= MAX_ASTERISK){
for (size_t i=0; i < bins.size(); i++ ){
if (bins[i] < 10 ){
std::cout << " " << bins[i] << "|";
} else if (bins[i] < 100) {
std::cout << " " << bins[i] << "|";
} else if (bins[i] < 1000) {
std::cout << bins[i] << "|";
}
for (size_t j=0; j < bins[i]; j++ ){
std::cout << "*";
}
std::cout << std::endl;
}
} else {
for (std::size_t i=0; i < bins.size(); i++){
std::size_t heightG= MAX_ASTERISK * (static_cast<double>(bins[i]) / maxbin);
if (bins[i] < 10 ){
std::cout << " " << bins[i] << "|";
} else if (bins[i] < 100) {
std::cout << " " << bins[i] << "|";
} else if (bins[i] < 1000) {
std::cout << bins[i] << "|";
}
for (std::size_t j=0; j < heightG; j++ ){
std::cout << "*";
}
std::cout << std::endl;
}
}
}

@ -0,0 +1,8 @@
#ifndef TEXT_H_INCLUDED
#define TEXT_H_INCLUDED
#include <vector>
void show_histogram_text(const std::vector <std::size_t> &bins);
#endif // TEXT_H_INCLUDED
Загрузка…
Отмена
Сохранить