- •Содержание
- •Лабораторная работа №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.
Задача 2.
7. Глобальные функции для выполнения заданий.
typedef stack<Money> My_stack;
My_stack Create_stack(int n)
//создание стека, заполенного случайными данными
{
My_stack stack1;
int t;
Money a;
for (int i=0;i<n;i++)
{
t=rand()%10000;
a.setrub(t/100);
a.setkop(t%100);
stack1.push(a);
}
return stack1;
}
vector<Money> stack_to_vector(My_stack stack1)
//копирование стека в вектор
{
vector<Money> temp;
while (!stack1.empty())
{
temp.push_back(stack1.top());
stack1.pop();
}
return temp;
}
My_stack vector_to_stack(vector<Money> temp)
//копирование вектора в стек
{
My_stack stack1;
vector<Money>::reverse_iterator it1;
for(it1=temp.rbegin();it1!=temp.rend();it1++)
stack1.push(*it1);
return stack1;
}
void out_el(Money mon1) {cout<<mon1<<" ";}
void print_stack(My_stack& stack1)
//вывод элементов стека
{
vector<Money> temp=stack_to_vector(stack1);
for_each(temp.begin(),temp.end(),out_el);
cout<<endl;
}
Money min_el(My_stack &stack1)
//поиск минимального элемента
{
vector<Money> temp=stack_to_vector(stack1);
vector<Money>::iterator min;
min=min_element(temp.begin(),temp.end());
return *min;
}
void ins(My_stack& stack1,Money a,int n)
//добавление элемента a на позицию n
{
vector<Money> temp;
vector<Money>::reverse_iterator it1;
int i=1;
if (n>stack1.size()+1)
cout<<"No such position"<<endl;
else
{
while (!stack1.empty())
{
if (i++==n)
temp.push_back(a);
temp.push_back(stack1.top());
stack1.pop();
}
if ((i==n))
temp.push_back(a);
stack1=vector_to_stack(temp);
}
}
Money Avg(My_stack stack1)
//нахождение среднего арифметического
{
Money a(0,0);
int s=stack1.size();
while (!stack1.empty())
{
a+=stack1.top();
stack1.pop();
}
a/=s;
return a;
}
void Del_m(My_stack& stack1,Money& a)
//удаление элементов, равных a из стека
{
vector<Money> temp=stack_to_vector(stack1);
vector<Money>::iterator last;
vector<Money>::reverse_iterator it1;
last=remove(temp.begin(),temp.end(),a);
temp.erase(last,temp.end());
stack1=vector_to_stack(temp);
}
Money max_el(My_stack &stack1)
//поиск максимального элемента
{
vector<Money> temp=stack_to_vector(stack1);
vector<Money>::iterator max;
max=max_element(temp.begin(),temp.end());
return *max;
}
void add(My_stack &stack1,Money a)
//увеличение всех элементов стека на сумму a
{
vector<Money> temp=stack_to_vector(stack1);
vector<Money>::reverse_iterator it1;
transform(temp.begin(),temp.end(),temp.begin(),bind2nd(plus<Money>(),a));
stack1=vector_to_stack(temp);
}
bool eq_rub(Money mon1,int r){return mon1.getrub()==r;}
//проверяет, равно ли количество рублей числу r
8. Функция main()
void main()
{
char ch;
try
{
My_stack stack1;
vector<Money> temp;
vector<Money>::iterator imon1;
int n,r;
cout<<"Input the size of the stack"<<endl;
cin>>n;
stack1=Create_stack(n);
print_stack(stack1);
cout<<"After inserting max element"<<endl;
ins(stack1,max_el(stack1),1);
print_stack(stack1);
cout<<"After deleting min element"<<endl;
Del_m(stack1,min_el(stack1));
print_stack(stack1);
cout<<"Ascending sort"<<endl; //сортировка по возрастанию
temp=stack_to_vector(stack1);
sort(temp.begin(),temp.end());
stack1=vector_to_stack(temp);
print_stack(stack1);
cout<<"Descending sort"<<endl; //сортировка по убыванию
sort(temp.begin(),temp.end(),greater<Money>());
stack1=vector_to_stack(temp);
print_stack(stack1);
cout<<"Input rubles fo find"<<endl;
cin>>r;
imon1=find_if(temp.begin(),temp.end(),bind2nd(ptr_fun(eq_rub),r));
if (imon1==temp.end())
cout<<"No element with such rubles"<<endl;
else cout<<*imon1<<endl;
cout<<"With average added"<<endl;
add(stack1,Avg(stack1));
print_stack(stack1);
}
catch(int)
{
cout<<"Error";
}
cin>>ch;
}
9. Результат работы программы для задачи 2.
Input the size of the stack
11
57,05 44,64 69,62 93,58 14,78 57,24 91,69 65,00 63,34 84,67 0,41
After inserting max element
93,58 57,05 44,64 69,62 93,58 14,78 57,24 91,69 65,00 63,34 84,67 0,41
After deleting min element
93,58 57,05 44,64 69,62 93,58 14,78 57,24 91,69 65,00 63,34 84,67
Ascending sort
14,78 44,64 57,05 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 57,05 44,64 14,78
Input rubles fo find
57
57,24
With average added
160,41 160,41 158,52 151,50 136,45 131,83 130,17 124,07 123,88 111,47 81,61