- •Содержание
- •Лабораторная работа №3
- •Лабораторная работа №6
- •1. Постановка задачи:
- •Лабораторная работа №11
- •1. Постановка задачи.
- •Задача 1.
- •Задача 2.
- •5. Описание класса
- •Int getrub() {return rub;} //получение количества рублей
- •Int getkop() {return kop;} //получение количества копеек
- •Задача 3.
- •Задача 4.
- •Задача 5.
- •Лабораторная работа №13
- •1. Постановка задачи. Задача 1.
- •Задача 2.
- •Задача 3.
- •2. Описание класса
- •Int getrub() {return rub;} //получение количества рублей
- •Int getkop() {return kop;} //получение количества копеек
- •Задача 1.
- •Задача 2.
- •Задача 3.
Задача 1.
4. Глобальные функции для выполнения заданий.
My_vector Create_vector(int n)
//создание списка, заполенного случайными данными
{
My_vector vector1;
int t;
Money a;
for (int i=0;i<n;i++)
{
t=rand()%10000;
a.setrub(t/100);
a.setkop(t%100);
vector1.push_back(a);
}
return vector1;
}
void out_el(Money mon1) {cout<<mon1<<" ";}
void print_vector(My_vector vector1)
//вывод элементов вектора
{
for_each(vector1.begin(),vector1.end(),out_el);
cout<<endl;
}
Money min_el(My_vector &vector1)
//поиск минимального элемента
{
My_vector::iterator min;
min=min_element(vector1.begin(),vector1.end());
return *min;
}
void ins(My_vector& vector1,Money a,int n)
//добавление элемента a на позицию n
{
if (n>vector1.size()+1)
cout<<"No such position"<<endl;
else
{
My_vector::iterator imon1=vector1.begin()+n-1;
vector1.insert(imon1,a);
}
}
Money Avg(My_vector& vector1)
//нахождение среднего арифметического
{
My_vector::iterator imon1,itend;
Money a(0,0);
imon1=vector1.begin();
itend=vector1.end();
while (imon1!=itend)
a+=*(imon1++);
a/=vector1.size();
return a;
}
void Del_m(My_vector& vector1,Money& a)
//удаление элементов, равных a из вектора
{
My_vector::iterator last;
last=remove(vector1.begin(),vector1.end(),a);
vector1.erase(last,vector1.end());
}
Money max_el(My_vector &vector1)
//поиск максимального элемента
{
My_vector::iterator max;
max=max_element(vector1.begin(),vector1.end());
return *max;
}
void add(My_vector &vector1,Money a)
//увеличение всех элементов вектора на сумму a
{
transform(vector1.begin(),vector1.end(),vector1.begin(),bind2nd(plus<Money>(),a));
}
bool eq_rub(Money mon1,int r){return mon1.getrub()==r;}
5. Функция main()
void main()
{
char ch;
try
{
My_list list1;
My_list::iterator it1;
int n,m;
cout<<"Input the size of the list"<<endl;
cin>>n;
list1=Create_list(n);
print_list(list1);
cout<<"Input position to insert min element"<<endl;
cin>>n;
ins(list1,min_el(list1),n);
print_list(list1);
cout<<"After deleting elements greater than average"<<endl;
Del_m(list1,Avg(list1));
print_list(list1);
cout<<"Ascending sort"<<endl; //сортировка по возрастанию
list1.sort();
print_list(list1);
cout<<"Descending sort"<<endl; //сортировка по убыванию
list1.sort(greater<Money>());
print_list(list1);
cout<<"Input minutes fo find"<<endl;
cin>>m;
it1=find_if(list1.begin(),list1.end(),bind2nd(ptr_fun(eq_min),m));
if (it1==list1.end())
cout<<"No element with such minutes"<<endl;
else cout<<*it1<<endl;
cout<<"Multipliceted by max"<<endl;
mult(list1,max_el(list1));
print_list(list1);
}
catch(int)
{
cout<<"Error";
}
cin>>ch;
}
6. Результаты работы программы для задачи 1.
Input the size of the vector
9
0,41 84,67 63,34 65,00 91,69 57,24 14,78 93,58 69,62
After inserting max element
93,58 0,41 84,67 63,34 65,00 91,69 57,24 14,78 93,58 69,62
After deleting min element
93,58 84,67 63,34 65,00 91,69 57,24 14,78 93,58 69,62
Ascending sort
14,78 57,24 63,34 65,00 69,62 84,67 91,69 93,58 93,58
Descending sort
93,58 93,58 91,69 84,67 69,62 65,00 63,34 57,24 14,78
Input rubles fo find
63
63,34
With average added
163,96 163,96 162,07 155,05 140,00 135,38 133,72 127,62 85,16