code: Конечный общий вариант

main
Evgeny 1 год назад
Родитель 0aa341dc28
Сommit 48471c5828

@ -0,0 +1,86 @@
/*#include "histogram.h"
void
find_minmax(const std::vector<double>& numbers, double& min, double& max) {
min = numbers[0];
max = numbers[0];
for (double x : numbers){
if (x < min){
min = x;
}
else if (x > max){
max = x;
}
}
}
std::vector<size_t> make_histogram(const std::vector<double>& numbers, size_t& bin_count){
std::vector<size_t> bins(bin_count);
double max, min = 0;
find_minmax(numbers, min, max);
double bin_size = (max - min) / bin_count;
for (size_t i = 0; i < numbers.size(); i++) {
bool found = false;
for (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;
}
*/
#include "histogram.h"
void
find_minmax(const std::vector<double>& numbers, double& min, double& max) {
min = numbers[0];
max = numbers[0];
for (double x : numbers)
{
if (x < min){
min = x;
}
else if (x > max){
max = x;
}
}
}
std::vector <size_t>
make_histogram(const std::vector<double>& numbers, size_t & bin_count, size_t & number_count, size_t & max_count) {
double min, max;
find_minmax(numbers, min, max);
double bin_size = (max - min) / bin_count;
std::vector<size_t> bins(bin_count);
max_count = bins[0];
for (size_t i = 0; i < number_count; i++){
bool found = false;
for (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 (bins[j] > max_count){
max_count = bins[j];
}
}
if (!found){
bins[bin_count - 1]++;
}
if (bins[bin_count - 1] > max_count){
max_count = bins[bin_count - 1];
}
}
return bins;
}

@ -0,0 +1,12 @@
/*#pragma once
#include <iostream>
#include <vector>
std::vector<size_t>
make_histogram(const std::vector<double>& numbers, size_t & bin_count);*/
#pragma once
#include <iostream>
#include <vector>
std::vector<size_t>
make_histogram(const std::vector<double>& numbers, size_t & bin_count, size_t & number_count, size_t & max_count);

@ -0,0 +1,12 @@
/*#pragma once
#include <iostream>
#include <vector>
void
find_minmax(const std::vector<double>& numbers, double& min, double& max);*/
#pragma once
#include <iostream>
#include <vector>
void
find_minmax(const std::vector<double>& numbers, double& min, double& max);

@ -1,5 +1,9 @@
#include <iostream>
/*#include <iostream>
#include <vector>
#include "histogram.h"
#include "text.h"
#include "histogram_internal.h"
#include "svg.h"
using namespace std;
@ -14,7 +18,7 @@ Input
input_data() {
size_t number_count;
cerr<<"Enter number count";
cin >> number_count;
cin>>number_count;
Input in;
in.numbers.resize(number_count);
vector<double> numbers(number_count);
@ -27,79 +31,51 @@ cerr<<"Enter bin count";
cin >> in.bin_count;
return in;
}
void
find_minmax(const vector<double>& numbers, double& min, double& max) {
min = numbers[0];
max = numbers[0];
for (double x : numbers){
if (x < min){
min = x;
}
else if (x > max){
max = x;
}
}
}
vector<size_t> make_histogram(const vector<double> numbers, size_t bin_count){
vector<size_t> bins(bin_count);
double max, min = 0;
find_minmax(numbers, min, max);
double bin_size = (max - min) / bin_count;
int main()
{
auto in = input_data();
auto bins = make_histogram(in.numbers, in.bin_count);
show_histogram_svg(bins, in.max_count, in.bin_count);
return 0;
}*/
#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{};
size_t number_count{};
size_t max_count{};
};
Input
input_data() {
Input in;
cerr << "Enter number count: ";
cin >> in.number_count;
for (size_t i = 0; i < numbers.size(); i++) {
bool found = false;
for (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]++;
}
vector<double> numbers(in.number_count);
in.numbers.resize(in.number_count);
for (size_t i = 0; i < in.number_count; i++) {
cin >> in.numbers[i];
}
return bins;
}
void
show_histogram_text(vector<size_t>& bins, size_t & max_count,size_t & bin_count){
const size_t SCREEN_WIDTH = 80;
const size_t MAX_ASTERISK = SCREEN_WIDTH - 3 - 1;
if (max_count > MAX_ASTERISK){
vector<size_t> hights(bin_count);
for (size_t i = 0; i < bin_count; i++){
size_t height = MAX_ASTERISK * (static_cast<double>(bins[i]) / max_count);
hights[i] = height;
}
for (size_t i = 0; i < bin_count; i++){
printf("%3d|", bins[i]);
for (size_t j = 0; j < hights[i]; j++){
cout<<"*";
}
cout << endl;
}
}
else{
for (size_t i = 0; i < bin_count; i++)
{
printf("%3d|", bins[i]);
for (size_t j = 0; j < bins[i]; j++){
cout<<"*";
}
cout << endl;
}
}
size_t bin_count;
cerr << "Enter bin count: ";
cin >> in.bin_count;
size_t max_count;
in.max_count = 0;
return in;
}
int main()
{
auto in = input_data();
auto bins = make_histogram(in.numbers, in.bin_count);
show_histogram_text(bins, in.max_count, in.bin_count);
auto bins = make_histogram(in.numbers, in.bin_count, in.number_count, in.max_count);
show_histogram_svg(bins, in.max_count, in.bin_count);
return 0;
}

@ -0,0 +1,49 @@
#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_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::string stroke = "black", std::string fil = "black"){
std::cout<< "<rect x='"<< x <<"' y='"<< y <<"' width='"<< width <<"' height='"<< height <<"' />";
}
void
show_histogram_svg(std::vector<size_t>& bins, size_t & max_count,size_t & bin_count) {
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 = (IMAGE_WIDTH - TEXT_WIDTH)/max_count;
svg_begin(IMAGE_WIDTH, IMAGE_HEIGHT);
double top = 0;
for (size_t bin : bins) {
const double bin_width = BLOCK_WIDTH * bin;
svg_text(TEXT_LEFT, top + TEXT_BASELINE, std::to_string(bin));
svg_rect(TEXT_WIDTH, top, bin_width, BIN_HEIGHT);
top += BIN_HEIGHT;
}
svg_end();
}
void
svg_end() {
std::cout << "</svg>\n";
}

29
svg.h

@ -0,0 +1,29 @@
/*#pragma once
#include <iostream>
#include <vector>
void
show_histogram_svg(std::vector<size_t>& bins, size_t & max_count,size_t & bin_count);
void
svg_begin(double width, double height);
void
svg_text(double left, double baseline, std::string text);
void
svg_rect(double x, double y, double width, double height,std::string stroke, std::string fil);
void
svg_end();*/
#pragma once
#include <iostream>
#include <vector>
//#include <string>
void
show_histogram_svg(std::vector<size_t>& bins, size_t & max_count,size_t & bin_count);
void
svg_begin(double width, double height);
void
svg_text(double left, double baseline, std::string text);
void
svg_rect(double x, double y, double width, double height, double fill_opacity, std::string stroke, std::string fil);
void
svg_end();

@ -0,0 +1,67 @@
/*#include "histogram.h"
void
show_histogram_text(std::vector<size_t>& bins, size_t & max_count,size_t & bin_count){
const size_t SCREEN_WIDTH = 80;
const size_t MAX_ASTERISK = SCREEN_WIDTH - 3 - 1;
if (max_count > MAX_ASTERISK){
std::vector<size_t> hights(bin_count);
for (size_t i = 0; i < bin_count; i++){
size_t height = MAX_ASTERISK * (static_cast<double>(bins[i]) / max_count);
hights[i] = height;
}
for (size_t i = 0; i < bin_count; i++){
printf("%3d|", bins[i]);
for (size_t j = 0; j < hights[i]; j++){
std::cout<<"*";
}
std::cout << std::endl;
}
}
else{
for (size_t i = 0; i < bin_count; i++)
{
printf("%3d|", bins[i]);
for (size_t j = 0; j < bins[i]; j++){
std::cout<<"*";
}
std::cout << std::endl;
}
}
}
*/
#include "histogram.h"
void
show_histogram_text(std::vector<size_t>& bins, size_t & max_count,size_t & bin_count){
const size_t SCREEN_WIDTH = 80;
const size_t MAX_ASTERISK = SCREEN_WIDTH - 3 - 1;
if (max_count > MAX_ASTERISK){
std::vector<size_t> hights(bin_count);
for (size_t i = 0; i < bin_count; i++){
size_t height = MAX_ASTERISK * (static_cast<double>(bins[i]) / max_count);
hights[i] = height;
}
for (size_t i = 0; i < bin_count; i++){
printf("%3d|", bins[i]);
for (size_t j = 0; j < hights[i]; j++){
std::cout<<"*";
}
std::cout << std::endl;
}
}
else{
for (size_t i = 0; i < bin_count; i++)
{
printf("%3d|", bins[i]);
for (size_t j = 0; j < bins[i]; j++){
std::cout<<"*";
}
std::cout << std::endl;
}
}
}

@ -0,0 +1,12 @@
/*#pragma once
#include <iostream>
#include <vector>
void
show_histogram_text(std::vector<size_t>& bins, size_t & max_count,size_t & bin_count);*/
#pragma once
#include <iostream>
#include <vector>
void
show_histogram_text(std::vector<size_t>& bins, size_t & max_count,size_t & bin_count);
Загрузка…
Отмена
Сохранить