Apr 15, 2015 at 7:13am UTC
//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;
}