Advertisement
uberzawadaog

projekt3_h

Oct 28th, 2014
205
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 7.54 KB | None | 0 0
  1. #include <iostream>
  2. #include <cstring>
  3. #include <fstream>
  4. /* koncepcja jest taka - jest wektor obiektow typu firma.
  5.    kazdy z tych obiektow zawiera wektor sklepow.
  6.    kazdy sklep zawiera wektor zabawek bo przyjmuje ze kazdy sklep, nawet tej samej firmy ma inny asortyment.
  7.  
  8.  */
  9. const int DEF_VAL=3;
  10.  
  11. template<typename Y1>
  12. class Iterator; //zapowiedz klasy
  13.  
  14. class Toy;
  15.  
  16. class Shop;
  17.  
  18.  
  19. //klasa szablonowa typu wektor
  20. template<typename T1>
  21. class Vector{
  22.  
  23.     template<typename Y1>
  24.     friend class Iterator;
  25.  
  26.     private:
  27.         T1 *tab;
  28.         int cnt;
  29.         int size;
  30.     public:
  31.         Vector();
  32.         Vector(int n);
  33.         Vector(const Vector &a);
  34.         ~Vector();
  35.  
  36.         template<typename U1>
  37.         friend Vector<U1> operator+(const Vector<U1> &a, const Vector<U1> &b);
  38.         template<typename U1>
  39.         friend bool operator==(const Vector<U1> &a, const Vector<U1> &b);
  40.         template<typename U1>
  41.         friend bool operator!=(const Vector<U1> &a, const Vector<U1> &b);
  42.         template<typename U1>
  43.         friend std::ostream& operator<<(std::ostream &os, const Vector<U1> &a);
  44.         template<typename U1>
  45.         friend std::istream& operator>>(std::istream &is, Vector<U1> &a);
  46.  
  47.  
  48.  
  49.         void rescale(int n);
  50.         void put(int n, T1 &a);
  51.         void empty();
  52.         T1& operator[](int index);
  53.         Vector& operator+=(const Vector &a);
  54.         Vector& operator=(const Vector &a);
  55. };
  56.  
  57.  
  58. template<typename Y1>
  59. class Iterator{
  60.  
  61.     private:
  62.         Vector<Y1> *pt;
  63.         int pos;
  64.     public:
  65.         Iterator(Vector<Y1> *p,int n) : pt(p), pos(0){};
  66.  
  67.         void getfromfile(std::ifstream &inFile, int n);
  68.         void showpos(){std::cout<<pos<<std::endl;};
  69.         void move(int n);
  70.         void print();
  71.         void add(Y1 &a);
  72.         Vector<Shop> *getShop(int n);  // to ma byc gdzies poza ta klasa bo klasa jest szablonowa
  73.         Vector<Toy> *getToy(int n, int m); // to tez
  74.         template<typename U1>
  75.         Vector<Y1> operator*(){return *pt;};
  76.         Iterator operator++(int); //postfix
  77.         Iterator operator++();    //prefix
  78.         Iterator operator--(int); //postfix
  79.         Iterator operator--();    //prefix
  80. };
  81.  
  82.  
  83. class Toy{
  84.  
  85.  
  86.     template<typename U1>
  87.     friend class Iterator;
  88.  
  89.     private:
  90.         float price;
  91.         std::string toyname;
  92.     public:
  93.         Toy() : price(0), toyname("brak"){};
  94.         Toy(float p, std::string t) : price(p), toyname(t){};
  95.         friend bool operator!=(const Toy &a, const Toy &b);
  96.         friend bool operator==(const Toy &a, const Toy &b);
  97.         friend std::ostream& operator<<(std::ostream &os, const Toy &a);
  98.         friend std::ifstream& operator>>(std::ifstream &is, Toy &a);
  99. };
  100.  
  101.  
  102. //klasa sklep
  103. class Shop{
  104.  
  105.  
  106.     template<typename U1>
  107.     friend class Iterator;
  108.  
  109.     private:
  110.         Vector<Toy> toydata;
  111.         std::string adress;
  112.     public:
  113.         Shop() : adress("brak"){};
  114.         Shop(float p, std::string t, std::string a) : adress(a){};
  115.         friend std::ostream& operator<<(std::ostream &os, const Shop &a);
  116.         friend std::istream& operator>>(std::istream &is, Shop &a);
  117.         friend bool operator!=(const Shop &a, const Shop &b);
  118.         friend bool operator==(const Shop &a, const Shop &b);
  119. };
  120.  
  121. //klasa firma
  122. class Company{
  123.  
  124.  
  125.     template<typename U1>
  126.     friend class Iterator;
  127.  
  128.     private:
  129.         Vector<Shop> shopdata;
  130.         std::string name;
  131.     public:
  132.         friend std::ostream& operator<<(std::ostream &os, const Company &a);
  133.         friend std::istream& operator>>(std::istream &is, Company &a);
  134.  
  135. };
  136.  
  137.  
  138. template<typename T1>
  139. Vector<T1>::Vector(){
  140.  
  141.     cnt=0;
  142.     size=DEF_VAL;
  143.     tab=new T1[size];
  144. }
  145.  
  146.  
  147. template<typename T1>
  148. Vector<T1>::Vector(int n){
  149.  
  150.     cnt=0;
  151.     size=n;
  152.     tab=new T1[size];
  153. }
  154.  
  155.  
  156. template<typename T1>
  157. Vector<T1>::Vector(const Vector &a){
  158.  
  159.     tab=new T1[a.size];
  160.     for(int i=0; i<a.size; ++i)
  161.         tab[i]=a.tab[i];
  162.     cnt=a.size;
  163. }
  164.  
  165.  
  166. template<typename T1>
  167. Vector<T1>::~Vector(){
  168.  
  169.     delete []tab;
  170. }
  171.  
  172.  
  173. template<typename T1>
  174. Vector<T1> operator+(const Vector<T1> &a, const Vector<T1> &b){
  175.  
  176.  
  177.     Vector<T1> c(a.size+b.size);
  178.  
  179.     for(int i=0; i<a.size; ++i)
  180.         c.tab[i]=a.tab[i];
  181.  
  182.     for(int i=a.size; i<b.size; ++i)
  183.         c.tab[i]=b.tab[i];
  184.  
  185.     return c;
  186.  
  187. }
  188.  
  189.  
  190. template<typename T1>
  191. bool operator==(const Vector<T1> &a, const Vector<T1> &b){
  192.  
  193.  
  194.     if(a.size!=b.size)
  195.         return false;
  196.     else{                                        //???
  197.         for(int i=0; i<a.size; ++i)
  198.             if(a.tab[i]!=b.tab[i])
  199.                 return false;
  200.         }
  201.     return true;
  202. }
  203.  
  204.  
  205.  
  206.  
  207. template<typename T1>
  208. bool operator!=(const Vector<T1> &a, const Vector<T1> &b){
  209.  
  210.     return !(a==b);
  211. }
  212.  
  213.  
  214. template<typename T1>
  215. std::ostream& operator<<(std::ostream &os, const Vector<T1> &a){
  216.  
  217.     for(int i=0; i<a.cnt; ++i){
  218.         os << a.tab[i] <<std::endl;
  219.     }
  220.     return os;
  221. }
  222.  
  223.  
  224. template<typename T1>
  225. std::istream& operator>>(std::istream &is, Vector<T1> &a){
  226.  
  227.     for(int i=0; i<a.size; ++i){
  228.         is >> a.tab[i];
  229.     }
  230.     return is;
  231. }
  232.  
  233.  
  234. template<typename T1>
  235. void Vector<T1>::rescale(int n){
  236.  
  237.     if(n<size){
  238.  
  239.         T1 *temp=new T1[n];
  240.         for(int i=0; i<n; ++i)
  241.             temp[i]=tab[i];
  242.  
  243.         delete []tab;
  244.         tab=temp;
  245.         size=n;
  246.         cnt=n;
  247.     }
  248.     else{
  249.         if(n==size)
  250.             return;
  251.  
  252.         T1 *temp=new T1[n];
  253.         for(int i=0; i<cnt; ++i)
  254.             temp[i]=tab[i];
  255.  
  256.         delete []tab;
  257.         tab=temp;
  258.         size=n;
  259.     }
  260. }
  261.  
  262.  
  263. template<typename T1>
  264. void Vector<T1>::put(int n, T1 &a){
  265.  
  266.     if(n>size){
  267.         rescale(n);
  268.         tab[n]=a;
  269.     }
  270.     else
  271.         tab[n]=a;
  272.  
  273. }
  274.  
  275.  
  276. template<typename T1>
  277. void Vector<T1>::empty(){
  278.  
  279.     delete []tab;
  280.     tab=new T1[size];
  281. }
  282.  
  283.  
  284. template<typename T1>
  285. T1& Vector<T1>::operator[](int index){
  286.  
  287.     return tab[index];
  288. }
  289.  
  290.  
  291. template<typename T1>
  292. Vector<T1>& Vector<T1>::operator=(const Vector &a){
  293.  
  294.     if(*this==a)
  295.         return *this;
  296.  
  297.     for(int i=0; i<a.cnt; ++i)
  298.         tab[i]=a.tab[i];
  299.     return *this;
  300. }
  301.  
  302.  
  303. template<typename Y1>
  304. void Iterator<Y1>::move(int n){
  305.  
  306.     if(n>pt->size){
  307.         std::cout << "przekroczono rozmiar wektora"<<std::endl;
  308.         return;
  309.     }
  310.  
  311.     pos=n;
  312. }
  313.  
  314.  
  315. template<typename Y1>
  316. void Iterator<Y1>::print(){
  317.  
  318.     std::cout<<pt->tab[pos]<<std::endl;
  319. }
  320.  
  321.  
  322.  
  323.  
  324. template<typename Y1>
  325. void Iterator<Y1>::add(Y1 &a){
  326.  
  327.     if(pt->size>pt->cnt){
  328.         pt->tab[(pt->cnt)]=a;
  329.         ++(pt->cnt);
  330.     }
  331.     else{
  332.         pt->rescale(pt->size +1);
  333.         pt->tab[pt->size]=a;
  334.         ++(pt->cnt);
  335.     }
  336.  
  337. }
  338.  
  339.  
  340. template<typename Y1>
  341. Iterator<Y1> Iterator<Y1>::operator++(int){
  342.  
  343.     if(pos==pt->size)
  344.         return *this;
  345.  
  346.     Iterator<Y1> i=*this;
  347.     ++pos;
  348.     return i;
  349. }
  350.  
  351.  
  352. template<typename Y1>
  353. Iterator<Y1> Iterator<Y1>::operator++(){
  354.  
  355.     if(pos==pt->size)
  356.         return *this;
  357.  
  358.     ++pos;
  359.     return *this;
  360. }
  361.  
  362.  
  363. template<typename Y1>
  364. Iterator<Y1> Iterator<Y1>::operator--(int){
  365.  
  366.     if(pos==0)
  367.         return *this;
  368.  
  369.     Iterator<Y1> i=*this;
  370.     --pos;
  371.     return i;
  372. }
  373.  
  374.  
  375. template<typename Y1>
  376. Iterator<Y1> Iterator<Y1>::operator--(){
  377.  
  378.     if(pos==0)
  379.         return *this;
  380.  
  381.     --pos;
  382.     return *this;
  383. }
  384.  
  385.  
  386. template<typename Y1>
  387. Vector<Shop> *Iterator<Y1>::getShop(int n){
  388.  
  389.    return &((pt->tab[n]).shopdata);
  390. }
  391.  
  392.  
  393. template<typename Y1>
  394. Vector<Toy> *Iterator<Y1>::getToy(int n, int m){
  395.  
  396.    return &(pt->tab[n].shopdata[m].toydata);
  397. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement