what to do

Write your question here.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
  iostream cstdlib vector algorithm iterator list numeric math.h
 
struct gen
int a
gen(int cos)
a=cos

int operator ()()
return a--

struct parz
bool operator()(const int&a)
if(a%2==0)return true
else return false


vector<int>vec(20)
generate(vec.begin(),vec.end(),gen(50))
copy(,,ostream_iterator<int>(cout,""))
vector<int>::iterator p=remove_if(,,parz())
vec.erase(p,vec.end())



#include <iostream>
#include<algorithm>
#include<ctime>
#include<iterator>
#include<vector>
#include<cstdlib>
#include<set>

using namespace std;

class Gen
{
int min, max;
public:
Gen()
{
min=max=0;
}
Gen(int _min, int _max)
{
min=_min;
max=_max;
}
int operator()()
{
return rand()%(min-max)+min;
}
};

struct Dodaj
{
int a=0;
int operator()(int liczba)
{
a+=liczba;
return a;
}
};

bool parzyste(int liczba)
{
if(liczba%2==0) return false;
else return true;
}

int main(int argc, char*argv[])
{
srand(time(NULL));
vector<int> v1(20),v2(20),v3(20);
generate(v1.begin(),v1.end(),Gen(30,79));
copy(v1.begin(),v1.end(),ostream_iterator<int>(cout," "));
cout << "\nOd konca: ";
copy(v1.rbegin(),v1.rend(),ostream_iterator<int>(cout," "));
copy(v1.begin(),v1.end(),v2.begin());
cout << endl;
copy(v2.begin(),v2.end(),ostream_iterator<int>(cout," "));
cout << "\nDodajemy: ";
transform(v1.begin(),v1.end(),v3.begin(),Dodaj());
copy(v3.end()-1,v3.end(),ostream_iterator<int>(cout," "));
cout << "\nTylko parzyste: ";
vector<int>::iterator nk=remove_if(v1.begin(),v1.end(),parzyste);
v1.erase(nk,v1.end());
copy(v1.begin(),v1.end(),ostream_iterator<int>(cout, " "));
return 0;
}
//ZAD1

#include<iostream>

using namespace std;

template<typename T> // szblon zmiennej typu "T"
class Box
{
T*wsk;
public:
Box() : wsk (new T( ) )
{
cout << "Konstruktor domyslny zostal wywolany \n";
}
T& operator*() // wyluskanie
{
return *wsk;
}
explicit Box (T *w); // jawnie
const T&operator *() const
{
return *wsk;
}
Box(const Box &a)
{
wsk=new T;
*wsk=*a.wsk;
}
Box &operator = (const Box &a)
{
delete wsk; // pamietac o wyciekach !!
wsk= new T;
*wsk=*a.wsk;
return *this;
}
~Box()
{
delete wsk;
cout << "Element zostal usuniety\n";
}
T *operator ->()
{
return wsk;
}
const T *operator ->() const
{
return wsk;
}
};

struct Point // moze byc class tylko tak latwiej
{
double x,y;
};

int main(int argc, char *argv[])
{
Box<int> b1;
cout << *b1 << endl;
*b1 = 10;
cout << *b1 << endl;
Box<int> b2 = b1;
cout << *b1 << " " << *b2 << endl;
*b2 = 4;
cout << *b1 << " " << *b2 << endl;
Box<int> b3;
b3 = b2;
cout << *b2 << " " << *b3 << endl;
*b3 = 8;
cout << *b2 << " " << *b3 << endl;
Box<Point> point;
point->x = 1.0;
point->y = 2.0;
cout << point->x << " " << point->y << endl;
return 0;
}
//ZAD2

#include<iostream>

using namespace std;

template <typename T>
class Table
{
unsigned int n;
T*tab;
public:
Table():tab(0),n(0){};
Table(T*begin,T*end)
{
n=end-begin;
tab=new T[n];
for(unsigned int i=0;i<n;i++)
{
tab[i]=T(begin[i]);
}
}
Table(const Table&t)
{
n=t.n;
tab=new T[n];
for(unsigned int i=0;i<n;i++)
{
tab[i]=t.tab[i];
}
}
class Iterator // klasa w klasie
{
T* current;
public:
Iterator(T*wsk):current(wsk){} // nie kopiujemy
bool operator !=(const Iterator& it)
{
return current !=it.current;
}
Iterator operator ++()
{
current++;
return (*(this));
}
T &operator *() // wyluskiwanie
{
return *current;
}
Iterator operator ++(int) // operator postinkrementacji
{
Iterator cur(*this); // cur -> kopia obiektu
++(*this);
return cur;
}
};
Iterator begin()
{
return Iterator(tab);
}
Iterator end()
{
return Iterator(tab+n);
}
virtual ~Table()
{
delete[] tab;
tab=0;
n=0;
}
};

int main(int argc, char*argv[])
{
int tab[] = {1, 3, 4, 8};
Table<int> vec(tab, tab + 4);
for(Table<int>::Iterator iter = vec.begin(); iter != vec.end();++iter)
cout << *iter << endl; // wyluskanie
Table<int> vec2(vec);
for(Table<int>::Iterator iter = vec2.begin(); iter != vec2.end();iter++)
{
*iter = 1;
cout << *iter << endl;
}
for(Table<int>::Iterator iter = vec.begin(); iter != vec.end();++iter)
cout << *iter << endl;
return 0;
}
//ZAD3
#include<iostream>
#include<ctime>
#include<cstdlib>
#include<vector>
#include<algorithm>
#include<iterator>
#include<set>

using namespace std;

class Gen // ZAD A
{
int min, max;
public:
Gen()
{
min=max=0;
}
Gen(int min_, int max_)
{
min=min_;
max=max_;
}
int operator()()
{
return rand()%(min-max)+min; // losuje z przedzialu od min do max
}
};

struct Osoba
{
string nazwisko, imie;
int wiek;
};

struct wedlugNazwiska // funkcja porzadkujaca
{
bool operator()(const Osoba &s1, const Osoba &s2)
{
return s1.nazwisko>s2.nazwisko;
}
};

ostream& operator<<(ostream&out, const Osoba &os1)
{
out << os1.nazwisko << " " << os1.imie << " " << os1.wiek << "\n";
return out;
}

struct wedlugWieku
{
bool operator()(const Osoba &s1, const Osoba &s2)
{
return s1.wiek>s2.wiek;
}
};

struct czyPrzedzial
{
int min, max;
czyPrzedzial()
{
min=max=0;
}
czyPrzedzial(int min_, int max_)
{
min=min_;
max=max_;
}
bool operator()(const Osoba &os)
{
return (min<=os.wiek)&&(max>=os.wiek);
}
};

int main(int argc, char *argv[])
{
srand(time(NULL));
//zadA
/*int a,b;
// wypelnij(tab, tab+n, gen_m100_100()); // przykladowe wywolanie wypelnia tab losowymi
//int a=gen(); // przypisuje zmiennej a losowa liczbe tzw. przeladowanie operatora
cout << "Podaj a i b: \n";
cin >> a >> b;
vector<int> vec(10);
generate(vec.begin(),vec.end(), Gen(a,b));
copy(vec.begin(),vec.end(), ostream_iterator<int>(cout," ")); // ostream wypisywanie na ekran zamiast for*/

//zadB
Osoba t[] = {
{"Kowal", "Mateusz", 22},
{"Abacki", "Grzegorz",20},
{"Nowak", "Jan", 21}
};
set<Osoba, wedlugNazwiska> s(t,t+3);
copy(s.begin(),s.end(),ostream_iterator<Osoba>(cout, ""));
cout << endl;
vector<Osoba> vec(t, t+3);
sort(vec.begin(),vec.end(),wedlugWieku()); // (poczatek, koniec, obiekt funkcyjny)
copy(vec.begin(),vec.end(),ostream_iterator<Osoba>(cout, ""));
cout << "\n\n";
vec.erase(remove_if(vec.begin(),vec.end(), czyPrzedzial(20,21)),vec.end()); // programowanie funkcyjne
copy(vec.begin(),vec.end(),ostream_iterator<Osoba>(cout, ""));

return 0;
}
//ZAD4
#include<iostream>
#include<ctime>
#include<cstdlib>
#include<vector>
#include<algorithm>
#include<iterator>
#include<set>
#include<list>
#include<numeric>
#include<math.h>

using namespace std;

class Gen
{
int min, max;
public:
Gen()
{
min=max=0;
}
Gen(int min_, int max_)
{
min=min_;
max=max_;
}
int operator()()
{
return rand()%(min-max)+min;
}
};

bool sprawdz(int a)
{
if(a>26&&a<84) return false;
else return true;
}

struct punkt // do zad3
{
double x,y,z;
public:
punkt()
{
x=0;
y=0;
z=0;
}
friend ostream& operator<<(ostream&out , const punkt&p)
{
out << p.x <<" " << p.y << " " << p.z;
return out;
}
};

struct GenPunktow // do zad3
{
double min,max;
public:
GenPunktow()
{
min=0;
max=0;
}
GenPunktow(double min_, double max_)
{
min=min_;
max=max_;
}
punkt operator()()
{
punkt p;
p.x=min+((double)rand()/RAND_MAX)*(max-min);
p.y=min+((double)rand()/RAND_MAX)*(max-min);
p.z=min+((double)rand()/RAND_MAX)*(max-min);
return p;
}
};

struct obj
{
double wartosc;
friend ostream& operator <<(ostream &out, const obj&o)
{
out << o.wartosc;
return out;
}
};

struct Transf // funkcja transform
{
obj operator()(const punkt &p)
{
obj V;
V.wartosc=p.x*p.y*p.z;
return V;
}
};

struct AkObj // acumulate, punktor
{
double operator()(double prev, const obj&o)
{
return prev*o.wartosc;
}
};

struct usun
{
double zmienna;
usun(double &ob)
{
zmienna=ob;
}

bool operator()(const obj &o)
{
return zmienna > o.wartosc;
}
};

struct usun_warunek
{
double zmienna;
usun_warunek(double &ob)
{
zmienna=ob;
}
bool operator()(const punkt&p)
{
return zmienna >(p.x*p.y*p.z);
}
};


int main(int argc, char*argv[])
{
//zad1
srand(time(NULL));
cout << "ZAD1\n";
vector<int> vec(20);
generate(vec.begin(),vec.end(),Gen(30,79));
cout << "Wylosowane: ";
copy(vec.begin(),vec.end(), ostream_iterator<int>(cout," "));
cout << endl;
sort(vec.begin(),vec.end());
cout << "Posortowane: ";
copy(vec.begin(),vec.end(), ostream_iterator<int>(cout," "));
vector<int>::iterator nk=unique(vec.begin(),vec.end());
vec.erase(nk,vec.end());
cout << endl;
cout << "Bez pow: ";
copy(vec.begin(),vec.end(), ostream_iterator<int>(cout," "));
//zad2
cout << "\nZAD2\n";
vector<int> vect(20); // ma byc 200
generate(vect.begin(),vect.end(),Gen(1,200));
cout << "Wylosowane: ";
copy(vect.begin(),vect.end(), ostream_iterator<int>(cout," "));
cout << endl;
vector<int>::iterator nk2=remove_if(vect.begin(),vect.end(),sprawdz);
vect.erase(nk2,vect.end());
cout << "Usuniete: ";
copy(vect.begin(),vect.end(), ostream_iterator<int>(cout," "));
//zad3
cout << "\nZAD3\n";
vector<punkt> vecp(20);
generate(vecp.begin(),vecp.end(),GenPunktow(2.5,10.75));
cout<<"Wygenerowane Punkty: "<<endl;
copy(vecp.begin(),vecp.end(),std::ostream_iterator<punkt>(cout," | "));
list<obj> listaObj(20);
transform(vecp.begin(),vecp.end(),listaObj.begin(),Transf());
cout<<endl<<endl<<"Objetosc: "<<endl;
copy(listaObj.begin(),listaObj.end(),std::ostream_iterator<obj>(cout," | "));
double geo_sr=pow(accumulate(listaObj.begin(),listaObj.end(),1.0,AkObj()),1.0/listaObj.size());
cout <<endl<<"Srednia: " <<geo_sr<<endl;
listaObj.remove_if(usun(geo_sr));
vector<punkt>::iterator cos=remove_if(vecp.begin(),vecp.end(),usun_warunek(geo_sr));
vecp.erase(cos,vecp.end());
cout << endl<<"Usuniete punkty: "<<endl;
copy(vecp.begin(),vecp.end(),std::ostream_iterator<punkt>(cout," | "));
cout<<endl<<"Pozostale Objetosci: " <<endl;
copy(listaObj.begin(),listaObj.end(),ostream_iterator<obj>(cout," | "));

return 0;
}
//ZAD5
#include<iostream>
#include<ctime>
#include<cstdlib>
#include<vector>
#include<algorithm>
#include<iterator>
#include<set>
#include<utility> // pair
#include<list>

using namespace std;

//KOLOS PO ŚWIĘTACH !!

template<typename Iter1, typename Iter2, typename Pred>
Iter2 copy_if(Iter1 b1, Iter1 e1, Iter2 b2, Pred p)
{
while(b1!=e1)
{
if(p(*b1)) // jezeli true
{
*b2=*b1;
b2++;
}
b1++;
}
return b2;
}

template<typename It1, typename It2>
pair<It1,It2> match(It1 b1, It1 e1, It2 b2)
{
while(b1!=e1)
{
if(*b1==*b2)
return pair<It1, It2>(b1,b2);
b1++;
b2++;
}
return pair<It1,It2>(e1,b2);
}

template<typename It1, typename Pred>
void bublesort(It1 b1, It1 e1, Pred p) // babelkowe
{
It1 e2=e1;
e2--;
for(It1 i=b1;i!=e1;i++)
{
for(It1 j=b1;j!=e2;j++)
{
It1 x=j;
x++;
if(p(*j,*x)) // jezeli true
{
swap(*x,*j); // zamien
}
}
}
}

int main(int argc, char*argv[])
{
//ZAD1
int b[20], a[] = {3, 5, 4, 9, 1, 3, 2};
int* w = copy_if(a, a+7, b, bind2nd(greater<int>(), 3));
copy(b,w, ostream_iterator<int>(cout," "));
//ZAD2
int t[] = {3, 8, 2, 9, 2, 7, 2, 5, 8};
vector<int> v(t, t+4);
list<int> l(t+4, t+9);
pair<std::vector<int>::iterator,
list<int>::iterator> p;
p = match(v.begin(), v.end(), l.begin());
cout << *p.first << ' ' << *p.second << '\n';
//ZAD3
int t1[] = {3, 8, 2, 9};
list<int> v2(t1, t1+4);
bublesort(v2.begin(), v2.end(), greater<int>());
copy(v2.begin(),v2.end(), ostream_iterator<int>(cout," "));
return 0;
}
//Przykladowe_kol_2
#include <iostream>
#include <vector>
#include <algorithm>
#include <cstdlib>
#include <ctime>
#include <iterator>
#include <list>
#include <cmath>
using namespace std;

struct Parzyste
{
static int i;
int operator()()
{
i=i+2;
return i;
}
};
struct Iloczyn
{
static int i;
int operator()(int x)
{
return i=i*x;
}
};

bool Warunek(int x){return(x>=6 && x<=16);}
int Parzyste::i=2;
int Iloczyn::i=1;
int main()
{
vector<int> v(10);
generate(v.begin(), v.end(), Parzyste());
copy(v.begin(), v.end(), ostream_iterator<int>(cout, " "));
v.erase(remove_if(v.begin(), v.end(), Warunek),v.end());
cout<<endl;
copy(v.begin(), v.end(), ostream_iterator<int>(cout, " "));
vector<int> p(4);
transform(v.begin(), v.end(), p.begin(), Iloczyn());
cout<<endl;
copy(p.end()-1, p.end(), ostream_iterator<int>(cout, " "));
return 0;
}
//Przykladowe_kol_1
#include <iostream>
#include <vector>
#include <algorithm>
#include <cstdlib>
#include <ctime>
#include <iterator>
#include <list>
#include <cmath>
using namespace std;

struct Kula
{
int r;
};
ostream &operator<<(ostream &out, const Kula &k)
{
return out<<k.r;
}
struct Wieksze
{
bool operator()(Kula a, Kula b)
{
return (a.r>b.r);
}
};

int Pole (Kula a){return 4*M_PI*pow(a.r,2);}
struct Generuj
{
Generuj(){}
Kula operator()()
{
Kula temp;
int a=rand()%10+12;
temp.r=a==21?15:a;
return temp;
}
};

template<class ForwardIterator1, class ForwardIterator2, class War>
ForwardIterator2 search_iter ( ForwardIterator1 first1, ForwardIterator1 last1, ForwardIterator2 first2, War warunek)
{
while (first1!=last1)
{
if(warunek(*first1))
{
*first2=first1;
first2++;
}
first1++;
}
return first2;
}


int Wskaznik (vector<int>::iterator a){return *a;}

int main()
{
srand(time(NULL));
vector<Kula> k(10);
generate(k.begin(),k.end(), Generuj());
copy(k.begin(), k.end(), ostream_iterator<Kula>(cout, " "));
sort(k.begin(), k.end(), Wieksze());
cout<<endl;
copy(k.begin(), k.end(), ostream_iterator<Kula>(cout, " "));
list<int> l(10);
transform(k.begin(), k.end(), l.begin(), Pole);
cout<<endl;
copy(l.begin(), l.end(), ostream_iterator<int>(cout, " "));


int tab[] = {1, 2 ,3, 4};
vector<int> v(tab, tab + 4);
list<vector<int>::iterator> lst(4);
list<vector<int>::iterator>::iterator out = search_iter(v.begin(),v.end(), lst.begin(), bind2nd(greater<int>(),1));
cout<<endl;
transform(lst.begin(), out, ostream_iterator<int>(cout, " "), Wskaznik);

return 0;
}
//LOSOWE PARZYSTE
class GenParz
{
int min,max;
public:
GenParz()
{
min=max=0;
}
GenParz(int _min, int _max)
{
min=_min;
max=_max;
}
int operator()()
{
int a=rand()%(min-max)+min;
if(a%2==0) return a;
else return a+1;
}
};
struct parzyste
{static int i
int operator()()
i=i+2
return i
}
struct il
{static int i
int operator()(int x)

return i=i*x
}
bool war(int x){return(x>6&&x<14)}
int parzyste::i=2
int il::i=1;
Topic archived. No new replies allowed.