Guest User

Untitled

a guest
May 23rd, 2018
66
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 3.20 KB | None | 0 0
  1. #include <iostream>
  2. #include <string.h>
  3. using namespace std;
  4. #include "assoctab.h"
  5. #include <ctype.h>
  6.  
  7. /* Klasa tablicy asocjacyjnej KEY SENSITIVE */
  8. /* Konstruktor */
  9. assocTabKS::assocTabKS ()
  10. {
  11.     head = NULL;
  12. }
  13.  
  14. assocTabKS::assocTabKS (const assocTabKS & l)
  15. {
  16.     node *src, **dst;
  17.     head = NULL;
  18.     src = l.head;
  19.     dst = &head;
  20.    
  21.     try
  22.     {
  23.         while (src)
  24.         {
  25.             *dst = new node (*src);
  26.             src = src->next;
  27.             dst = &((*dst)->next);
  28.         }
  29.     }
  30.     catch (...)
  31.     {
  32.         clear ();
  33.         throw;
  34.     };
  35. }
  36. /** Konstruktor **/
  37.  
  38. /* Destruktor */
  39. assocTabKS::~assocTabKS ()
  40. {
  41.     clear ();
  42. }
  43. /** Destruktor **/
  44.  
  45. assocTabKS & assocTabKS::operator= (const assocTabKS & l)
  46. {
  47.     if (&l == this)
  48.         return *this;
  49.    
  50.     node *src, **dst;
  51.     head = NULL;
  52.     src = l.head;
  53.     dst = &head;
  54.    
  55.     try
  56.     {
  57.         while (src)
  58.         {
  59.             *dst = new node (*src);
  60.             src = src->next;
  61.             dst = &((*dst)->next);
  62.         }
  63.     }
  64.     catch (...)
  65.     {
  66.         clear ();
  67.         throw;
  68.     };
  69.    
  70.     return *this;
  71. }
  72.  
  73. void assocTabKS::clear ()
  74. {
  75.     while (head)
  76.     {
  77.         node *t = head->next;
  78.         delete head;
  79.         head = t;
  80.     }
  81. }
  82.  
  83. void assocTabKS::insert (const char *key, int value)
  84. {
  85.     node *nowy = new node (key);
  86.     nowy->next = head;
  87.     head = nowy;
  88.     head->val = value;
  89. }
  90.  
  91. /* Operacja swap do ewentualnego wykozystania */
  92. void assocTabKS::swap (assocTabKS & l)
  93. {
  94.     node *t = head;
  95.     head = l.head;
  96.     l.head = t;
  97. }
  98.  
  99. node * assocTabKS::returnHead()
  100. {
  101.     return head;
  102. }
  103.  
  104. node * assocTabKS::find (const char *key) const
  105. {
  106.     node * c = head;
  107.     while (c)
  108.     {
  109.         if (!strcmp (c->key, key))
  110.             return c;
  111.         c = c->next;
  112.     };
  113.     return NULL;
  114. }
  115.  
  116. int & assocTabKS::operator[] (const char *key)
  117. {
  118.     node *c = find (key);
  119.    
  120.     if (!c)
  121.     {
  122.         insert (key, 0);
  123.         c = head;
  124.     };
  125.    
  126.     return c->val;
  127. }
  128. /** Klasa tablicy asocjacyjnej KEY SENSITIVE **/
  129.  
  130. /* Klasa tablicy asocjacyjnej NON KEY SENSITIVE */
  131. /* Konstruktor */
  132. assocTabNKS::assocTabNKS ():assocTabKS ()
  133. {
  134. }
  135.  
  136. /*
  137. assocTabNKS::assocTabNKS (const assocTabNKS & l)
  138. {
  139.     node *src, **dst;
  140.     head = NULL;
  141.     src = l.head;
  142.     dst = &head;
  143.    
  144.     try
  145.     {
  146.         while (src)
  147.         {
  148.             *dst = new node (*src);
  149.             src = src->next;
  150.             dst = &((*dst)->next);
  151.         }
  152.     }
  153.     catch (...)
  154.     {
  155.         clear ();
  156.         throw;
  157.     };
  158. }*/
  159. /** Konstruktor **/
  160.  
  161. /*
  162. assocTabNKS & assocTabNKS::operator= (const assocTabNKS & l)
  163. {
  164.     if (&l == this)
  165.         return *this;
  166.    
  167.     node *src, **dst;
  168.     head = NULL;
  169.     src = l.head;
  170.     dst = &head;
  171.    
  172.     try
  173.     {
  174.         while (src)
  175.         {
  176.             *dst = new node (*src);
  177.             src = src->next;
  178.             dst = &((*dst)->next);
  179.         }
  180.     }
  181.     catch (...)
  182.     {
  183.         clear ();
  184.         throw;
  185.     };
  186.    
  187.     return *this;
  188. }
  189. */
  190. node *returnHead()
  191. {
  192.     return assocTabKS::returnHead();
  193. }
  194.  
  195. void assocTabNKS::insert (const char *k, int value)
  196. {
  197.     assocTabKS::insert(k, value);
  198. }
  199.  
  200. node * assocTabNKS::find (const char *k) const
  201. {
  202.     return assocTabKS::find(k);
  203. }
  204.  
  205. int & assocTabNKS::operator[] (const char *k)
  206. {
  207.     char *key = new char[strlen (k) + 1];
  208.     char zn;
  209.     int i = 0;
  210.    
  211.     while(*(k+i))
  212.     {
  213.         zn = *(k+i);
  214.         zn = (char)tolower(zn);
  215.         *(key+i) = zn;
  216.         i++;
  217.     }
  218.     *(key+i) = '\0';
  219.    
  220.     node* c = find(key);
  221.    
  222.     if (!c)
  223.     {
  224.         insert (key, 0);
  225.         c = assocTabKS::returnHead();
  226.     };
  227.  
  228.     delete [] key;
  229.    
  230.     return c->val;
  231. }
  232. /** Klasa tablicy asocjacyjnej NON KEY SENSITIVE **/
Add Comment
Please, Sign In to add comment