Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- Zad3
- #include <iostream>
- #include <cmath>
- using namespace std;
- class punkt
- {
- float _x, _y;
- public:
- punkt ()
- {
- _x=_y=0;
- }
- punkt (float a, float b)
- {
- _x=a;
- _y=b;
- }
- float& x()
- {
- return _x;
- }
- float& y()
- {
- return _y;
- }
- float odleglosc(punkt p)
- {
- return sqrt(pow(p.x(),2)-2*p.x()*_x+pow(_x,2)+pow(p.y(),2)-2*p.y()*_y+pow(_y,2));
- }
- };
- class wielobok
- {
- punkt *tab;
- int n;
- public:
- wielobok()
- {
- tab=0;
- n=0;
- }
- wielobok(punkt *p, punkt *k)
- {
- n=k-p;
- tab = new punkt [n];
- int i=0;
- while (p!=k)
- {
- tab[i]=*p;
- i++; p++;
- }
- }
- wielobok (wielobok &k)
- {
- n=k.n;
- tab = new punkt [n];
- for (int i=0; i<n; ++i)
- tab[i]=k.tab[i];
- }
- wielobok operator= (const wielobok &w)
- {
- if(this!= &w)
- {
- n=w.n;
- if (tab!=0) delete [] tab;
- tab = new punkt [n];
- for (int i=0; i<n; ++i)
- tab[i]=w.tab[i];
- }
- return *this;
- }
- ~wielobok ()
- {
- if (tab!=0) delete [] tab;
- }
- float obwod()
- {
- float suma=0;
- for(int i=0; i<n-1; ++i)
- {
- suma+=tab[i].odleglosc(tab[i+1]);
- }
- return suma;
- }
- punkt &Punkt(int a)
- {
- return tab[a];
- }
- void punkty(punkt *p, punkt *k)
- {
- n=k-p;
- tab = new punkt [n];
- int i=0;
- while (p!=k)
- {
- tab[i]=*p;
- i++; p++;
- }
- }
- int ilosc() { return n;}
- punkt operator[] (int a)
- {
- return tab[a];
- }
- wielobok operator+ (const punkt &p)
- {
- tab = new punkt [n];
- for
- }
- };
- bool operator== (wielobok &w1, wielobok &w2)
- {
- if (abs (w1.obwod()-w2.obwod()) < 0.000001)
- return true;
- else
- return false;
- }
- int main ()
- {
- punkt p(2, 3);
- cout << p.x() << ' ' << p.y() << '\n';
- p.x() = 1;
- p.y() = 1;
- cout << p.x() << ' ' << p.y() << '\n';
- cout << p.odleglosc(punkt()) << '\n';
- punkt t[] = { punkt(0, 1), punkt(0, 0), punkt(1, 0), punkt(1, 1) };
- wielobok w1(t, t+4);
- cout << w1.obwod() << '\n';
- w1.Punkt(1) = punkt(0.5, 0.5);
- cout << w1.obwod() << '\n';
- wielobok w2;
- w2.punkty(t, t+3);
- cout << w2.obwod() << '\n';
- for (int i = 0; i < w2.ilosc(); ++i)
- cout << w2.Punkt(i).x() << ' ' << w2.Punkt(i).y() << '\n';
- return 0;
- }
- Zad4
- #include <iostream>
- #include <cstring>
- using namespace std;
- class adres
- {
- int n;
- adres *tab;
- public:
- string miasto, ulica, kod;
- int nr_domu;
- adres ()
- {
- nr_domu = 0;
- }
- adres (string m, string k, string u, int nr)
- {
- miasto = m;
- kod = k;
- ulica = u;
- nr_domu = nr;
- }
- const string &m() const
- {
- return miasto;
- }
- const string &k() const
- {
- return kod;
- }
- const string &u() const
- {
- return ulica;
- }
- const int &nr() const
- {
- return nr_domu;
- }
- };
- ostream& operator<< (ostream& o, const adres &a)
- {
- o<<a.m()<<", "<<a.k()<<", "<<a.u()<<", "<<a.nr();
- return o;
- }
- int main ()
- {
- adres* wsk = new adres("Czestochowa", "42-200", "Dabrowskiego", 73);
- cout << *wsk << '\n';
- adres a1(*wsk);
- delete wsk;
- const adres* wsk1 = new adres("Warszawa", "00-950", "Mysliwiecka", 357);
- cout << a1 << '\n';
- cout << *wsk1 << '\n';
- adres a2;
- cout << a2 << '\n';
- a2 = a1;
- cout << a2 << '\n';
- /*osoba o("Jan", "Kos", 25, *wsk1);
- cout << o << '\n';
- osoba o1(o);
- cout << o1 << '\n';
- osoba o2;
- cout << o2 << '\n';
- o2 = o1;
- cout << o2 << '\n';
- delete wsk1;*/
- return 0;
- }
- Zad5
- #include <iostream>
- #include <cmath>
- using namespace std;
- class point
- {
- float tab[3];
- public:
- point()
- {
- tab[0]=tab[1]=tab[2]=0;
- }
- point (float x, float y, float z)
- {
- tab[0]=x;
- tab[1]=y;
- tab[2]=z;
- }
- point (double tab1[3])
- {
- tab[0]=tab1[0];
- tab[1]=tab1[1];
- tab[2]=tab1[2];
- }
- float& x()
- {
- return tab[0];
- }
- float& y()
- {
- return tab[1];
- }
- float& z()
- {
- return tab[2];
- }
- float& operator[] (int n)
- {
- return tab[n];
- }
- const float& operator[] (int n) const
- {
- return tab[n];
- }
- float distance (point p)
- {
- return sqrt(pow(p.x()-tab[0],2)+pow(p.y()-tab[1],2));
- }
- const float distance (point p) const
- {
- return sqrt(pow(p.x()-tab[0],2)+pow(p.y()-tab[1],2));
- }
- };
- point operator+ (const point &a, const point &b)
- {
- point tmp;
- for (int i=0; i<3; ++i)
- tmp[i]=a[i]+b[i];
- return tmp;
- }
- point operator- (const point &a, const point &b)
- {
- point tmp;
- for (int i=0; i<3; ++i)
- tmp[i]=a[i]-b[i];
- return tmp;
- }
- point operator* (const point &a, float v)
- {
- point tmp;
- for (int i=0; i<3; ++i)
- tmp[i]=a[i]*v;
- return tmp;
- }
- point operator* (float v, const point &a)
- {
- return a*v;
- }
- ostream& operator<< (ostream& o, const point &p)
- {
- o<<p;
- return o;
- }
- int main ()
- {
- double x[2][3] = {{1.0, 1.0, 1.0},
- {1.0, 2.0, 3.0}};
- point p1(x[0]), p2(x[1]);
- const point p3(0.4, 0.2, 0.1);
- cout << p1 << ", " << p2 << '\n';
- cout << p3[0] << ' ' << p3[1] << ' ' << p3[2] << '\n';
- cout << p1.distance(point()) << ", "<< p3.distance(p1) << '\n';
- cout << p1 + p2 << ", " << p1 - p3 << '\n';
- cout << 3.14 * p2 << ", " << p2 * 3.14 << '\n';
- /*cout << (p1 < p3) << ", " << (p1 == point(1.0, 1.0, 1.0)) << '\n';
- cin >> p1;
- cout << p1 << '\n';*/
- return 0;
- }
- Zad6
- #include <iostream>
- #include <fstream>
- using namespace std;
- class punkt
- {
- float _x,_y;
- public:
- punkt ()
- {
- _x=_y=0;
- }
- punkt (float a, float b)
- {
- a=_x;
- b=_y;
- }
- float& x()
- {
- return _x;
- }
- float& y()
- {
- return _y;
- }
- const float& x() const
- {
- return _x;
- }
- const float& y() const
- {
- return _y;
- }
- };
- class polygon
- {
- string line;
- int n;
- punkt *punkty;
- int *pol;
- public:
- polygon()
- {
- punkty = 0;
- pol = 0;
- n = 0;
- }
- ~polygon()
- {
- if (punkty!=0)
- delete [] punkty;
- if (pol!=0)
- delete [] pol;
- }
- polygon (polygon &a)
- {
- n = a.n;
- punkty = new punkt [n];
- pol = new int [n];
- for (int i=0; i<n; ++i)
- {
- punkty[i] = a.punkty[i];
- pol[i] = a.pol[i];
- }
- }
- polygon operator= (const polygon &p)
- {
- if (this != &p)
- {
- n = p.n;
- punkty = new punkt [n];
- pol = new int [n];
- for (int i=0; i<n; ++i)
- {
- punkty[i] = p.punkty[i];
- pol[i] = p.pol[i];
- }
- }
- return *this;
- }
- punkt operator[] (int i)
- {
- return punkty[i];
- }
- void wczytaj (ifstream& plik, int i, float x, float y)
- {
- plik.open("plik.txt");
- while (!plik.eof())
- {
- getline(plik,line);
- if (line == "[NUMBER OF NODES]")
- {
- plik>>n;
- punkty = new punkt [n];
- pol = new int [n];
- }
- if (line == "[POLYGON]")
- {
- for (int j=0; j<n; ++j)
- plik >> pol[j];
- }
- if (line == "[NODES]")
- {
- for (int j=0; j<n; ++j)
- {
- plik >> n >> x >> y;
- punkty[i-1].x() = x;
- punkty[i-1].y() = y;
- }
- }
- }
- }
- };
- int main ()
- {
- return 0;
- }
- Zad7
- #include <iostream>
- #include <cstring>
- #include <vector>
- #include <algorithm>
- using namespace std;
- class Osoba
- {
- string _imie, _nazwisko;
- int _wiek;
- public:
- Osoba()
- {
- _wiek=0;
- }
- Osoba (string im, string na, int w)
- {
- _imie=im;
- _nazwisko=na;
- _wiek=w;
- }
- string& imie()
- {
- return _imie;
- }
- string& nazwisko()
- {
- return _nazwisko;
- }
- int& wiek ()
- {
- return _wiek;
- }
- const string& imie() const
- {
- return _imie;
- }
- const string& nazwisko() const
- {
- return _nazwisko;
- }
- const int& wiek () const
- {
- return _wiek;
- }
- };
- bool f_sort (const Osoba& o1, const Osoba& o2)
- {
- return o1.nazwisko() < o2.nazwisko();
- }
- ostream& operator<< (ostream& os, const Osoba &o)
- {
- os<<o.imie()<<" "<<o.nazwisko()<<" "<<o.wiek()<<endl;
- return os;
- }
- int main ()
- {
- vector <Osoba> tab;
- Osoba os1;
- for (int i=0; i<5; ++i)
- {
- cout<<"podaj imie"<<endl;
- cin>>os1.imie();
- cout<<"podaj nazwisko"<<endl;
- cin>>os1.nazwisko();
- cout<<"podaj wiek"<<endl;
- cin>>os1.wiek();
- tab.push_back(os1);
- }
- for (unsigned int i=0; i<tab.size(); ++i)
- cout<<tab[i];
- cout<<endl;
- sort (tab.begin(), tab.end(), f_sort);
- for (unsigned int i=0; i<tab.size(); ++i)
- cout<<tab[i];
- return 0;
- }
- Zad8
- #include <iostream>
- #include <cstring>
- using namespace std;
- class Adres
- {
- string _miasto, _ulica;
- public:
- Adres() {} //konstr dom.
- string& miasto() //metoda dostepowa, dzieki niej uzyskujemy dostep do prywatnych pul
- {
- return _miasto;
- }
- string& ulica()
- {
- return _ulica;
- }
- const string& miasto() const //metoda dostepowa, dzieki niej uzyskujemy dostep do prywatnych pul + consty
- {
- return _miasto;
- }
- const string& ulica() const
- {
- return _ulica;
- }
- };
- ostream& operator<< (ostream& o, const Adres &a) //dzieki temu wyswietla elementy klasy adres w mainie poprzez cout<<nazwa obiektu
- {
- o<<a.miasto()<<" "<<a.ulica()<<endl;
- return o;
- }
- class Przedmiot
- {
- string _nazwa;
- int _ocena;
- public:
- Przedmiot() {}
- string& nazwa () //metoda dostepowa, dzieki niej uzyskujemy dostep do prywatnych pul
- {
- return _nazwa;
- }
- int& ocena ()
- {
- return _ocena;
- }
- const string& nazwa () const
- {
- return _nazwa;
- }
- const int& ocena () const
- {
- return _ocena;
- }
- };
- ostream& operator<< (ostream& o, const Przedmiot p) //ostream jest typem "<<" tak jak "+" dla int
- {
- o<<p.nazwa()<<" "<<p.ocena()<<endl;
- return o;
- }
- class Osoba
- {
- string _imie, _nazwisko;
- int n; //public
- Adres *adresy;
- public:
- Osoba()
- {
- adresy=0;
- n=0;
- }
- string& imie () //metoda dostepowa, dzieki niej uzyskujemy dostep do prywatnych pul
- {
- return _imie;
- }
- string& nazwisko ()
- {
- return _nazwisko;
- }
- const string& imie () const
- {
- return _imie;
- }
- const string& nazwisko () const
- {
- return _nazwisko;
- }
- ostream& operator<< (ostream& o)
- {
- o<<imie()<<" "<<nazwisko()<<" ";
- for(int i=0; i<n; ++i)
- {
- o<<adresy[i];
- }
- o<<endl;
- return o;
- }
- Osoba (const Osoba &a) //konstruktor kopiujacy, kopiuje ...
- {
- n=a.n;
- adresy = new Adres [n];
- for (int i=0; i<n; ++i)
- adresy[i]=a.adresy[i];
- }
- Osoba operator= (const Osoba &a) // operator przypisania
- {
- if (this != &a)
- {
- n=a.n;
- adresy = new Adres [n];
- for (int i=0; i<n; ++i)
- adresy[i]=a.adresy[i];
- }
- return *this;
- }
- ~Osoba()
- {
- if (adresy!=0)
- delete [] adresy;
- }
- };
- class Student : public Osoba
- {
- Przedmiot *przedmioty;
- int n;
- public:
- Student()
- {
- n=0;
- }
- Student (const Student &s):Osoba(s)
- {
- n=s.n;
- przedmioty = new Przedmiot [n];
- for (int i=0; i<n; ++i)
- przedmioty[i]=s.przedmioty[i];
- }
- Student& operator= (const Student &s)
- {
- Osoba :: operator=(s);
- if (this != &s)
- {
- n=s.n;
- przedmioty = new Przedmiot [n];
- for (int i=0; i<n; ++i)
- przedmioty[i]=s.przedmioty[i];
- }
- return *this;
- }
- ~Student()
- {
- if(przedmioty !=0)
- delete [] przedmioty;
- }
- float srednia(float p)
- {
- for (int i=0; i<n; ++i)
- {
- p+=przedmioty[i].ocena();
- }
- p=p/n;
- return p;
- }
- };
- int main()
- {
- Adres a;
- a.miasto()="Czestochowa";
- a.ulica()="ALNMP";
- cout<<a;
- Przedmiot p;
- p.nazwa()="polski";
- p.ocena()=2;
- Przedmiot p1;
- p1.nazwa()="mata";
- p1.ocena()=5;
- cout<<p;
- return 0;
- }
- Zad9
- #include <iostream>
- #include <vector>
- using namespace std;
- class operacja
- {
- string _nazwa;
- int _wart;
- public:
- operacja ()
- {
- int _wart=0;
- }
- string& nazwa()
- {
- return _nazwa;
- }
- const string& nazwa() const
- {
- return _nazwa;
- }
- int& wart()
- {
- return _wart;
- }
- const int& wart()const
- {
- return _wart;
- }
- };
- ostream& operator<< (ostream& o, const operacja &op)
- {
- o<<op.nazwa()<<" "<<op.wart();
- return o;
- }
- class robot
- {
- int _id;
- public:
- robot ()
- {
- _id=0;
- }
- int& id ()
- {
- return _id;
- }
- const& id () const
- {
- return _id;
- }
- virtual void praca() {}
- };
- class podajnik : public robot
- {
- public:
- void praca ()
- {
- cout<<"pracuje podajnik nr: "<<id();
- }
- };
- class robot1R : public robot
- {
- public:
- vector <operacja> tab;
- operacja o;
- robot1R () {}
- robot1R (robot &id, int b, int e)
- {
- for (int i=b; i<=e; ++i)
- tab.push_back(o);
- }
- void praca()
- {
- cout<<"pracuje robot jednoramienny o nr: "<<id();
- for (int i=0; i<tab.size(); ++i)
- {
- cout<<tab[i];
- }
- }
- };
- class robot2R : public robot
- {
- public:
- vector <operacja> tab1;
- vector <operacja> tab2;
- operacja o;
- robot2R () {}
- robot1R (robot &id, int b1, int e1, int b2, int e2)
- {
- for (int i=b1; i<=e1; ++i)
- tab1.push_back(o);
- for (int i=b2; i<=e2; ++i)
- tab2.push_back(o);
- }
- void praca()
- {
- cout<<"pracuje robot dwuramienny o nr: "<<id();
- for (int i=0; i<tab1.size(); ++i)
- {
- cout<<tab1[i];
- }
- for (int i=0; i<tab2.size(); ++i)
- {
- cout<<tab2[i];
- }
- }
- };
- int main ()
- {
- operacja o[] = {
- operacja("w lewo", 12), operacja("obrot", 8),
- operacja("w gore", 32), operacja("obrot", 10),
- operacja("w prawo", 5), operacja("w dol", 25),
- operacja("obrot", 32), operacja("w gore", 21),
- operacja("obrot", 11), operacja("w prawo", 6)
- };
- robot* linia[] = {
- new podajnik(0), new robot1R(1, o, o+3),
- new podajnik(2), new robot2R(3, o+2, o+5, o+3, o+6),
- new podajnik(4), new robot2R(5, o, o+4, o+2, o+6),
- new podajnik(6), new robot1R(7, o+7, o+10),
- new podajnik(8)
- };
- wykonaj(linia, linia+9);
- return 0;
- }
- ZAD 8
- #include <iostream>
- #include <string>
- using namespace std;
- class osoba
- {
- string _nazwisko;
- int _liczba_lat;
- public:
- osoba()
- {
- }
- osoba(string nazwisko, int liczba_lat)
- {
- _nazwisko = nazwisko;
- _liczba_lat = liczba_lat;
- }
- string & nazwisko()
- {
- return _nazwisko;
- }
- int & liczba_lat()
- {
- return _liczba_lat;
- }
- const string & nazwisko()const
- {
- return _nazwisko;
- }
- const int & liczba_lat()const
- {
- return _liczba_lat;
- }
- void pokaz()
- {
- cout <<" "<< _nazwisko <<" ";
- cout <<" "<< _liczba_lat<<" ";
- }
- };
- class pracownik : public osoba
- {
- string _stanowisko;
- double _placa;
- public:
- pracownik()
- {
- }
- pracownik(string stanowisko, double placa, string nazwisko, int liczba_lat) : osoba(nazwisko,liczba_lat)
- {
- _stanowisko = stanowisko;
- _placa = placa;
- }
- string & stanowisko ()
- {
- return _stanowisko;
- }
- double & placa ()
- {
- return _placa;
- }
- const string & stanowisko ()const
- {
- return _stanowisko;
- }
- const double & placa ()const
- {
- return _placa;
- }
- void pokaz()
- {
- osoba::pokaz();
- cout<<" "<<_stanowisko<<" ";
- cout<<" "<<_placa<<" "<<
- ndl;
- }
- };
- int main()
- {
- osoba os("Dolas", 26);
- os.pokaz();
- const pracownik pr1("Dyzma", 35, "mistrz", 1250.0);
- cout << pr1.nazwisko() << pr1.liczba_lat();
- cout << pr1.stanowisko() << pr1.placa();
- pracownik pr2(pr1);
- pr2.pokaz();
- pracownik pr3("Kos", 45, "kierownik", 2260.0);
- pr3.pokaz();
- pr3 = pr2;
- pr3.pokaz();
- osoba* w = &os;
- w->pokaz();
- w = &pr3;
- w->pokaz();
- static_cast<pracownik*>(w)->pokaz();
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement