Другие языки программирования и технологии
помогите с прогой на с++ списки
Сформировать списки L1 и L2 из списка L по следующему правилу : в L1 поместить четные положительные элементы списка L, в L2-нечетные отрицательные элементы списка L .Посчитать количество компонентов в списках L1 и L2. заранее спасибо
#include <iostream>
#include <sstream>
#include <list>
using namespace std;
template <class T, bool Predicate(T x)>
void filter(const list<T> & source, list<T> & result)
{
for (list<T>::const_iterator i = source.begin(); i != source.end(); i++)
{
T item = *i;
if (Predicate(item))
result.push_back(item);
}
}
bool is_positive_even(int x)
{
return (x > 0) && (x % 2 == 0);
}
bool is_negative_odd(int x)
{
return (x < 0) && (x % 2 != 0);
}
template <class T>
void print_list(const string & name, const list<T> & list_)
{
cout << name << " (" << list_.size() << " items): ";
bool is_first_item = true;
for (list<T>::const_iterator i = list_.begin(); i != list_.end(); i++)
{
if (is_first_item)
is_first_item = false;
else
cout << " ";
cout << *i;
}
cout << endl;
}
void _tmain()
{
list<int> source;
bool done = false;
do
{
char buff[255];
cin.getline(buff, sizeof(buff) / sizeof(*buff));
string line(buff);
if (line.length() == 0)
done = true;
else
{
istringstream stream(line);
int item = 0;
stream >> item;
if (!stream.fail())
source.push_back(item);
}
} while (!done);
list<int> list1;
filter<int, is_positive_even>(source, list1);
list<int> list2;
filter<int, is_negative_odd>(source, list2);
print_list<int>("L", source);
print_list<int>("L1", list1);
print_list<int>("L2", list2);
}
#include <sstream>
#include <list>
using namespace std;
template <class T, bool Predicate(T x)>
void filter(const list<T> & source, list<T> & result)
{
for (list<T>::const_iterator i = source.begin(); i != source.end(); i++)
{
T item = *i;
if (Predicate(item))
result.push_back(item);
}
}
bool is_positive_even(int x)
{
return (x > 0) && (x % 2 == 0);
}
bool is_negative_odd(int x)
{
return (x < 0) && (x % 2 != 0);
}
template <class T>
void print_list(const string & name, const list<T> & list_)
{
cout << name << " (" << list_.size() << " items): ";
bool is_first_item = true;
for (list<T>::const_iterator i = list_.begin(); i != list_.end(); i++)
{
if (is_first_item)
is_first_item = false;
else
cout << " ";
cout << *i;
}
cout << endl;
}
void _tmain()
{
list<int> source;
bool done = false;
do
{
char buff[255];
cin.getline(buff, sizeof(buff) / sizeof(*buff));
string line(buff);
if (line.length() == 0)
done = true;
else
{
istringstream stream(line);
int item = 0;
stream >> item;
if (!stream.fail())
source.push_back(item);
}
} while (!done);
list<int> list1;
filter<int, is_positive_even>(source, list1);
list<int> list2;
filter<int, is_negative_odd>(source, list2);
print_list<int>("L", source);
print_list<int>("L1", list1);
print_list<int>("L2", list2);
}
Создаёшь итератор для списка L.Проходишь по списку. Если элемент чётный и положительный, то
L1.pushback(*iter);
если нечётный и отрицательный
L2.pushback(*iter);
Количество в списках L1 и L2
L1.size(); L2.size();
L1.pushback(*iter);
если нечётный и отрицательный
L2.pushback(*iter);
Количество в списках L1 и L2
L1.size(); L2.size();
а проблема-то в чем?
Похожие вопросы
- помогите составить прогу в паскале
- Помогите написать прогу!!!
- помогите с прогой в с++
- Помогите написать прогу на Паскале, экзамен горит, очень нужно..
- помогите найти прогу
- Помогите выбрать прогу, очень нужно!!!
- Помогите написать прогу на паскале
- Помогите найти прогу!! ! Очень срочно нужно!!!
- Помогите написать прогу на паскале!
- Помогите дописать прогу на паскале