Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <iostream>
- #include <cstring>
- #include <fstream>
- /* koncepcja jest taka - jest wektor obiektow typu firma.
- kazdy z tych obiektow zawiera wektor sklepow.
- kazdy sklep zawiera wektor zabawek bo przyjmuje ze kazdy sklep, nawet tej samej firmy ma inny asortyment.
- */
- const int DEF_VAL=3;
- template<typename Y1>
- class Iterator; //zapowiedz klasy
- class Toy;
- class Shop;
- //klasa szablonowa typu wektor
- template<typename T1>
- class Vector{
- template<typename Y1>
- friend class Iterator;
- private:
- T1 *tab;
- int cnt;
- int size;
- public:
- Vector();
- Vector(int n);
- Vector(const Vector &a);
- ~Vector();
- template<typename U1>
- friend Vector<U1> operator+(const Vector<U1> &a, const Vector<U1> &b);
- template<typename U1>
- friend bool operator==(const Vector<U1> &a, const Vector<U1> &b);
- template<typename U1>
- friend bool operator!=(const Vector<U1> &a, const Vector<U1> &b);
- template<typename U1>
- friend std::ostream& operator<<(std::ostream &os, const Vector<U1> &a);
- template<typename U1>
- friend std::istream& operator>>(std::istream &is, Vector<U1> &a);
- void rescale(int n);
- void put(int n, T1 &a);
- void empty();
- T1& operator[](int index);
- Vector& operator+=(const Vector &a);
- Vector& operator=(const Vector &a);
- };
- template<typename Y1>
- class Iterator{
- private:
- Vector<Y1> *pt;
- int pos;
- public:
- Iterator(Vector<Y1> *p,int n) : pt(p), pos(0){};
- void getfromfile(std::ifstream &inFile, int n);
- void showpos(){std::cout<<pos<<std::endl;};
- void move(int n);
- void print();
- void add(Y1 &a);
- Vector<Shop> *getShop(int n); // to ma byc gdzies poza ta klasa bo klasa jest szablonowa
- Vector<Toy> *getToy(int n, int m); // to tez
- template<typename U1>
- Vector<Y1> operator*(){return *pt;};
- Iterator operator++(int); //postfix
- Iterator operator++(); //prefix
- Iterator operator--(int); //postfix
- Iterator operator--(); //prefix
- };
- class Toy{
- template<typename U1>
- friend class Iterator;
- private:
- float price;
- std::string toyname;
- public:
- Toy() : price(0), toyname("brak"){};
- Toy(float p, std::string t) : price(p), toyname(t){};
- friend bool operator!=(const Toy &a, const Toy &b);
- friend bool operator==(const Toy &a, const Toy &b);
- friend std::ostream& operator<<(std::ostream &os, const Toy &a);
- friend std::ifstream& operator>>(std::ifstream &is, Toy &a);
- };
- //klasa sklep
- class Shop{
- template<typename U1>
- friend class Iterator;
- private:
- Vector<Toy> toydata;
- std::string adress;
- public:
- Shop() : adress("brak"){};
- Shop(float p, std::string t, std::string a) : adress(a){};
- friend std::ostream& operator<<(std::ostream &os, const Shop &a);
- friend std::istream& operator>>(std::istream &is, Shop &a);
- friend bool operator!=(const Shop &a, const Shop &b);
- friend bool operator==(const Shop &a, const Shop &b);
- };
- //klasa firma
- class Company{
- template<typename U1>
- friend class Iterator;
- private:
- Vector<Shop> shopdata;
- std::string name;
- public:
- friend std::ostream& operator<<(std::ostream &os, const Company &a);
- friend std::istream& operator>>(std::istream &is, Company &a);
- };
- template<typename T1>
- Vector<T1>::Vector(){
- cnt=0;
- size=DEF_VAL;
- tab=new T1[size];
- }
- template<typename T1>
- Vector<T1>::Vector(int n){
- cnt=0;
- size=n;
- tab=new T1[size];
- }
- template<typename T1>
- Vector<T1>::Vector(const Vector &a){
- tab=new T1[a.size];
- for(int i=0; i<a.size; ++i)
- tab[i]=a.tab[i];
- cnt=a.size;
- }
- template<typename T1>
- Vector<T1>::~Vector(){
- delete []tab;
- }
- template<typename T1>
- Vector<T1> operator+(const Vector<T1> &a, const Vector<T1> &b){
- Vector<T1> c(a.size+b.size);
- for(int i=0; i<a.size; ++i)
- c.tab[i]=a.tab[i];
- for(int i=a.size; i<b.size; ++i)
- c.tab[i]=b.tab[i];
- return c;
- }
- template<typename T1>
- bool operator==(const Vector<T1> &a, const Vector<T1> &b){
- if(a.size!=b.size)
- return false;
- else{ //???
- for(int i=0; i<a.size; ++i)
- if(a.tab[i]!=b.tab[i])
- return false;
- }
- return true;
- }
- template<typename T1>
- bool operator!=(const Vector<T1> &a, const Vector<T1> &b){
- return !(a==b);
- }
- template<typename T1>
- std::ostream& operator<<(std::ostream &os, const Vector<T1> &a){
- for(int i=0; i<a.cnt; ++i){
- os << a.tab[i] <<std::endl;
- }
- return os;
- }
- template<typename T1>
- std::istream& operator>>(std::istream &is, Vector<T1> &a){
- for(int i=0; i<a.size; ++i){
- is >> a.tab[i];
- }
- return is;
- }
- template<typename T1>
- void Vector<T1>::rescale(int n){
- if(n<size){
- T1 *temp=new T1[n];
- for(int i=0; i<n; ++i)
- temp[i]=tab[i];
- delete []tab;
- tab=temp;
- size=n;
- cnt=n;
- }
- else{
- if(n==size)
- return;
- T1 *temp=new T1[n];
- for(int i=0; i<cnt; ++i)
- temp[i]=tab[i];
- delete []tab;
- tab=temp;
- size=n;
- }
- }
- template<typename T1>
- void Vector<T1>::put(int n, T1 &a){
- if(n>size){
- rescale(n);
- tab[n]=a;
- }
- else
- tab[n]=a;
- }
- template<typename T1>
- void Vector<T1>::empty(){
- delete []tab;
- tab=new T1[size];
- }
- template<typename T1>
- T1& Vector<T1>::operator[](int index){
- return tab[index];
- }
- template<typename T1>
- Vector<T1>& Vector<T1>::operator=(const Vector &a){
- if(*this==a)
- return *this;
- for(int i=0; i<a.cnt; ++i)
- tab[i]=a.tab[i];
- return *this;
- }
- template<typename Y1>
- void Iterator<Y1>::move(int n){
- if(n>pt->size){
- std::cout << "przekroczono rozmiar wektora"<<std::endl;
- return;
- }
- pos=n;
- }
- template<typename Y1>
- void Iterator<Y1>::print(){
- std::cout<<pt->tab[pos]<<std::endl;
- }
- template<typename Y1>
- void Iterator<Y1>::add(Y1 &a){
- if(pt->size>pt->cnt){
- pt->tab[(pt->cnt)]=a;
- ++(pt->cnt);
- }
- else{
- pt->rescale(pt->size +1);
- pt->tab[pt->size]=a;
- ++(pt->cnt);
- }
- }
- template<typename Y1>
- Iterator<Y1> Iterator<Y1>::operator++(int){
- if(pos==pt->size)
- return *this;
- Iterator<Y1> i=*this;
- ++pos;
- return i;
- }
- template<typename Y1>
- Iterator<Y1> Iterator<Y1>::operator++(){
- if(pos==pt->size)
- return *this;
- ++pos;
- return *this;
- }
- template<typename Y1>
- Iterator<Y1> Iterator<Y1>::operator--(int){
- if(pos==0)
- return *this;
- Iterator<Y1> i=*this;
- --pos;
- return i;
- }
- template<typename Y1>
- Iterator<Y1> Iterator<Y1>::operator--(){
- if(pos==0)
- return *this;
- --pos;
- return *this;
- }
- template<typename Y1>
- Vector<Shop> *Iterator<Y1>::getShop(int n){
- return &((pt->tab[n]).shopdata);
- }
- template<typename Y1>
- Vector<Toy> *Iterator<Y1>::getToy(int n, int m){
- return &(pt->tab[n].shopdata[m].toydata);
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement