» »

C++ programi

C++ programi

error101 ::

Kdo ve rešiti ta dva programa?

1 program:
Napišite program v izbranem programskem jeziku, ki bo napolnil Vnos iz tipkovnice - dvodimenzionalno polje poljubnih dimenzij [m x n]. Nato izpišite tiste člene v polju, ki imajo enak index - kar pomeni tam kjer je m=n. ( napr. [1,1], [2,2] in tako do [m,m].

2 program:
Izdelaj eno dinamično podatkovno strukturo (s pomočjo kazalcev), sklad, vrsta ali seznam.
Sklad je struktura tipa LIFO (last in first out) - zadnji naložen podatek se najprej dvigne.
Vrsta je struktura tipa FIFO (first in first out) - prvi vloženi podatek se prvi dvigne.
V seznam pa lahko podatke vstavljaš na poljubno mesto in tudi jemlješ lahko s poljubnega mesta.

Za strukturo, ki si ga izbral napiši ustrezne funkcije za vstavljanje, brisanje in čitanje podatkov. Posamezni element strukture lahko vsebuje poljubni podatek(e).

error101 ::

hvala za odgovore že vnaprej
LP

Tutankhamun ::

na hitr 1. naloga, drugo pa kasnej napišem
const int WIDTH = 4;
const int HEIGHT = 4;

char tabela [HEIGHT][WIDTH];
char *ptr = tabela [0];

for (int i = 0; i < HEIGHT * WIDTH; i++)
{
	cin >> *(ptr+i);
}

for (int j = 0; j < HEIGHT && j < WIDTH; j++)
{
	cout << tabela [j][j];
}
AMD Phenom QUAD 9950 Black Edition, 8GB

Zgodovina sprememb…

Tutankhamun ::

Uf, evo neki kode še za 2. nalogo. Mau sm zakompliciru, pa tut ne vem če dela lih 100%, ampak nekak zgleda da neki čara...
// Tvoji podatki
struct DATA
{
	DATA (int x) { a = x; }
	int a;
};

class CStorageData
{
public:
	CStorageData () : m_prev (NULL), m_next (NULL) {}

public:
	DATA			*m_data;
		
	CStorageData	*m_next;
	CStorageData	*m_prev;
};

class CStorage
{
public:
	CStorage () : m_first (NULL), m_last (NULL), m_count (0) {}

	void InsertAt (DATA *data, UINT index)
	{
		if (!m_count)
		{
			InsertFirst (data);
			m_count++;
			return;
		}

		if (index == m_count)
		{
			Append (data);
			return;
		}

		CStorageData *currStorAtIndex = this->GetElement (index);
		CStorageData *newStor = new CStorageData ();

		if (currStorAtIndex->m_prev == NULL)
		{
			m_first = newStor;
		}
		else
		{
			newStor->m_prev = currStorAtIndex->m_prev;
		}

		if (currStorAtIndex->m_prev != NULL)
		{
			currStorAtIndex->m_prev->m_next = newStor;
		}
		
		currStorAtIndex->m_prev = newStor;
		newStor->m_next = currStorAtIndex;

		newStor->m_data = data;

		m_count++;
	}

	void Append (DATA *data)
	{
		if (!m_count)
		{
			InsertFirst (data);
			m_count++;
			return;
		}

		CStorageData *newStor = new CStorageData ();
		newStor->m_data = data;

		m_last->m_next = newStor;
		newStor->m_prev = m_last;
		m_last = newStor;
		m_count++;
	}

	DATA* RemoveAt (UINT index)
	{
		DATA *data;
		if (m_count == 1 && index == 0)
		{
			data = m_first->m_data;
			delete m_first;
			m_first = NULL;
			m_last = NULL;
			m_count--;
			return data;
		}

		CStorageData *currStorAtIndex = this->GetElement (index);

		data = currStorAtIndex->m_data;

		if (currStorAtIndex->m_prev == NULL)
		{
			currStorAtIndex->m_next->m_prev = NULL;
			m_first = currStorAtIndex->m_next;
			delete currStorAtIndex;
		}
		else if (currStorAtIndex->m_next == NULL)
		{
			currStorAtIndex->m_prev->m_next = NULL;
			m_last = currStorAtIndex->m_prev;
			delete currStorAtIndex;
		}
		else
		{
			currStorAtIndex->m_prev->m_next = currStorAtIndex->m_next;
			currStorAtIndex->m_next->m_prev = currStorAtIndex->m_prev;
			delete currStorAtIndex;
		}

		m_count--;
		return data;
	}

private:
	CStorageData* GetElement (UINT index)
	{
		if (index >= m_count) throw "index out of range";

		CStorageData *storData = m_first;

		for (UINT i = 0; i < index; i++)
			storData = storData->m_next;

		return storData;
	}

	void InsertFirst (DATA *data)
	{
		m_first = m_last = new CStorageData ();
		m_first->m_data = data;
	}

private:
	CStorageData	*m_first;
	CStorageData	*m_last;

protected:
	UINT		m_count;
};

class LIFO : CStorage
{
public:
	LIFO () {}

	void Push (DATA *data)
	{
		CStorage::Append (data);
	}

	DATA* Pop ()
	{
		return CStorage::RemoveAt (CStorage::m_count-1);
	}

	int Size () { return CStorage::m_count; }
};

class FIFO : CStorage
{
public:
	FIFO () {}

	void Push (DATA *data)
	{
		CStorage::Append (data);
	}

	DATA* Pop ()
	{
		return CStorage::RemoveAt (0);
	}

	int Size () { return CStorage::m_count; }
};

class CCReg : CStorage
{
public:
	CCReg () {}

	void InsertAt (DATA *data, UINT index)
	{
		CStorage::InsertAt (data, index);
	}

	DATA* RemoveAt (UINT index)
	{
		return CStorage::RemoveAt (index);
	}

	int Size () { return CStorage::m_count; }
};

int main(int argc, char* argv[])
{
	UINT i;
	DATA *data;

/* LIFO test */
	LIFO lifo;

	for (i = 0; i < 10; i++)
		lifo.Push (new DATA (i));

	while (lifo.Size ())
	{
		data = lifo.Pop ();
		cout << data->a << endl;
		delete data;
	}

/* end LIFO test */

/* FIFO test */
	FIFO fifo;

	for (i = 0; i < 10; i++)
		fifo.Push (new DATA (i));

	while (fifo.Size ())
	{
		data = fifo.Pop ();
		cout << data->a << endl;
		delete data;
	}

/* end FIFO test */

/* seznam test */
	CCReg reg;

	reg.InsertAt (new DATA (0), 0);
	reg.InsertAt (new DATA (1), 0);
	reg.InsertAt (new DATA (2), 1);
	reg.InsertAt (new DATA (3), 0);
	reg.InsertAt (new DATA (4), 2);
	reg.InsertAt (new DATA (5), 4);

	data = reg.RemoveAt (4);
	cout << data->a << endl;
	delete data;
	data = reg.RemoveAt (2);
	cout << data->a << endl;
	delete data;
	data = reg.RemoveAt (1);
	cout << data->a << endl;
	delete data;
	data = reg.RemoveAt (0);
	cout << data->a << endl;
	delete data;
	data = reg.RemoveAt (1);
	cout << data->a << endl;
	delete data;
	data = reg.RemoveAt (0);
	cout << data->a << endl;
	delete data;

/* end seznam test */
	return 0;
}
AMD Phenom QUAD 9950 Black Edition, 8GB

Tutankhamun ::

Ok neki sm pozabu prej :D
// Tvoji podatki
struct DATA
{
	DATA (int x) { a = x; }
	int a;
};

template <class T>
class CStorageData
{
public:
	CStorageData () : m_prev (NULL), m_next (NULL) {}

public:
	T	m_data;
		
	CStorageData	*m_next;
	CStorageData	*m_prev;
};

template <class T>
class CStorage
{
public:
	CStorage () : m_first (NULL), m_last (NULL), m_count (0) {}

	void InsertAt (T data, UINT index)
	{
		if (!m_count)
		{
			InsertFirst (data);
			m_count++;
			return;
		}

		if (index == m_count)
		{
			Append (data);
			return;
		}

		CStorageData<T> *currStorAtIndex = this->GetElement (index);
		CStorageData<T> *newStor = new CStorageData<T> ();

		if (currStorAtIndex->m_prev == NULL)
		{
			m_first = newStor;
		}
		else
		{
			newStor->m_prev = currStorAtIndex->m_prev;
		}

		if (currStorAtIndex->m_prev != NULL)
		{
			currStorAtIndex->m_prev->m_next = newStor;
		}
		
		currStorAtIndex->m_prev = newStor;
		newStor->m_next = currStorAtIndex;

		newStor->m_data = data;

		m_count++;
	}

	void Append (T data)
	{
		if (!m_count)
		{
			InsertFirst (data);
			m_count++;
			return;
		}

		CStorageData<T> *newStor = new CStorageData<T> ();
		newStor->m_data = data;

		m_last->m_next = newStor;
		newStor->m_prev = m_last;
		m_last = newStor;
		m_count++;
	}

	T RemoveAt (UINT index)
	{
		T data;
		if (m_count == 1 && index == 0)
		{
			data = m_first->m_data;
			delete m_first;
			m_first = NULL;
			m_last = NULL;
			m_count--;
			return data;
		}

		CStorageData<T> *currStorAtIndex = this->GetElement (index);

		data = currStorAtIndex->m_data;

		if (currStorAtIndex->m_prev == NULL)
		{
			currStorAtIndex->m_next->m_prev = NULL;
			m_first = currStorAtIndex->m_next;
			delete currStorAtIndex;
		}
		else if (currStorAtIndex->m_next == NULL)
		{
			currStorAtIndex->m_prev->m_next = NULL;
			m_last = currStorAtIndex->m_prev;
			delete currStorAtIndex;
		}
		else
		{
			currStorAtIndex->m_prev->m_next = currStorAtIndex->m_next;
			currStorAtIndex->m_next->m_prev = currStorAtIndex->m_prev;
			delete currStorAtIndex;
		}

		m_count--;
		return data;
	}

private:
	CStorageData<T>* GetElement (UINT index)
	{
		if (index >= m_count) throw "index out of range";

		CStorageData<T> *storData = m_first;

		for (UINT i = 0; i < index; i++)
			storData = storData->m_next;

		return storData;
	}

	void InsertFirst (T data)
	{
		m_first = m_last = new CStorageData<T> ();
		m_first->m_data = data;
	}

private:
	CStorageData<T>	*m_first;
	CStorageData<T>	*m_last;

protected:
	UINT		m_count;
};

template <class T>
class LIFO : CStorage <T>
{
public:
	LIFO () {}

	void Push (T data)
	{
		CStorage<T>::Append (data);
	}

	DATA* Pop ()
	{
		return CStorage<T>::RemoveAt (CStorage<T>::m_count-1);
	}

	int Size () { return CStorage<T>::m_count; }
};

template <class T>
class FIFO : CStorage <T>
{
public:
	FIFO () {}

	void Push (T data)
	{
		CStorage<T>::Append (data);
	}

	T Pop ()
	{
		return CStorage<T>::RemoveAt (0);
	}

	int Size () { return CStorage<T>::m_count; }
};

template <class T>
class CList : CStorage <T>
{
public:
	CList () {}

	void InsertAt (T data, UINT index)
	{
		CStorage<T>::InsertAt (data, index);
	}

	T RemoveAt (UINT index)
	{
		return CStorage<T>::RemoveAt (index);
	}

	int Size () { return CStorage<T>::m_count; }
};

int main(int argc, char* argv[])
{
	UINT i;
	DATA *data;

/* LIFO test */
	LIFO <DATA*> lifo;

	for (i = 0; i < 10; i++)
		lifo.Push (new DATA (i));

	while (lifo.Size ())
	{
		data = lifo.Pop ();
		cout << data->a << endl;
		delete data;
	}

/* end LIFO test */

/* FIFO test */
	FIFO <DATA*> fifo;

	for (i = 0; i < 10; i++)
		fifo.Push (new DATA (i));

	while (fifo.Size ())
	{
		data = fifo.Pop ();
		cout << data->a << endl;
		delete data;
	}

/* end FIFO test */

/* list test */
	CList <DATA*> list;

	list.InsertAt (new DATA (0), 0);
	list.InsertAt (new DATA (1), 0);
	list.InsertAt (new DATA (2), 1);
	list.InsertAt (new DATA (3), 0);
	list.InsertAt (new DATA (4), 2);
	list.InsertAt (new DATA (5), 4);

	data = list.RemoveAt (4);
	cout << data->a << endl;
	delete data;
	data = list.RemoveAt (2);
	cout << data->a << endl;
	delete data;
	data = list.RemoveAt (1);
	cout << data->a << endl;
	delete data;
	data = list.RemoveAt (0);
	cout << data->a << endl;
	delete data;
	data = list.RemoveAt (1);
	cout << data->a << endl;
	delete data;
	data = list.RemoveAt (0);
	cout << data->a << endl;
	delete data;

/* end list test */
	return 0;
}
AMD Phenom QUAD 9950 Black Edition, 8GB

error101 ::

ok
hvala

iNooby ::

Bom kr tukaj napisal.
Kak bi lahko naredo en program ki bi simuliral igro milijonar al pa kaj podobnega...da bi uporabil if pa for zanke?
Haters gonna hate...

ERGY ::

Ce nisi do zdej nic delal potem tudi naslednjic ne bos. Pac treba vzet stvar v roke resno in zacet delat.


Vredno ogleda ...

TemaSporočilaOglediZadnje sporočilo
TemaSporočilaOglediZadnje sporočilo
»

[Java] While zanka

Oddelek: Programiranje
262354 (1937) kunigunda
»

[Java] Urejanje baze z JTable

Oddelek: Programiranje
81009 (793) vonNeumann
»

Javascript - izračun razlike v datumih

Oddelek: Programiranje
81904 (1759) kogledom
»

[C++] Problem z dedovanjem šablon (template inhieritance)

Oddelek: Programiranje
131647 (1525) Gundolf
»

[C++] Krivulje - fillanje

Oddelek: Programiranje
101631 (1435) Jebiveter

Več podobnih tem