Родитель
113f66c7ec
Сommit
7c6d2f19da
@ -1,75 +1,43 @@
|
|||||||
#include <vector>
|
#include <vector>
|
||||||
#include <iostream>
|
#include <iostream>
|
||||||
#include <string>
|
#include <string>
|
||||||
|
#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 - 3 - 1;
|
|
||||||
|
|
||||||
|
|
||||||
double maxx, minn, MaxCount = 0;
|
struct Input {
|
||||||
size_t NumberCount, BinCount;
|
vector<double> Numbers;
|
||||||
|
size_t BinCount{};
|
||||||
|
};
|
||||||
|
|
||||||
|
Input
|
||||||
|
InputData() {
|
||||||
|
Input in;
|
||||||
|
size_t NumberCount;
|
||||||
cerr << "Enter number count: ";
|
cerr << "Enter number count: ";
|
||||||
cin >> NumberCount;
|
cin >> NumberCount;
|
||||||
cerr << "Enter bin count: ";
|
cerr << "Enter bin count: ";
|
||||||
cin >> BinCount;
|
cin >> in.BinCount;
|
||||||
vector <double> Numbers(NumberCount);
|
in.Numbers.resize(NumberCount);
|
||||||
for (int i = 0; i < NumberCount; i++) {
|
for (int i = 0; i < NumberCount; i++) {
|
||||||
cin >> Numbers[i];
|
cin >> in.Numbers[i];
|
||||||
}
|
|
||||||
vector <size_t> Bins(BinCount);
|
|
||||||
maxx = Numbers[0];
|
|
||||||
minn = Numbers[0];
|
|
||||||
for (auto x : Numbers) {
|
|
||||||
if (x > maxx) {
|
|
||||||
maxx = x;
|
|
||||||
}
|
|
||||||
if (x < minn) {
|
|
||||||
minn = x;
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
double BinSize = (maxx - minn) / BinCount;
|
return in;
|
||||||
|
}
|
||||||
|
|
||||||
for (size_t i = 0; i < NumberCount; i++) {
|
void FindMinMax(const vector<double>& Numbers, double& minn, double& maxx);
|
||||||
bool found = false;
|
|
||||||
for (size_t j = 0; (j < BinCount - 1) && !found; j++) {
|
vector<size_t> MakeHistogram(vector<double> Numbers, size_t BinCount, double& MaxCount);
|
||||||
auto lo = minn + j * BinSize;
|
|
||||||
auto hi = minn + (j + 1) * BinSize;
|
void ShowHistogrammText(vector<size_t> Bins);
|
||||||
if ((lo <= Numbers[i]) && (Numbers[i] < hi)) {
|
|
||||||
Bins[j]++;
|
int main() {
|
||||||
found = true;
|
double MaxCount = 0;
|
||||||
if (Bins[j] > MaxCount) {
|
Input in = InputData();
|
||||||
MaxCount = Bins[j];
|
vector <size_t> Bins(in.BinCount);
|
||||||
}
|
Bins = MakeHistogram(in.Numbers, in.BinCount, MaxCount);
|
||||||
}
|
ShowHistogramSvg(Bins);
|
||||||
}
|
|
||||||
if (!found) {
|
|
||||||
Bins[BinCount - 1]++;
|
|
||||||
if (Bins[BinCount - 1] > MaxCount) {
|
|
||||||
MaxCount = Bins[BinCount - 1];
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
for (auto bin : Bins) {
|
|
||||||
if (bin < 100) {
|
|
||||||
cout << " ";
|
|
||||||
}
|
|
||||||
if (bin < 10) {
|
|
||||||
cout << " ";
|
|
||||||
}
|
|
||||||
cout << bin << "|";
|
|
||||||
size_t height = MAX_ASTERISK * (static_cast<double>(bin) / MaxCount);
|
|
||||||
if (MaxCount <= 76) {
|
|
||||||
for (int i = 0; i < bin; i++) {
|
|
||||||
cout << '*';
|
|
||||||
}
|
|
||||||
}
|
|
||||||
else {
|
|
||||||
for (int i = 0; i < height; i++) {
|
|
||||||
cout << '*';
|
|
||||||
}
|
|
||||||
}
|
|
||||||
cout << endl;
|
|
||||||
}
|
|
||||||
return 0;
|
return 0;
|
||||||
}
|
}
|
||||||
|
@ -0,0 +1,43 @@
|
|||||||
|
#include "histogram.h"
|
||||||
|
#include <vector>
|
||||||
|
using namespace std;
|
||||||
|
void FindMinMax(const vector<double>& Numbers, double& minn, double& maxx) {
|
||||||
|
maxx = Numbers[0];
|
||||||
|
minn = Numbers[0];
|
||||||
|
for (auto x : Numbers) {
|
||||||
|
if (x > maxx) {
|
||||||
|
maxx = x;
|
||||||
|
}
|
||||||
|
if (x < minn) {
|
||||||
|
minn = x;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
vector<size_t> MakeHistogram(vector<double> Numbers, size_t BinCount, double& MaxCount) {
|
||||||
|
double maxx, minn;
|
||||||
|
FindMinMax(Numbers, minn, maxx);
|
||||||
|
double BinSize = (maxx - minn) / BinCount;
|
||||||
|
vector <size_t> Bins(BinCount);
|
||||||
|
for (size_t i = 0; i < Numbers.size(); i++) {
|
||||||
|
bool found = false;
|
||||||
|
for (size_t j = 0; (j < BinCount - 1) && !found; j++) {
|
||||||
|
auto lo = minn + j * BinSize;
|
||||||
|
auto hi = minn + (j + 1) * BinSize;
|
||||||
|
if ((lo <= Numbers[i]) && (Numbers[i] < hi)) {
|
||||||
|
Bins[j]++;
|
||||||
|
found = true;
|
||||||
|
if (Bins[j] > MaxCount) {
|
||||||
|
MaxCount = Bins[j];
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
if (!found) {
|
||||||
|
Bins[BinCount - 1]++;
|
||||||
|
if (Bins[BinCount - 1] > MaxCount) {
|
||||||
|
MaxCount = Bins[BinCount - 1];
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return Bins;
|
||||||
|
}
|
@ -0,0 +1,4 @@
|
|||||||
|
#pragma once
|
||||||
|
#include <vector>
|
||||||
|
std::vector<size_t>
|
||||||
|
MakeHistogram(const std::vector<double> Numbers, size_t BinCount, double& MaxCount);
|
@ -0,0 +1,3 @@
|
|||||||
|
#pragma once
|
||||||
|
#include <vector>
|
||||||
|
void FindMinMax(const std::vector<double>& Numbers, double& minn, double& maxx);
|
@ -0,0 +1,64 @@
|
|||||||
|
#include <iostream>
|
||||||
|
#include "svg.h"
|
||||||
|
#include <vector>
|
||||||
|
#include <string>
|
||||||
|
using namespace std;
|
||||||
|
|
||||||
|
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;
|
||||||
|
|
||||||
|
void
|
||||||
|
SvgBegin(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
|
||||||
|
SvgEnd() {
|
||||||
|
cout << "</svg>\n";
|
||||||
|
}
|
||||||
|
|
||||||
|
void
|
||||||
|
SvgText(double left, double baseline, string text) {
|
||||||
|
cout << "<text x='" << left << "' y='" << baseline << "' >" << text << "</text>\n";
|
||||||
|
}
|
||||||
|
|
||||||
|
void SvgRect(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 << "' />\n";
|
||||||
|
}
|
||||||
|
|
||||||
|
void
|
||||||
|
ShowHistogramSvg(const vector<size_t>& bins) {
|
||||||
|
double top = 0;
|
||||||
|
SvgBegin(400, 300);
|
||||||
|
|
||||||
|
size_t MaxCount = 0;
|
||||||
|
for (auto bin : bins) {
|
||||||
|
if (MaxCount < bin) {
|
||||||
|
MaxCount = bin;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
int k = (IMAGE_WIDTH - TEXT_WIDTH) / MaxCount * BLOCK_WIDTH;
|
||||||
|
|
||||||
|
if (k > 1) k = 1;
|
||||||
|
|
||||||
|
for (size_t bin : bins) {
|
||||||
|
bin = bin * k;
|
||||||
|
const double bin_width = BLOCK_WIDTH * bin;
|
||||||
|
SvgText(TEXT_LEFT, top + TEXT_BASELINE, to_string(bin));
|
||||||
|
SvgRect(TEXT_WIDTH, top, bin_width, BIN_HEIGHT, "red", "#FF69B4");
|
||||||
|
top += BIN_HEIGHT;
|
||||||
|
}
|
||||||
|
SvgEnd();
|
||||||
|
}
|
||||||
|
|
||||||
|
|
@ -0,0 +1,6 @@
|
|||||||
|
#pragma once
|
||||||
|
#include <vector>
|
||||||
|
|
||||||
|
void
|
||||||
|
ShowHistogramSvg(const std::vector<size_t>& bins);
|
||||||
|
|
@ -0,0 +1,39 @@
|
|||||||
|
#include "text.h"
|
||||||
|
#include <iostream>
|
||||||
|
using namespace std;
|
||||||
|
const size_t SCREEN_WIDTH = 80;
|
||||||
|
const size_t MAX_ASTERISK = SCREEN_WIDTH - 3 - 1;
|
||||||
|
|
||||||
|
void ShowHistogrammText(vector<size_t> Bins) {
|
||||||
|
size_t MaxCount = 0;
|
||||||
|
for (auto bin : Bins) {
|
||||||
|
if (MaxCount < bin) {
|
||||||
|
MaxCount = bin;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
for (auto bin : Bins) {
|
||||||
|
if (bin < 100) {
|
||||||
|
cout << " ";
|
||||||
|
}
|
||||||
|
if (bin < 10) {
|
||||||
|
cout << " ";
|
||||||
|
}
|
||||||
|
cout << bin << "|";
|
||||||
|
size_t height = MAX_ASTERISK * (static_cast<double>(bin) / MaxCount);
|
||||||
|
if (MaxCount <= 76) {
|
||||||
|
for (int i = 0; i < bin; i++) {
|
||||||
|
cout << '*';
|
||||||
|
}
|
||||||
|
}
|
||||||
|
else {
|
||||||
|
for (int i = 0; i < height; i++) {
|
||||||
|
cout << '*';
|
||||||
|
}
|
||||||
|
}
|
||||||
|
cout << endl;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
@ -0,0 +1,6 @@
|
|||||||
|
#pragma once
|
||||||
|
#include <vector>
|
||||||
|
|
||||||
|
|
||||||
|
void
|
||||||
|
ShowHistogrammText(std::vector<size_t> Bins);
|
Загрузка…
Ссылка в новой задаче