code: возвращение к базовому коду

main
BaranovEK 11 месяцев назад
Родитель bfb3f8cf9c
Сommit c0d2a43b70

@ -8,7 +8,7 @@ using namespace std;
struct Input { struct Input {
vector<double> numbers; vector<double> numbers;
size_t bin_count{}; size_t bin_count{};
double COLUMN_HIGHT;
}; };
Input Input
input_data() { input_data() {
@ -23,227 +23,16 @@ input_data() {
} }
cerr << "Enter bin count: "; cerr << "Enter bin count: ";
cin>> in.bin_count; cin>> in.bin_count;
cerr << "Enter column hight: ";
cin >> in.COLUMN_HIGHT;
while (in.COLUMN_HIGHT <=0 ){
cerr << "The height of the column can only be positive, try again: ";
cin >> in.COLUMN_HIGHT;
}
return in; 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 > max )
{
max = x;
}
if (x < min)
{
min = x;
}
}
}
vector<size_t>
make_histogram(const vector<double>& numbers, size_t& bin_count)
{
double min = 0;
double max = 0;
find_minmax(numbers, min, max);
auto bin_size = (max - min)/bin_count;
vector<size_t> bins(bin_count);
for (double number : numbers)
{
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 <= number) && (number < hi))
{
bins[j]++;
found = true;
}
}
if (!found)
{
bins[bin_count - 1]++;
}
}
return bins;
}*/
/*void
show_histogram_text(const vector<size_t>& bins, size_t& bin_count) {
const size_t SCREEN_WIDTH = 80;
const size_t MAX_ASTERISK = SCREEN_WIDTH - 3 - 1;
size_t max_count = 0;
for (size_t s = 0; s < bin_count; s++)
{
if (bins[s] > max_count)
{
max_count = bins[s];
}
}
auto kof = static_cast<double>(MAX_ASTERISK) / max_count;
if (kof > 1)
{
kof = 1;
}
for (size_t bin : bins)
{
if (bin <100)
{
cout << " ";
}
if (bin <10)
{
cout << " ";
}
cout << bin << "|";
size_t weight = bin * kof;
for (size_t t = 0; t < weight; t++)
{
cout << "*";
}
cout << endl;
}
}*/
int int
main() { main() {
auto in = input_data(); auto in = input_data();
auto bins = make_histogram(in.numbers, in.bin_count); auto bins = make_histogram(in.numbers, in.bin_count);
// show_histogram_svg(bins); show_histogram_svg(bins);
COLUMN_hight_function(in.COLUMN_HIGHT,in.bin_count,bins);
/*int main()
{
Input in = input_data();
double min, max;
find_minmax(in.numbers, min, max);
cout<< min << " " << max;
const size_t SCREEN_WIDTH = 80;
const size_t MAX_ASTERISK = SCREEN_WIDTH - 3 - 1;
size_t max_count = 0;
size_t max_count_bin;
size_t number_count;
//cerr << "Enter number count: ";
//cin >> number_count;
//vector<double> numbers(number_count);
//cerr << "Enter numbers: ";
//for (int i = 0; i < number_count; i++)
// {
// cin >> numbers[i];
// }
//size_t bin_count;
// cerr << "Enter bin count: ";
// cin >> bin_count;
// vector<size_t > bins(bin_count);
double bin_size = ((max - min) / bin_count);
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 (!found)
{
bins[bin_count - 1]++;
}
for (size_t i = 0; i < bin_count; i++)
{
if (bins[i] > max_count)
{
max_count = bins[i];
max_count_bin = i;
}
}
}
vector<size_t> bins_height(bin_count);
if (max_count > MAX_ASTERISK) {
for (size_t i = 0; i < bin_count; i++) {
bins_height[i]= MAX_ASTERISK * (static_cast<double>(bins[i]) / max_count);
}
}
else {
for (size_t i = 0; i < bin_count; i++) {
bins_height[i] = bins[i];
}
}
for (size_t i = 0; i < bin_count; i++) {
if (bins[i] < 100) {
cout << " ";
if (bins[i] < 10) {
cout << " ";
}
}
cout << bins[i] << "|";
for (size_t j = 0; j < bins_height[i]; j++) {
cout << "*";
}
cout << endl;
}
size_t histogram_height;
cerr << "Enter the histogram height" << endl;
cin >> histogram_height;
size_t C = (histogram_height / bin_count);
//èíäèâèäóëüíûé âàðèàíò íîìåð 3:
if (C >= 1) {
for (size_t i = 0; i < bin_count; i++) {
if (bins[i] < 100) {
cout << " ";
if (bins[i] < 10) {
cout << " ";
}
}
cout << bins[i];
cout << "|";
for (size_t j = 0; j < bins_height[i]; j++) {
cout << "*";
}
cout << endl;
for (size_t g = 1; g < C; g++) {
cout << " ";
cout << "|";
for (size_t j = 0; j < bins_height[i]; j++) {
cout << "*";
}
cout << endl;
}
}
}
else
{
cout << "Histogram height is too small";
}
*/
return 0; return 0;
} }

@ -5,7 +5,7 @@
using namespace std; using namespace std;
const auto IMAGE_WIDTH = 400; const auto IMAGE_WIDTH = 400;
const auto IMAGE_HEIGHT = 700; const auto IMAGE_HEIGHT = 300;
const auto TEXT_LEFT = 20; const auto TEXT_LEFT = 20;
const auto TEXT_BASELINE = 20; const auto TEXT_BASELINE = 20;
const auto TEXT_WIDTH = 50; const auto TEXT_WIDTH = 50;
@ -54,7 +54,7 @@ show_histogram_svg(const vector<size_t>& bins) {
if (scale_factor > 1) { if (scale_factor > 1) {
scale_factor = 1; scale_factor = 1;
} }
svg_begin(400, 300); svg_begin(IMAGE_WIDTH, IMAGE_HEIGHT);
double top = 0; double top = 0;
for (size_t bin : bins) { for (size_t bin : bins) {
double bin_width = BLOCK_WIDTH * bin * scale_factor; double bin_width = BLOCK_WIDTH * bin * scale_factor;
@ -65,40 +65,4 @@ show_histogram_svg(const vector<size_t>& bins) {
svg_end(); svg_end();
} }
void
svg_rect_hight(double x, double y, double width, double height, string stroke = "black", string fill = "hfhfh") {
cout << "<rect x='" << x<< "' y='" << y<< "' width='" << width<< "' height='" << height<< "' stroke='" << stroke<< "' fill='" << fill << "' />\n";
}
void
COLUMN_hight_function( double &COLUMN_height ,size_t bin_count,const vector<size_t>& bins){
const auto MAX_WIDTH = IMAGE_WIDTH - TEXT_WIDTH;
size_t max_count = 0;
for (size_t x : bins) {
if (x > max_count) {
max_count = x;
}
}
if (max_count == 0) {
max_count = 1;
}
auto scale_factor = static_cast<double>(MAX_WIDTH) / (max_count * BLOCK_WIDTH);
if (scale_factor > 1) {
scale_factor = 1;
}
svg_begin(IMAGE_WIDTH, IMAGE_HEIGHT );
if ((bin_count*COLUMN_height) > IMAGE_HEIGHT ){
COLUMN_height= static_cast<double>(IMAGE_HEIGHT) / bin_count;
}
double top = 0;
for (size_t bin : bins) {
double bin_width = BLOCK_WIDTH * bin * scale_factor;
svg_text(TEXT_LEFT, top + TEXT_BASELINE, to_string(bin));
svg_rect(TEXT_WIDTH, top, bin_width, COLUMN_height);
top += COLUMN_height;
}
svg_end();
}

@ -5,6 +5,5 @@
void void
show_histogram_svg(const std::vector<size_t>& bins); show_histogram_svg(const std::vector<size_t>& bins);
void
COLUMN_hight_function( double &COLUMN_height ,size_t bin_count,const std::vector<size_t>& bins);
#endif // SVG_H_INCLUDED #endif // SVG_H_INCLUDED

Загрузка…
Отмена
Сохранить