Добавил:
Upload Опубликованный материал нарушает ваши авторские права? Сообщите нам.
Вуз: Предмет: Файл:
5 Вариант.doc
Скачиваний:
11
Добавлен:
17.09.2019
Размер:
227.84 Кб
Скачать

Задача 3.

10. Глобальные функции для выполнения заданий.

typedef set<Time> My_Set;

My_Set Create_set(int n)

//создание множества, заполенного случайными данными

{

My_Set set1;

int t;

Time a;

for (int i=0;i<n;i++)

{

t=rand()%1000;

a.setmin(t/60);

a.setsec(t%60);

set1.insert(a);

}

return set1;

}

void out_el(Time t1) {cout<<t1<<" ";}

void print_set(My_Set set1)

//вывод элементов множества

{

for_each(set1.begin(),set1.end(),out_el);

cout<<endl;

}

Time min_el(My_Set &set1)

//поиск минимального элемента

{

My_Set::iterator min;

min=min_element(set1.begin(),set1.end());

return *min;

}

void ins(My_Set& set1,Time a,int n)

//добавление элемента a на позицию n

{

if (n>set1.size()+1)

cout<<"No such position"<<endl;

else

{

My_Set::iterator it1=set1.begin();

for (int i=0;i<n-1;i++)

it1++;

set1.insert(it1,a);

}

}

Time Avg(My_Set& set1)

//нахождение среднего арифметического

{

My_Set::iterator it1,itend;

Time a(0,0);

it1=set1.begin();

itend=set1.end();

while (it1!=itend)

a+=*(it1++);

a/=set1.size();

return a;

}

void Del_m(My_Set& set1,Time& a)

//удаление элементов, больших a из множества

{

My_Set::iterator last;

last=remove_if(set1.begin(),set1.end(),bind2nd(greater<Time>(),a));

set1.erase(last,set1.end());

}

Time max_el(My_Set &set1)

//поиск максимального элемента

{

My_Set::iterator max;

max=max_element(set1.begin(),set1.end());

return *max;

}

void mult(My_Set &set1,Time a)

//домножение всех элементов множества на время a

{

transform(set1.begin(),set1.end(),set1.begin(),bind2nd(multiplies<Time>(),a));

}

bool eq_min(Time t1,int m){return t1.getmin()==m;}

//проверяет равно ли количество минут значению m

11. Функция main()

void main()

{

char ch;

try

{

My_Set set1;

My_Set::iterator it1;

int n,m;

cout<<"Input the size of the set"<<endl;

cin>>n;

set1=Create_set(n);

print_set(set1);

cout<<"Input position to insert min element"<<endl;

cin>>n;

ins(set1,min_el(set1),n);

print_set(set1);

cout<<"After deleting elements greater than average"<<endl;

Del_m(set1,Avg(set1));

print_set(set1);

cout<<"Ascending sort"<<endl; //сортировка по возрастанию

print_set(set1);

cout<<"Descending sort"<<endl; //сортировка по убыванию

for_each(set1.rbegin(),set1.rend(),out_el);

cout<<endl;

cout<<"Input minutes fo find"<<endl;

cin>>m;

it1=find_if(set1.begin(),set1.end(),bind2nd(ptr_fun(eq_min),m));

if (it1==set1.end())

cout<<"No element with such minutes"<<endl;

else cout<<*it1<<endl;

cout<<"Multiplicated by max"<<endl;

mult(set1,max_el(set1));

print_set(set1);

}

catch(int)

{

cout<<"Error";

}

cin>>ch;

}

12. Результат работы программы для задачи 3.

Input the size of the set

12

0:41 2:25 2:49 5:34 5:58 7:44 7:47 7:58 8:20 11:45 12:04 16:02

Input position to insert min element

9

0:41 2:25 2:49 5:34 5:58 7:44 7:47 7:58 8:20 11:45 12:04 16:02

After deleting elements greater than average

0:41 2:25 2:49 5:34 5:58

Ascending sort

0:41 2:25 2:49 5:34 5:58

Descending sort

5:58 5:34 2:49 2:25 0:41

Input minutes fo find

2

2:25

Multiplicated by max

244:38 865:10 1008:22 1992:52 2136:04