Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <iostream>
- #include <string.h>
- using namespace std;
- #include "assoctab.h"
- #include <ctype.h>
- /* Klasa tablicy asocjacyjnej KEY SENSITIVE */
- /* Konstruktor */
- assocTabKS::assocTabKS ()
- {
- head = NULL;
- }
- assocTabKS::assocTabKS (const assocTabKS & l)
- {
- node *src, **dst;
- head = NULL;
- src = l.head;
- dst = &head;
- try
- {
- while (src)
- {
- *dst = new node (*src);
- src = src->next;
- dst = &((*dst)->next);
- }
- }
- catch (...)
- {
- clear ();
- throw;
- };
- }
- /** Konstruktor **/
- /* Destruktor */
- assocTabKS::~assocTabKS ()
- {
- clear ();
- }
- /** Destruktor **/
- assocTabKS & assocTabKS::operator= (const assocTabKS & l)
- {
- if (&l == this)
- return *this;
- node *src, **dst;
- head = NULL;
- src = l.head;
- dst = &head;
- try
- {
- while (src)
- {
- *dst = new node (*src);
- src = src->next;
- dst = &((*dst)->next);
- }
- }
- catch (...)
- {
- clear ();
- throw;
- };
- return *this;
- }
- void assocTabKS::clear ()
- {
- while (head)
- {
- node *t = head->next;
- delete head;
- head = t;
- }
- }
- void assocTabKS::insert (const char *key, int value)
- {
- node *nowy = new node (key);
- nowy->next = head;
- head = nowy;
- head->val = value;
- }
- /* Operacja swap do ewentualnego wykozystania */
- void assocTabKS::swap (assocTabKS & l)
- {
- node *t = head;
- head = l.head;
- l.head = t;
- }
- node * assocTabKS::returnHead()
- {
- return head;
- }
- node * assocTabKS::find (const char *key) const
- {
- node * c = head;
- while (c)
- {
- if (!strcmp (c->key, key))
- return c;
- c = c->next;
- };
- return NULL;
- }
- int & assocTabKS::operator[] (const char *key)
- {
- node *c = find (key);
- if (!c)
- {
- insert (key, 0);
- c = head;
- };
- return c->val;
- }
- /** Klasa tablicy asocjacyjnej KEY SENSITIVE **/
- /* Klasa tablicy asocjacyjnej NON KEY SENSITIVE */
- /* Konstruktor */
- assocTabNKS::assocTabNKS ():assocTabKS ()
- {
- }
- /*
- assocTabNKS::assocTabNKS (const assocTabNKS & l)
- {
- node *src, **dst;
- head = NULL;
- src = l.head;
- dst = &head;
- try
- {
- while (src)
- {
- *dst = new node (*src);
- src = src->next;
- dst = &((*dst)->next);
- }
- }
- catch (...)
- {
- clear ();
- throw;
- };
- }*/
- /** Konstruktor **/
- /*
- assocTabNKS & assocTabNKS::operator= (const assocTabNKS & l)
- {
- if (&l == this)
- return *this;
- node *src, **dst;
- head = NULL;
- src = l.head;
- dst = &head;
- try
- {
- while (src)
- {
- *dst = new node (*src);
- src = src->next;
- dst = &((*dst)->next);
- }
- }
- catch (...)
- {
- clear ();
- throw;
- };
- return *this;
- }
- */
- node *returnHead()
- {
- return assocTabKS::returnHead();
- }
- void assocTabNKS::insert (const char *k, int value)
- {
- assocTabKS::insert(k, value);
- }
- node * assocTabNKS::find (const char *k) const
- {
- return assocTabKS::find(k);
- }
- int & assocTabNKS::operator[] (const char *k)
- {
- char *key = new char[strlen (k) + 1];
- char zn;
- int i = 0;
- while(*(k+i))
- {
- zn = *(k+i);
- zn = (char)tolower(zn);
- *(key+i) = zn;
- i++;
- }
- *(key+i) = '\0';
- node* c = find(key);
- if (!c)
- {
- insert (key, 0);
- c = assocTabKS::returnHead();
- };
- delete [] key;
- return c->val;
- }
- /** Klasa tablicy asocjacyjnej NON KEY SENSITIVE **/
Add Comment
Please, Sign In to add comment