Skocz do zawartości
Zamknięcie Forum PC LAB

Szanowny Użytkowniku,

Informujemy, że za 30 dni tj. 30 listopada 2024 r. serwis internetowy Forum PC LAB zostanie zamknięty.

Administrator Serwisu Forum PC LAB - Ringier Axel Springer Polska sp. z o.o. z siedzibą w Warszawie: wypowiada całość usług Serwisu Forum PC LAB z zachowaniem miesięcznego okresu wypowiedzenia.

Administrator Serwisu Forum PC LAB informuje, że:

  1. Z dniem 29 listopada 2024 r. zakończy się świadczenie wszystkich usług Serwisu Forum PC LAB. Ważną przyczyną uzasadniającą wypowiedzenie jest zamknięcie Serwisu Forum PC LAB
  2. Dotychczas zamowione przez Użytkownika usługi Serwisu Forum PC LAB będą świadczone w okresie wypowiedzenia tj. do dnia 29 listopada 2024 r.
  3. Po ogłoszeniu zamknięcia Serwisu Forum od dnia 30 października 2024 r. zakładanie nowych kont w serwisie Forum PC LAB nie będzie możliwe
  4. Wraz z zamknięciem Serwisu Forum PC LAB, tj. dnia 29 listopada 2024 r. nie będzie już dostępny katalog treści Forum PC LAB. Do tego czasu Użytkownicy Forum PC LAB mają dostęp do swoich treści w zakładce "Profil", gdzie mają możliwość ich skopiowania lub archiwizowania w formie screenshotów.
  5. Administrator danych osobowych Użytkowników - Ringier Axel Springer Polska sp. z o.o. z siedzibą w Warszawie zapewnia realizację praw podmiotów danych osobowych przez cały okres świadczenia usług Serwisu Forum PC LAB. Szczegółowe informacje znajdziesz w Polityce Prywatności

Administrator informuje, iż wraz z zamknięciem Serwisu Forum PC LAB, dane osobowe Użytkowników Serwisu Forum PC LAB zostaną trwale usunięte ze względu na brak podstawy ich dalszego przetwarzania. Proces trwałego usuwania danych z kopii zapasowych może przekroczyć termin zamknięcia Forum PC LAB o kilka miesięcy. Wyjątek może stanowić przetwarzanie danych użytkownika do czasu zakończenia toczących się postepowań.

Temat został przeniesiony do archiwum

Ten temat przebywa obecnie w archiwum. Dodawanie nowych odpowiedzi zostało zablokowane.

Cabalerio

[C++] Tablica asocjacyjna

Rekomendowane odpowiedzi

Muszę napisać klasę odpowiedzialną za implementację tablicy asocjacyjnej Non key sensitive i key sensitive w jednym poprzez dziedziczenie. Mam do tego użyć funkcji wirtualnych. Program ma więc mniej więcej działać tak:

 

assoctab a;
a["ala"] = 10;
a["mala"] = 5;

cout << a["ala"] << a["mala"];

 

No i tam jeszcze dodatkowe ficzery typu operator przypisania itd. Jak do tej pory udało mi się zrobić idealnie(tak mi się wydaje) działającą klasę Key sensitive, ale mam problem z Non key sensitive, gdyż mam pokazać działanie dziedziczenia i funkcji wirtualnych. Sam napisałem cos takiego:

 

assoctab.h:


#include <string.h>

struct node
{
node *next;
char *key;
int val;

/* Tworzymy strukture */
node (const char *k):next (NULL)
{
	key = new char[strlen (k) + 1];
	strcpy (key, k);
};

node (const node & s):next (NULL)
{
	if (s.key == NULL)
		key = NULL;
	else
	{
		key = new char[strlen (s.key) + 1];
		strcpy (key, s.key);
	}

	val=s.val;
};
/** Tworzymy strukture **/

/* Destruktor */
~node ()
{
	delete[]key;
}
/** Destruktor **/

private:
	/* Zabezpieczamy sie przed automatycznym operatorem przypisania */		
	node & operator= (const node &);
};

class assocTabNKS;

/* Klasa tablicy asocjacyjnej KEY SENSITIVE */
class assocTabKS
{
private:

	node *head;



	void insert (const char *key, int value);
	void clear ();
	node *find (const char *key) const;

public:

	assocTabKS ();
	assocTabKS (const assocTabKS & l);
	assocTabKS & operator= (const assocTabKS & l);
	~assocTabKS ();
	void swap (assocTabKS & l);
	int &operator[] (const char *);

	/* Metoda zaprzyjazniona */
	friend ostream & operator<< (ostream & stream, assocTabKS & l)
	{
		node * c = l.head;
		while (c)
		{
			stream << c->val << " ";
			c = c->next;
		};

		return stream;
	};
};
/** Klasa tablicy asocjacyjnej KEY SENSITIVE **/

/* Klasa tablicy asocjacyjnej NON KEY SENSITIVE */
class assocTabNKS : public assocTabKS
{
private:

	void insert (const char *k, int value);
	//void clear ();
	node *find (const char *k) const;

public:

	assocTabNKS ();
	assocTabNKS (const assocTabNKS & l);
	assocTabNKS & operator= (const assocTabNKS & l);
	//~assocTabNKS ();
	//void swap (assocTabNKS & l);
	int &operator[] (const char *k);

	/* Metoda zaprzyjazniona */
	friend ostream & operator<< (ostream & stream, assocTabNKS & l)
	{

	};
};
/** Klasa tablicy asocjacyjnej NON KEY SENSITIVE **/

 

assoctab.cpp(przepraszam za bałagan z komentarzami, ale już sam nie wiem co jest mi potrzebne a co nie ;/):

#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;
}

assocTabKS::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 **/

/* Destruktor */
/*
assocTabNKS::~assocTabNKS ()
{
clear ();
}*/
/** Destruktor **/
/*
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;
}
*/
/*
void assocTabNKS::clear ()
{
while (head)
{
	node *t = head->next;
	delete head;
	head = t;
}
}
*/
void assocTabNKS::insert (const char *k, int value)
{
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';

assocTabKS::insert(key, value);

delete [] key;
}

/* Operacja swap do ewentualnego wykozystania *//*
void assocTabNKS::swap (assocTabNKS & l)
{
node *t = head;
head = l.head;
l.head = t;
}*/

assocTabNKS::node * assocTabNKS::find (const char *k) const
{
assocTabKS::find(key);
}

int & assocTabNKS::operator[] (const char *k)
{
int val;
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';

assocTabNKS::operator[](key);

delete [] key;

}
/** Klasa tablicy asocjacyjnej NON KEY SENSITIVE **/

 

I wyskakują mi errory których nie wiem jak się pozbyć:

g++ testassoc.cpp -o testassoc
In file included from testassoc.cpp:3:0:
assoctab.cpp:99:1: error: ‘node’ in ‘class assocTabKS’ does not name a type
assoctab.cpp: In member function ‘void assocTabNKS::insert(const char*, int)’:
assoctab.cpp:83:6: error: ‘void assocTabKS::insert(const char*, int)’ is private
assoctab.cpp:218:31: error: within this context
assoctab.cpp: At global scope:
assoctab.cpp:231:1: error: ‘node’ in ‘class assocTabNKS’ does not name a type

 

Proszę o pomoc to jest bardzo pilne. :)

Udostępnij tę odpowiedź


Odnośnik do odpowiedzi
Udostępnij na innych stronach
assoctab.cpp:83:6: error: ‘void assocTabKS::insert(const char*, int)’ is private

 

insert() w assocTabKS Powinno być przynajmniej protected zamiast private, skoro assocTabNKS jest klasą pochodną i ma korzystać z tej metody.

 

assoctab.cpp:231:1: error: ‘node’ in ‘class assocTabNKS’ does not name a type

 

assoctab.cpp:99:1: error: ‘node’ in ‘class assocTabKS’ does not name a type

 

node nie jest częścią tych klas, więc zapis :: jest bez sensu, usuń to assocTabNKS:: i assocTabKS:: sprzed node i będzie ok.

 

Ogółem to ma być tak:

 

node * assocTabNKS::find (const char *k) const

{

return assocTabKS::find(k);

}

Udostępnij tę odpowiedź


Odnośnik do odpowiedzi
Udostępnij na innych stronach

Udało mi się już skompilować to albo mam niestety naruszenie ochrony pamięci. Wklejam juz troche bardziej poukładany kod:

 

assoctab.h:


#include <string.h>

struct node
{
node *next;
char *key;
int val;

/* Tworzymy strukture */
node (const char *k):next (NULL)
{
	key = new char[strlen (k) + 1];
	strcpy (key, k);
};

node (const node & s):next (NULL)
{
	if (s.key == NULL)
		key = NULL;
	else
	{
		key = new char[strlen (s.key) + 1];
		strcpy (key, s.key);
	}

	val=s.val;
};
/** Tworzymy strukture **/

/* Destruktor */
~node ()
{
	delete[]key;
}
/** Destruktor **/

private:
	/* Zabezpieczamy sie przed automatycznym operatorem przypisania */		
	node & operator= (const node &);
};

class assocTabNKS;

/* Klasa tablicy asocjacyjnej KEY SENSITIVE */
class assocTabKS
{
private:
	node *head;

protected:	
	void clear ();
	node *find (const char *key) const;
	void insert (const char *key, int value);
	void swap (assocTabKS & l);

public:

	assocTabKS ();
	assocTabKS (const assocTabKS & l);
	assocTabKS & operator= (const assocTabKS & l);
	~assocTabKS ();
	int &operator[] (const char *);

	/* Metoda zaprzyjazniona */
	friend ostream & operator<< (ostream & stream, assocTabKS & l)
	{
		node * c = l.head;
		while (c)
		{
			stream << c->val << " ";
			c = c->next;
		};

		return stream;
	};
};
/** Klasa tablicy asocjacyjnej KEY SENSITIVE **/

/* Klasa tablicy asocjacyjnej NON KEY SENSITIVE */
class assocTabNKS : public assocTabKS
{
private:


protected:	
	//void clear ();
	node *find (const char *k) const;
	void insert (const char *k, int value);
	//void swap (assocTabKS & l);

public:

	assocTabNKS ();
	assocTabNKS (const assocTabNKS & l);
	assocTabNKS & operator= (const assocTabNKS & l);
	//~assocTabNKS ();
	int &operator[] (const char *k);

	/* Metoda zaprzyjazniona */
	friend ostream & operator<< (ostream & stream, assocTabNKS & l)
	{

	};
};
/** Klasa tablicy asocjacyjnej NON KEY SENSITIVE **/

 

assoctab.cpp:

#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::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;
}
*/

void assocTabNKS::insert (const char *k, int value)
{
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';

assocTabKS::insert(key, value);

delete [] key;
}

node * assocTabNKS::find (const char *k) const
{
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';

return assocTabKS::find(key);
}

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';

return assocTabNKS::operator[](key);

delete [] key;

}
/** Klasa tablicy asocjacyjnej NON KEY SENSITIVE **/

 

testassoc.cpp:

#include <iostream>
using namespace std;
#include "assoctab.cpp"


int
main ()
{
assocTabKS tab1;
tab1["Darek"] = 5;
tab1["darek"] = 10;
cout << "1." << tab1 << endl;
tab1["janek"] = tab1["darek"];
tab1["Janek"] = tab1["janek"];
cout << "2." << tab1 << endl;

assocTabNKS tab2;
tab2["Darek"] = 5;
//int val = tab2["darek"];
//cout << "3. " << val << endl;
}

 

Naruszenie następuje dopiero w linijce:

tab2["Darek"] = 5;

Ma ktoś jakiś pomysł jak przerobić mój kod ?

 

@EDIT: Już udało mi się naprawić wszystko, jak by ktoś potrzebował coś takiego to piszcie PW.

Udostępnij tę odpowiedź


Odnośnik do odpowiedzi
Udostępnij na innych stronach

  • Ostatnio przeglądający   0 użytkowników

    Brak zarejestrowanych użytkowników przeglądających tę stronę.

×
×
  • Dodaj nową pozycję...