VolodinDV 12 месяцев назад
Родитель a68a044912
Сommit 80edb8c280

Разница между файлами не показана из-за своего большого размера Загрузить разницу

@ -0,0 +1,44 @@
#include "histogram.h"
#include <vector>
#include <iostream>
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 <int> make_histogram( std::vector<double> &numbers, 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<int> 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<int>
make_histogram(const std::vector<double> &numbers, std::size_t &bin_count);
#endif // HISTOGRAM_H_INCLUDED

@ -0,0 +1,8 @@
#ifndef HISTOGRAM_INTERNAL_H_INCLUDED
#define HISTOGRAM_INTERNAL_H_INCLUDED
#include <vector>
void find_minmax(const std::vector <double> &numbers, double &min, double &max,bool &res);
#endif // HISTOGRAM_INTERNAL_H_INCLUDED

@ -17,7 +17,6 @@
<Linker> <Linker>
<Add option="-static-libstdc++" /> <Add option="-static-libstdc++" />
<Add option="-static-libgcc" /> <Add option="-static-libgcc" />
<Add option="-static" />
</Linker> </Linker>
</Target> </Target>
<Target title="Release"> <Target title="Release">
@ -37,7 +36,14 @@
<Add option="-Wall" /> <Add option="-Wall" />
<Add option="-fexceptions" /> <Add option="-fexceptions" />
</Compiler> </Compiler>
<Unit filename="doctest.h" />
<Unit filename="histogram.cpp" />
<Unit filename="histogram.h" />
<Unit filename="main.cpp" /> <Unit filename="main.cpp" />
<Unit filename="svg.cpp" />
<Unit filename="svg.h" />
<Unit filename="text.cpp" />
<Unit filename="text.h" />
<Extensions> <Extensions>
<lib_finder disable_auto="1" /> <lib_finder disable_auto="1" />
</Extensions> </Extensions>

@ -1,81 +1,52 @@
#include <iostream> #include <iostream>
#include <vector> #include <vector>
#include <cmath> #include "histogram.h"
#include "text.h"
#include "svg.h"
using namespace std; using namespace std;
int main(){
const size_t SCREEN_WIDTH = 80;
const size_t MAX_ASTERISK = SCREEN_WIDTH - 4; struct Input {
size_t n,h; vector<double> numbers;
cerr<<"Enter number count "; size_t bin_count{};
cin>>n; };
cerr<<"Enter bin count ";
cin>>h; Input input_data() {
vector <double> num(n);
vector <size_t> bins(h); size_t number_count;
cerr<<"Enter massive elements ";
for (size_t i=0;i<n;i++){ cerr << "Enter number count: ";
cin>> num[i]; cin >> number_count;
}
double minn = num[0]; Input in;
double maxx = num[0];
for (double x : num) { cerr << "Enter number of bins: ";
if (x < minn) { cin >> in.bin_count;
minn = x;
} in.numbers.resize(number_count);
else if (x > maxx) {
maxx = x; cerr << "Enter numbers: ";
} for (size_t i = 0; i < number_count; i++)
} {
double l=(maxx - minn) / h; cin >> in.numbers[i];
for (size_t i = 0; i < n; i++) {
bool found = false;
for (size_t j = 0; (j < h - 1) && !found; j++) {
auto lo = minn + j * l;
auto hi = minn + (j + 1) * l;
if ((lo <= num[i]) && (num[i] < hi)) {
bins[j]++;
found = true;
}
}
if (!found) {
bins[h - 1]++;
}
}
size_t max_count = bins[0];
for (size_t i = 0; i < h; i++) {
if (bins[i]>max_count) {
max_count = bins[i];
}
}
vector<size_t> height(h);
for (size_t i = 0; i < h; i++) {
if (max_count > MAX_ASTERISK) {
height[i] = round(MAX_ASTERISK * (static_cast<double>(bins[i]) / max_count));
} else {
height[i]=bins[i];
}
}
for (size_t i=0;i<h;i++){
if(bins[i]<1000 && bins[i]>=100){
cout<< bins[i]<< "|";
for (size_t j=0;j<height[i];j++){
cout <<"*";
}
cout << endl;
}
if(bins[i]<100 && bins[i]>=10){
cout<<" "<< bins[i]<< "|";
for (size_t j=0;j<height[i];j++){
cout <<"*";
}
cout << endl;
}
if(bins[i]<10){
cout<<" "<< bins[i]<< "|";
for (size_t j=0;j<height[i];j++){
cout <<"*";
}
cout << endl;
}
} }
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 <int> 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<int> &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

@ -0,0 +1,40 @@
<?xml version="1.0" encoding="UTF-8" standalone="yes" ?>
<CodeBlocks_project_file>
<FileVersion major="1" minor="6" />
<Project>
<Option title="unittest" />
<Option pch_mode="2" />
<Option compiler="gcc" />
<Build>
<Target title="Debug">
<Option output="bin/Debug/unittest" prefix_auto="1" extension_auto="1" />
<Option object_output="obj/Debug/" />
<Option type="1" />
<Option compiler="gcc" />
<Compiler>
<Add option="-g" />
</Compiler>
</Target>
<Target title="Release">
<Option output="bin/Release/unittest" prefix_auto="1" extension_auto="1" />
<Option object_output="obj/Release/" />
<Option type="1" />
<Option compiler="gcc" />
<Compiler>
<Add option="-O2" />
</Compiler>
<Linker>
<Add option="-s" />
</Linker>
</Target>
</Build>
<Compiler>
<Add option="-Wall" />
</Compiler>
<Unit filename="histogram.cpp" />
<Unit filename="histogram.h" />
<Unit filename="histogram_internal.h" />
<Unit filename="unittest.cpp" />
<Extensions />
</Project>
</CodeBlocks_project_file>

@ -0,0 +1,49 @@
#define DOCTEST_CONFIG_NO_MULTITHREADING
#define DOCTEST_CONFIG_IMPLEMENT_WITH_MAIN
#include "doctest.h"
#include <vector>
#include "histogram_internal.h"
TEST_CASE("distinct positive numbers") {
double min = 0;
double max = 0;
bool a;
find_minmax({1, 2}, min, max,a);
CHECK(min == 1);
CHECK(max == 2);
}
TEST_CASE("distinct negative numbers") {
double min = 0;
double max = 0;
bool a;
find_minmax({-1, -2}, min, max,a);
CHECK(min == -2);
CHECK(max == -1);
}
TEST_CASE("one array") {
double min = 0;
double max = 0;
bool a;
const std::vector <double> numbers={1};
find_minmax(numbers, min, max,a);
CHECK(min == 1);
CHECK(max == 1);
}
TEST_CASE("= array") {
double min = 0;
double max = 0;
bool a;
const std::vector <double> numbers={1,1,1,1};
find_minmax(numbers, min, max,a);
CHECK(min == 1);
CHECK(max == 1);
}
TEST_CASE("empty array") {
double min = 0;
double max = 0;
bool a;
find_minmax({}, min, max,a);
CHECK(a==1);
CHECK(min == 0);
CHECK(max == 0);
}
Загрузка…
Отмена
Сохранить