Сравнить коммиты
9 Коммитов
a264f738e0
...
main
| Автор | SHA1 | Дата | |
|---|---|---|---|
|
|
2eb0146779 | ||
|
|
d8705af2f6 | ||
|
|
1ff183266d | ||
|
|
41401e5a9f | ||
|
|
12b523ef2f | ||
|
|
e22859b6eb | ||
| ef3566b21e | |||
|
|
f67263f063 | ||
|
|
4ec4a7cd73 |
7106
doctest.h
Обычный файл
7106
doctest.h
Обычный файл
Разница между файлами не показана из-за своего большого размера
Загрузить разницу
48
histogram.cpp
Обычный файл
48
histogram.cpp
Обычный файл
@@ -0,0 +1,48 @@
|
|||||||
|
#include "histogram.h"
|
||||||
|
#include <iostream>
|
||||||
|
#include <vector>
|
||||||
|
using namespace std;
|
||||||
|
void find_minmax( const vector<double>& numbers, double& minN, double& maxN) {
|
||||||
|
minN = numbers[0];
|
||||||
|
maxN = numbers[0];
|
||||||
|
|
||||||
|
for (double x: numbers){
|
||||||
|
if (minN > x){
|
||||||
|
minN = x;
|
||||||
|
}
|
||||||
|
if (maxN < x){
|
||||||
|
maxN = x;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
|
vector<size_t> make_histogram(const vector<double>& numbers, size_t bin_count){
|
||||||
|
double minN, maxN;
|
||||||
|
find_minmax( numbers, minN, maxN);
|
||||||
|
|
||||||
|
vector <size_t> bins(bin_count);
|
||||||
|
double diff = (maxN - minN) / bin_count;
|
||||||
|
size_t max_count = 0;
|
||||||
|
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 = minN + j * diff;
|
||||||
|
auto hi = minN + (j + 1) * diff;
|
||||||
|
if ((lo <= numbers[i]) && (hi > numbers[i])){
|
||||||
|
bins[j]++;
|
||||||
|
if (bins[j] > max_count){
|
||||||
|
max_count = bins[j];
|
||||||
|
}
|
||||||
|
found = true;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
if(!found){
|
||||||
|
bins[bin_count - 1]++;
|
||||||
|
if (bins[bin_count - 1] > max_count){
|
||||||
|
max_count = bins[bin_count - 1];
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return bins;
|
||||||
|
}
|
||||||
6
histogram.h
Обычный файл
6
histogram.h
Обычный файл
@@ -0,0 +1,6 @@
|
|||||||
|
#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
|
||||||
6
histogram_internal.h
Обычный файл
6
histogram_internal.h
Обычный файл
@@ -0,0 +1,6 @@
|
|||||||
|
#ifndef HISTOGRAM_INTERNAL_H_INCLUDED
|
||||||
|
#define HISTOGRAM_INTERNAL_H_INCLUDED
|
||||||
|
#include <vector>
|
||||||
|
void find_minmax( const std::vector<double>& numbers, double& minN, double& maxN);
|
||||||
|
|
||||||
|
#endif // HISTOGRAM_INTERNAL_H_INCLUDED
|
||||||
@@ -33,7 +33,13 @@
|
|||||||
<Add option="-fexceptions" />
|
<Add option="-fexceptions" />
|
||||||
</Compiler>
|
</Compiler>
|
||||||
<Unit filename=".gitignore" />
|
<Unit filename=".gitignore" />
|
||||||
|
<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>
|
||||||
|
|||||||
109
main.cpp
109
main.cpp
@@ -1,92 +1,35 @@
|
|||||||
#include <iostream>
|
#include <iostream>
|
||||||
#include <vector>
|
#include <vector>
|
||||||
|
#include "histogram.h"
|
||||||
|
#include "text.h"
|
||||||
|
#include "svg.h"
|
||||||
using namespace std;
|
using namespace std;
|
||||||
|
|
||||||
int main(){
|
struct Input {
|
||||||
|
vector<double> numbers;
|
||||||
|
size_t bin_count{};
|
||||||
|
};
|
||||||
|
|
||||||
const size_t SCREEN_WIDTH = 80;
|
Input
|
||||||
const size_t MAX_ASTERISK = SCREEN_WIDTH - 6 - 1;
|
input_data(){
|
||||||
size_t bin_count, counts;
|
Input in;
|
||||||
cerr << "Enter counts:";
|
size_t number_count;
|
||||||
cin >> counts;
|
cin >> number_count;
|
||||||
|
in.numbers.resize(number_count);
|
||||||
|
|
||||||
vector <double> numbers(counts);
|
vector<double> numbers(number_count);
|
||||||
|
for (size_t i = 0; i < number_count; i++) {
|
||||||
for (size_t i = 0; i < counts; i++){
|
cin >> in.numbers[i];
|
||||||
cerr << "Enter array "<< i <<" number:";
|
|
||||||
cin >> numbers[i];
|
|
||||||
}
|
}
|
||||||
|
|
||||||
cerr << "Enter bin_count:";
|
cin >> in.bin_count;
|
||||||
cin >> bin_count;
|
return in;
|
||||||
|
}
|
||||||
double minN = numbers[0], maxN = numbers[0];
|
|
||||||
|
int main(){
|
||||||
for (double x: numbers){
|
size_t max_count;
|
||||||
if (minN > x){
|
auto in = input_data();
|
||||||
minN = x;
|
auto bins = make_histogram(in.numbers, in.bin_count);
|
||||||
}
|
show_histogram_svg(bins);
|
||||||
if (maxN < x){
|
return 0;
|
||||||
maxN = x;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
double diff = (maxN - minN) / bin_count;
|
|
||||||
|
|
||||||
vector <size_t> bins(bin_count);
|
|
||||||
|
|
||||||
size_t max_count = 0;
|
|
||||||
for (size_t i = 0; i < counts; i++){
|
|
||||||
bool found = false;
|
|
||||||
for (size_t j = 0;(j < bin_count - 1) && !found; j++){
|
|
||||||
auto lo = minN + j * diff;
|
|
||||||
auto hi = minN + (j + 1) * diff;
|
|
||||||
if ((lo <= numbers[i]) && (hi > numbers[i])){
|
|
||||||
bins[j]++;
|
|
||||||
if (bins[j] > max_count){
|
|
||||||
max_count = bins[j];
|
|
||||||
}
|
|
||||||
found = true;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
if(!found){
|
|
||||||
bins[bin_count - 1]++;
|
|
||||||
if (bins[bin_count - 1] > max_count){
|
|
||||||
max_count = bins[bin_count - 1];
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
bool scaling = false;
|
|
||||||
|
|
||||||
if (max_count > MAX_ASTERISK){
|
|
||||||
scaling = true;
|
|
||||||
}
|
|
||||||
|
|
||||||
for (size_t i = 0; i < bin_count; i++){
|
|
||||||
cout << " ";
|
|
||||||
if (bins[i] < 100){
|
|
||||||
cout << ' ';
|
|
||||||
}
|
|
||||||
if (bins[i] < 10){
|
|
||||||
cout << ' ';
|
|
||||||
}
|
|
||||||
cout << bins[i] << '|';
|
|
||||||
|
|
||||||
size_t number_of_stars = bins[i];
|
|
||||||
|
|
||||||
if (scaling){
|
|
||||||
if (bins[i] == max_count){
|
|
||||||
number_of_stars = MAX_ASTERISK * 1.0;
|
|
||||||
}
|
|
||||||
else {
|
|
||||||
number_of_stars = MAX_ASTERISK * (static_cast<double>(bins[i]) / max_count);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
for (size_t j = 0; j < number_of_stars; j++){
|
|
||||||
cout << '*';
|
|
||||||
}
|
|
||||||
cout << endl;
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|||||||
63
svg.cpp
Обычный файл
63
svg.cpp
Обычный файл
@@ -0,0 +1,63 @@
|
|||||||
|
#include "svg.h"
|
||||||
|
#include <string>
|
||||||
|
using namespace std;
|
||||||
|
|
||||||
|
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 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 = "black")
|
||||||
|
{
|
||||||
|
cout << "<rect x='" << x << "' y='" << y << "' width='" << width
|
||||||
|
<< "' height='" << height << "' stroke='" << stroke << "' fill='" << fill << " '/>";
|
||||||
|
}
|
||||||
|
void show_histogram_svg(const vector<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;
|
||||||
|
|
||||||
|
svg_begin(400, 300);
|
||||||
|
double top = 0;
|
||||||
|
|
||||||
|
size_t max_count = 0;
|
||||||
|
for (auto bin : bins)
|
||||||
|
{
|
||||||
|
if (bin > max_count)
|
||||||
|
max_count = bin;
|
||||||
|
}
|
||||||
|
const auto BLOCK_WIDTH = (IMAGE_WIDTH - TEXT_WIDTH) / max_count;
|
||||||
|
|
||||||
|
for (size_t bin : bins)
|
||||||
|
{
|
||||||
|
string color;
|
||||||
|
cout<< "color"<<bin<< "=";
|
||||||
|
cin>>color;
|
||||||
|
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, "blue", color);
|
||||||
|
top += BIN_HEIGHT;
|
||||||
|
}
|
||||||
|
svg_end();
|
||||||
|
}
|
||||||
8
svg.h
Обычный файл
8
svg.h
Обычный файл
@@ -0,0 +1,8 @@
|
|||||||
|
#ifndef SVG_H_INCLUDED
|
||||||
|
#define SVG_H_INCLUDED
|
||||||
|
#include <iostream>
|
||||||
|
#include <vector>
|
||||||
|
|
||||||
|
void show_histogram_svg(const std::vector<size_t> &bins);
|
||||||
|
|
||||||
|
#endif // SVG_H_INCLUDED
|
||||||
41
text.cpp
Обычный файл
41
text.cpp
Обычный файл
@@ -0,0 +1,41 @@
|
|||||||
|
#include "text.h"
|
||||||
|
#include <iostream>
|
||||||
|
#include <vector>
|
||||||
|
using namespace std;
|
||||||
|
void show_histogram_text(const vector <size_t>& bins,size_t bin_count, size_t max_count){
|
||||||
|
const size_t SCREEN_WIDTH = 80;
|
||||||
|
const size_t MAX_ASTERISK = SCREEN_WIDTH - 6 - 1;
|
||||||
|
|
||||||
|
bool scaling = false;
|
||||||
|
|
||||||
|
if (max_count > MAX_ASTERISK){
|
||||||
|
scaling = true;
|
||||||
|
}
|
||||||
|
|
||||||
|
for (size_t i = 0; i < bin_count; i++){
|
||||||
|
cout << " ";
|
||||||
|
if (bins[i] < 100){
|
||||||
|
cout << ' ';
|
||||||
|
}
|
||||||
|
if (bins[i] < 10){
|
||||||
|
cout << ' ';
|
||||||
|
}
|
||||||
|
cout << bins[i] << '|';
|
||||||
|
|
||||||
|
size_t number_of_stars = bins[i];
|
||||||
|
|
||||||
|
if (scaling){
|
||||||
|
if (bins[i] == max_count){
|
||||||
|
number_of_stars = MAX_ASTERISK * 1.0;
|
||||||
|
}
|
||||||
|
else {
|
||||||
|
number_of_stars = MAX_ASTERISK * (static_cast<double>(bins[i]) / max_count);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
for (size_t j = 0; j < number_of_stars; j++){
|
||||||
|
cout << '*';
|
||||||
|
}
|
||||||
|
cout << endl;
|
||||||
|
}
|
||||||
|
}
|
||||||
6
text.h
Обычный файл
6
text.h
Обычный файл
@@ -0,0 +1,6 @@
|
|||||||
|
#ifndef TEXT_H_INCLUDED
|
||||||
|
#define TEXT_H_INCLUDED
|
||||||
|
#include <vector>
|
||||||
|
void show_histogram_text(const std::vector <size_t>& bins,size_t bin_count, size_t max_count);
|
||||||
|
|
||||||
|
#endif // TEXT_H_INCLUDED
|
||||||
47
unittest.cbp
Обычный файл
47
unittest.cbp
Обычный файл
@@ -0,0 +1,47 @@
|
|||||||
|
<?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="doctest.h" />
|
||||||
|
<Unit filename="histogram.cpp" />
|
||||||
|
<Unit filename="histogram.h" />
|
||||||
|
<Unit filename="histogram_internal.h" />
|
||||||
|
<Unit filename="svg.cpp" />
|
||||||
|
<Unit filename="svg.h">
|
||||||
|
<Option target="<{~None~}>" />
|
||||||
|
</Unit>
|
||||||
|
<Unit filename="unittest.cpp" />
|
||||||
|
<Extensions>
|
||||||
|
<lib_finder disable_auto="1" />
|
||||||
|
</Extensions>
|
||||||
|
</Project>
|
||||||
|
</CodeBlocks_project_file>
|
||||||
13
unittest.cpp
Обычный файл
13
unittest.cpp
Обычный файл
@@ -0,0 +1,13 @@
|
|||||||
|
#define DOCTEST_CONFIG_NO_MULTITHREADING
|
||||||
|
#define DOCTEST_CONFIG_IMPLEMENT_WITH_MAIN
|
||||||
|
#include "doctest.h"
|
||||||
|
#include "histogram_internal.h"
|
||||||
|
#include "histogram.h"
|
||||||
|
|
||||||
|
TEST_CASE("distinct positive numbers") {
|
||||||
|
double minN = 0;
|
||||||
|
double maxN = 0;
|
||||||
|
find_minmax({1, 2}, minN, maxN);
|
||||||
|
CHECK(minN == 1);
|
||||||
|
CHECK(maxN == 2);
|
||||||
|
}
|
||||||
Ссылка в новой задаче
Block a user