Сравнить коммиты
8 Коммитов
e5bab319ac
...
main
| Автор | SHA1 | Дата | |
|---|---|---|---|
|
|
161fd910f1 | ||
|
|
6cd71782fd | ||
|
|
17deb398d5 | ||
|
|
faa5b7af4e | ||
|
|
86ea4a326a | ||
|
|
14dc9d1947 | ||
|
|
1fed52ed73 | ||
|
|
19e8e672be |
@@ -0,0 +1 @@
|
||||
task.txt
|
||||
50
histogram.cpp
Обычный файл
50
histogram.cpp
Обычный файл
@@ -0,0 +1,50 @@
|
||||
#include <iostream>
|
||||
#include <vector>
|
||||
|
||||
using namespace std;
|
||||
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<int>
|
||||
make_histogram(std::vector<double> numbers, size_t bin_count){
|
||||
double min;
|
||||
double max;
|
||||
vector<int> bins(bin_count);
|
||||
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;
|
||||
}
|
||||
9
histogram.h
Обычный файл
9
histogram.h
Обычный файл
@@ -0,0 +1,9 @@
|
||||
#ifndef HISTOGRAM_H_INCLUDED
|
||||
#define HISTOGRAM_H_INCLUDED
|
||||
|
||||
#include <vector>
|
||||
#include <iostream>
|
||||
std::vector<int>
|
||||
make_histogram(std::vector<double> numbers, size_t bin_count);
|
||||
|
||||
#endif
|
||||
125
main.cpp
125
main.cpp
@@ -1,73 +1,42 @@
|
||||
#include <iostream>
|
||||
#include <vector>
|
||||
#include "histogram.h"
|
||||
|
||||
using namespace std;
|
||||
|
||||
int main()
|
||||
{
|
||||
const size_t SCREEN_WIDTH = 80;
|
||||
const size_t MAX_ASTERISK = SCREEN_WIDTH - 3 - 1;
|
||||
const size_t SCREEN_WIDTH = 80;
|
||||
const size_t MAX_ASTERISK = SCREEN_WIDTH - 3 - 1;
|
||||
|
||||
int max_count=0;
|
||||
struct
|
||||
Input {
|
||||
vector<double> numbers;
|
||||
int bin_count{};
|
||||
};
|
||||
|
||||
char grafic_symbol = '*';
|
||||
char axis_symbol = '|';
|
||||
char correcting_symbol = ' ';
|
||||
|
||||
cout<<"Enter a grafic_symbol";
|
||||
cin>>grafic_symbol;
|
||||
cout<<"Enter a axis_symbol";
|
||||
cin>>axis_symbol;
|
||||
cout<<"Enter a correcting_symbol";
|
||||
cin>>correcting_symbol;
|
||||
|
||||
int count;
|
||||
Input
|
||||
input_data(){
|
||||
size_t number_count;
|
||||
cerr << "Enter number count: ";
|
||||
cin >> number_count;
|
||||
size_t bin_count;
|
||||
cerr << "Enter bin count: ";
|
||||
cin >> bin_count;
|
||||
vector<double> numbers(number_count);
|
||||
vector<size_t> bins(bin_count);
|
||||
for (int i=0; i<number_count; i++)
|
||||
{
|
||||
cin>>numbers[i];
|
||||
Input in;
|
||||
in.numbers.resize(number_count);
|
||||
for (size_t i = 0; i < number_count; i++) {
|
||||
cin >> in.numbers[i];
|
||||
}
|
||||
int bin_count;
|
||||
cin >> in.bin_count;
|
||||
return in;
|
||||
}
|
||||
|
||||
double min = numbers[0];
|
||||
double max = numbers[0];
|
||||
for (double x : numbers)
|
||||
{
|
||||
if (x < min)
|
||||
{
|
||||
min = x;
|
||||
}
|
||||
else if (x > max)
|
||||
{
|
||||
max = x;
|
||||
}
|
||||
}
|
||||
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;
|
||||
}
|
||||
void
|
||||
show_histogramm_text(vector<int> bins, vector<double> numbers, size_t bin_count){
|
||||
|
||||
size_t max_count;
|
||||
for (int i =0; i<bin_count;i++){
|
||||
if (numbers[i]>max_count){
|
||||
max_count=numbers[i];
|
||||
}
|
||||
if (!found)
|
||||
{
|
||||
bins[bin_count - 1]++;
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
for (int i =0; i<bin_count;i++){
|
||||
if (bins[i]>max_count){
|
||||
@@ -75,29 +44,27 @@ int main()
|
||||
}
|
||||
}
|
||||
|
||||
cout<<max_count<<endl;
|
||||
|
||||
if (max_count>MAX_ASTERISK){
|
||||
for (int i=0; i<bin_count; i++){
|
||||
size_t height = MAX_ASTERISK * (static_cast<double>(bins[i]) / max_count);
|
||||
if (bins[i]<10){
|
||||
cout<<correcting_symbol<<correcting_symbol<<bins[i]<<axis_symbol;
|
||||
for (int j=0;j<height;j++){
|
||||
cout<<grafic_symbol;
|
||||
cout<<" "<<bins[i]<<"|";
|
||||
for (int j=0;j<bins[i];j++){
|
||||
cout<<"*";
|
||||
}
|
||||
cout<<endl;
|
||||
}
|
||||
else if(bins[i]<100){
|
||||
cout<<correcting_symbol<<bins[i]<<axis_symbol;
|
||||
for (int j=0;j<height;j++){
|
||||
cout<<grafic_symbol;
|
||||
cout<<" "<<bins[i]<<"|";
|
||||
for (int j=0;j<bins[i];j++){
|
||||
cout<<"*";
|
||||
}
|
||||
cout<<endl;
|
||||
}
|
||||
else{
|
||||
cout<<bins[i]<<axis_symbol;
|
||||
for (int j=0;j<height;j++){
|
||||
cout<<grafic_symbol;
|
||||
cout<<bins[i]<<"|";
|
||||
for (int j=0;j<bins[i];j++){
|
||||
cout<<"*";
|
||||
}
|
||||
cout<<endl;
|
||||
}
|
||||
@@ -106,23 +73,23 @@ int main()
|
||||
}else{
|
||||
for (int i=0; i<bin_count; i++){
|
||||
if (bins[i]<10){
|
||||
cout<<correcting_symbol<<correcting_symbol<<bins[i]<<axis_symbol;
|
||||
cout<<" "<<bins[i]<<"|";
|
||||
for (int j=0;j<bins[i];j++){
|
||||
cout<<grafic_symbol;
|
||||
cout<<"*";
|
||||
}
|
||||
cout<<endl;
|
||||
}
|
||||
else if(bins[i]<100){
|
||||
cout<<correcting_symbol<<bins[i]<<axis_symbol;
|
||||
cout<<" "<<bins[i]<<"|";
|
||||
for (int j=0;j<bins[i];j++){
|
||||
cout<<grafic_symbol;
|
||||
cout<<"*";
|
||||
}
|
||||
cout<<endl;
|
||||
}
|
||||
else{
|
||||
cout<<bins[i]<<axis_symbol;
|
||||
cout<<bins[i]<<"|";
|
||||
for (int j=0;j<bins[i];j++){
|
||||
cout<<grafic_symbol;
|
||||
cout<<"*";
|
||||
}
|
||||
cout<<endl;
|
||||
}
|
||||
@@ -130,3 +97,15 @@ int main()
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
int
|
||||
main()
|
||||
{
|
||||
|
||||
auto in = input_data();
|
||||
auto bins = make_histogram(in.numbers, in.bin_count);
|
||||
show_histogramm_text(bins, in.numbers, in.bin_count);
|
||||
}
|
||||
|
||||
|
||||
24
task.txt
Обычный файл
24
task.txt
Обычный файл
@@ -0,0 +1,24 @@
|
||||
Работа ведется на основе кода лабораторной работы № 1.
|
||||
|
||||
Структурировать программу при помощи функций:
|
||||
|
||||
Определить структуру Input для хранения исходных данных.
|
||||
Вынести ввод данных в функцию input_data().
|
||||
Вынести поиск минимума и максимума в функцию find_minmax().
|
||||
Вынести расчет количества чисел в корзинах в функцию make_histogram().
|
||||
Вынести отображение гистограммы в функцию show_histogram_text().
|
||||
Разделить программу на единицы трансляции:
|
||||
|
||||
main.cpp: основная программа;
|
||||
histogram.cpp: функции для расчетов;
|
||||
text.cpp: отображение гистограммы в виде текста.
|
||||
Написать программу с модульными тестами функции find_minmax().
|
||||
|
||||
Перевести программу на отображение гистограммы в формате SVG.
|
||||
|
||||
В результате должно быть две программы с частично общим кодом. Основная программа при запуске без параметров работает так же, как ЛР № 1 в базовом виде (не своего варианта), но выводит гистограмму в формате SVG. Вторая программа выполняет модульные тесты функции find_minmax().
|
||||
|
||||
Код должен быть загружен в репозитарий cs-lab34. Начальный коммит должен содержать код ЛР № 1 без изменений. Должны быть коммиты, фиксирующие выполнение пунктов задания, с номером и описанием пункта в первой строке сообщения к коммиту. Можно делать больше промежуточных коммитов на свое усмотрение.
|
||||
|
||||
Отчета не нужно.
|
||||
|
||||
Ссылка в новой задаче
Block a user