From 0ffa65cac7ad2f2a719d1c32c074d209626b6d60 Mon Sep 17 00:00:00 2001 From: Lendaia Mirai Date: Sun, 2 Nov 2025 00:03:25 +0100 Subject: [PATCH] ouchsick --- ALGA/03_Tomb.cs | 502 +++++++-------- ALGA/04_EgyszeruLanc.cs | 596 +++++++++--------- ALGA/05_BinarisKeresoFa.cs | 165 ++++- ALGA/Adatszerkezetek/03_TombImplementacio.cs | 268 +++++++- .../04_LancoltImplementacio.cs | 319 +++++++++- ALGA/Adatszerkezetek/05_BinarisKeresoFa.cs | 165 ++++- ALGA/Product.cs | 102 +++ Tesztek/05_BinarisKeresoFaTesztek.cs | 248 ++++---- 8 files changed, 1654 insertions(+), 711 deletions(-) create mode 100644 ALGA/Product.cs diff --git a/ALGA/03_Tomb.cs b/ALGA/03_Tomb.cs index dd51b19..8173515 100644 --- a/ALGA/03_Tomb.cs +++ b/ALGA/03_Tomb.cs @@ -1,274 +1,274 @@ -using System; -using System.Collections; -using System.Collections.Generic; +// using System; +// using System.Collections; +// using System.Collections.Generic; -namespace OE.ALGA.Adatszerkezetek -{ - public class TombVerem : Verem - { - T[] tomb; - int n = 0; - public TombVerem(int l) - { - tomb = new T[l]; - } - public bool Ures - { - get { return n == 0; } - } +// namespace OE.ALGA.Adatszerkezetek +// { +// public class TombVerem : Verem +// { +// T[] tomb; +// int n = 0; +// public TombVerem(int l) +// { +// tomb = new T[l]; +// } +// public bool Ures +// { +// get { return n == 0; } +// } - public T Felso() - { - return tomb[n - 1]; - } +// public T Felso() +// { +// return tomb[n - 1]; +// } - public void Verembe(T ertek) - { - if (n < tomb.Length) - { - tomb[n] = ertek; - n++; - } - else - { - throw new NincsHelyKivetel(); - } - } +// public void Verembe(T ertek) +// { +// if (n < tomb.Length) +// { +// tomb[n] = ertek; +// n++; +// } +// else +// { +// throw new NincsHelyKivetel(); +// } +// } - public T Verembol() - { - if (Ures) - { - throw new NincsElemKivetel(); - } - else - { - T ertek = tomb[n - 1]; - n--; - return ertek; - } +// public T Verembol() +// { +// if (Ures) +// { +// throw new NincsElemKivetel(); +// } +// else +// { +// T ertek = tomb[n - 1]; +// n--; +// return ertek; +// } - } - } - public class TombSor : Sor - { - T[] tomb; - int n = 0; - public TombSor(int l) - { - tomb = new T[l]; - } +// } +// } +// public class TombSor : Sor +// { +// T[] tomb; +// int n = 0; +// public TombSor(int l) +// { +// tomb = new T[l]; +// } - public bool Ures - { - get { return n == 0; } - } +// public bool Ures +// { +// get { return n == 0; } +// } - public T Elso() - { - return tomb[0]; - } +// public T Elso() +// { +// return tomb[0]; +// } - public void Sorba(T ertek) - { - if (n < tomb.Length) - { - tomb[n] = ertek; - n++; - } - else - { - throw new NincsHelyKivetel(); - } - } +// public void Sorba(T ertek) +// { +// if (n < tomb.Length) +// { +// tomb[n] = ertek; +// n++; +// } +// else +// { +// throw new NincsHelyKivetel(); +// } +// } - public T Sorbol() - { - if (Ures) - { - throw new NincsElemKivetel(); - } - else - { - T ertek = tomb[0]; - T[] tmp = new T[tomb.Length]; - n--; - for (int i = 1; i < tomb.Length; i++) - { - tmp[i - 1] = tomb[i]; - } - tomb = tmp; - return ertek; - } - } - } - public class TombLista : Lista, IEnumerable - { - T[] tomb; - int n = 0; +// public T Sorbol() +// { +// if (Ures) +// { +// throw new NincsElemKivetel(); +// } +// else +// { +// T ertek = tomb[0]; +// T[] tmp = new T[tomb.Length]; +// n--; +// for (int i = 1; i < tomb.Length; i++) +// { +// tmp[i - 1] = tomb[i]; +// } +// tomb = tmp; +// return ertek; +// } +// } +// } +// public class TombLista : Lista, IEnumerable +// { +// T[] tomb; +// int n = 0; - public bool Ures - { - get { return n <= 0; } - } - public TombLista() - { - tomb = new T[1]; - } +// public bool Ures +// { +// get { return n <= 0; } +// } +// public TombLista() +// { +// tomb = new T[1]; +// } - public int Elemszam - { - get { return n; } - } +// public int Elemszam +// { +// get { return n; } +// } - public void Bejar(Action muvelet) - { - for (int i = 0; i < n; i++) - { - muvelet?.Invoke(tomb[i]); - } - } +// public void Bejar(Action muvelet) +// { +// for (int i = 0; i < n; i++) +// { +// muvelet?.Invoke(tomb[i]); +// } +// } - public void Beszur(int index, T ertek) - { - if (index < 0) - { - throw new HibasIndexKivetel(); - } - else - { - T[] tmp = new T[tomb.Length * 2]; - for (int i = 0; i < index; i++) - { - tmp[i] = tomb[i]; - } - tmp[index] = ertek; - for (int i = index + 1; i < tomb.Length; i++) - { - tmp[i] = tomb[i - 1]; - } - tomb = tmp; - n++; - } - } +// public void Beszur(int index, T ertek) +// { +// if (index < 0) +// { +// throw new HibasIndexKivetel(); +// } +// else +// { +// T[] tmp = new T[tomb.Length * 2]; +// for (int i = 0; i < index; i++) +// { +// tmp[i] = tomb[i]; +// } +// tmp[index] = ertek; +// for (int i = index + 1; i < tomb.Length; i++) +// { +// tmp[i] = tomb[i - 1]; +// } +// tomb = tmp; +// n++; +// } +// } - public IEnumerator GetEnumerator() - { - foreach (T t in tomb) - { - if (t != null) - { - yield return t; - } - } - } +// public IEnumerator GetEnumerator() +// { +// foreach (T t in tomb) +// { +// if (t != null) +// { +// yield return t; +// } +// } +// } - public void Hozzafuz(T ertek) - { - if (n < tomb.Length) - { - tomb[n] = ertek; - n++; - } - else - { - T[] tmp = new T[tomb.Length * 2]; - for (int i = 0; i < tomb.Length; i++) - { - tmp[i] = tomb[i]; - } - tmp[n] = ertek; - tomb = tmp; - n++; - } - } +// public void Hozzafuz(T ertek) +// { +// if (n < tomb.Length) +// { +// tomb[n] = ertek; +// n++; +// } +// else +// { +// T[] tmp = new T[tomb.Length * 2]; +// for (int i = 0; i < tomb.Length; i++) +// { +// tmp[i] = tomb[i]; +// } +// tmp[n] = ertek; +// tomb = tmp; +// n++; +// } +// } - public T Kiolvas(int index) - { - if (index < 0 || index >= tomb.Length) - { - throw new HibasIndexKivetel(); - } - return tomb[index]; - } +// public T Kiolvas(int index) +// { +// if (index < 0 || index >= tomb.Length) +// { +// throw new HibasIndexKivetel(); +// } +// return tomb[index]; +// } - public void Modosit(int index, T ertek) - { - if (index < 0 && index < tomb.Length) - { - throw new HibasIndexKivetel(); - } - else - { - tomb[index] = ertek; - } - } +// public void Modosit(int index, T ertek) +// { +// if (index < 0 && index < tomb.Length) +// { +// throw new HibasIndexKivetel(); +// } +// else +// { +// tomb[index] = ertek; +// } +// } - public void Torol(T ertek) - { - int m = n; - T[] tmp = new T[tomb.Length]; - int index = 0; - for (int i = 0; i < m; i++) - { - if (ertek.Equals(tomb[i])) - { - n--; - continue; - } - tmp[index] = tomb[i]; - index++; - } - tomb = tmp; - } +// public void Torol(T ertek) +// { +// int m = n; +// T[] tmp = new T[tomb.Length]; +// int index = 0; +// for (int i = 0; i < m; i++) +// { +// if (ertek.Equals(tomb[i])) +// { +// n--; +// continue; +// } +// tmp[index] = tomb[i]; +// index++; +// } +// tomb = tmp; +// } - IEnumerator IEnumerable.GetEnumerator() - { - TombListaBejaro bejaro = new TombListaBejaro(tomb, n); - return bejaro; - } - } - public class TombListaBejaro : IEnumerator - { - T[] e; - int n; - int aktualisindex; - T current; - public T Current - { - get { return current; } - } - public TombListaBejaro(T[] e, int n) - { - this.n = n; - this.e = e; - aktualisindex = 0; - current = e[0]; - } +// IEnumerator IEnumerable.GetEnumerator() +// { +// TombListaBejaro bejaro = new TombListaBejaro(tomb, n); +// return bejaro; +// } +// } +// public class TombListaBejaro : IEnumerator +// { +// T[] e; +// int n; +// int aktualisindex; +// T current; +// public T Current +// { +// get { return current; } +// } +// public TombListaBejaro(T[] e, int n) +// { +// this.n = n; +// this.e = e; +// aktualisindex = 0; +// current = e[0]; +// } - object IEnumerator.Current => Current; +// object IEnumerator.Current => Current; - public void Dispose() - { - throw new NotImplementedException(); - } +// public void Dispose() +// { +// throw new NotImplementedException(); +// } - public bool MoveNext() - { - if (aktualisindex < n - 1) - { - aktualisindex++; - return true; - } - else - { - return false; - } - } +// public bool MoveNext() +// { +// if (aktualisindex < n - 1) +// { +// aktualisindex++; +// return true; +// } +// else +// { +// return false; +// } +// } - public void Reset() - { - aktualisindex = -1; - } - } -} \ No newline at end of file +// public void Reset() +// { +// aktualisindex = -1; +// } +// } +// } \ No newline at end of file diff --git a/ALGA/04_EgyszeruLanc.cs b/ALGA/04_EgyszeruLanc.cs index dac244d..4b7dc88 100644 --- a/ALGA/04_EgyszeruLanc.cs +++ b/ALGA/04_EgyszeruLanc.cs @@ -1,325 +1,325 @@ -using System; -using System.Collections; -using System.Collections.Generic; -using System.Runtime.CompilerServices; +// using System; +// using System.Collections; +// using System.Collections.Generic; +// using System.Runtime.CompilerServices; -namespace OE.ALGA.Adatszerkezetek; +// namespace OE.ALGA.Adatszerkezetek; -public class LancElem -{ - public T tart; - public LancElem? kov; - public LancElem? elozo; +// public class LancElem +// { +// public T tart; +// public LancElem? kov; +// public LancElem? elozo; - public LancElem(T tart, LancElem? kov, LancElem? elozo) - { - this.tart = tart; - this.kov = kov; - this.elozo = elozo; - } -} -public class LancoltVerem : Verem -{ - LancElem? fej; - LancElem? teteje; - public LancoltVerem() - { - } - public void Felszabadit() - { +// public LancElem(T tart, LancElem? kov, LancElem? elozo) +// { +// this.tart = tart; +// this.kov = kov; +// this.elozo = elozo; +// } +// } +// public class LancoltVerem : Verem +// { +// LancElem? fej; +// LancElem? teteje; +// public LancoltVerem() +// { +// } +// public void Felszabadit() +// { - } - public bool Ures - { - get { return fej == null; } - } +// } +// public bool Ures +// { +// get { return fej == null; } +// } - public T Felso() - { - if (Ures) - { - throw new NincsElemKivetel(); - } - else - { - LancElem? tmp = fej; - while (tmp.kov != null) - { - tmp = tmp.kov; - } - teteje = tmp; - return tmp.tart; - } - } +// public T Felso() +// { +// if (Ures) +// { +// throw new NincsElemKivetel(); +// } +// else +// { +// LancElem? tmp = fej; +// while (tmp.kov != null) +// { +// tmp = tmp.kov; +// } +// teteje = tmp; +// return tmp.tart; +// } +// } - public void Verembe(T ertek) - { - if (Ures) - { - fej = new LancElem(ertek, null, null); - teteje = fej; - } - else - { - teteje.kov = new LancElem(ertek, null, teteje); - teteje = teteje.kov; - } - } +// public void Verembe(T ertek) +// { +// if (Ures) +// { +// fej = new LancElem(ertek, null, null); +// teteje = fej; +// } +// else +// { +// teteje.kov = new LancElem(ertek, null, teteje); +// teteje = teteje.kov; +// } +// } - public T Verembol() - { - if (Ures) - { - throw new NincsElemKivetel(); - } - else - { - if (teteje.elozo == null) - { - T ertek = fej.tart; - fej = null; - teteje = fej; - return ertek; - } - else - { - T ertek = Felso(); - teteje.elozo.kov = null; - teteje = teteje.elozo; - return ertek; - } - } - } -} -public class LancoltSor : Sor -{ - LancElem? fej; - LancElem? vege; - public bool Ures - { - get { return ( vege == null); } - } - public LancoltSor() - { +// public T Verembol() +// { +// if (Ures) +// { +// throw new NincsElemKivetel(); +// } +// else +// { +// if (teteje.elozo == null) +// { +// T ertek = fej.tart; +// fej = null; +// teteje = fej; +// return ertek; +// } +// else +// { +// T ertek = Felso(); +// teteje.elozo.kov = null; +// teteje = teteje.elozo; +// return ertek; +// } +// } +// } +// } +// public class LancoltSor : Sor +// { +// LancElem? fej; +// LancElem? vege; +// public bool Ures +// { +// get { return ( vege == null); } +// } +// public LancoltSor() +// { - } - public void Felszabadit() - { +// } +// public void Felszabadit() +// { - } - public T Elso() - { - return fej.tart; - } +// } +// public T Elso() +// { +// return fej.tart; +// } - public void Sorba(T ertek) - { - if (Ures) - { - fej = new LancElem(ertek, null, null); - vege = fej; - } - else - { - LancElem? tmp = fej; - while (tmp.kov != null) - { - tmp = tmp.kov; - } - tmp.kov = new LancElem(ertek, null, tmp); - vege = tmp.kov; - } - } +// public void Sorba(T ertek) +// { +// if (Ures) +// { +// fej = new LancElem(ertek, null, null); +// vege = fej; +// } +// else +// { +// LancElem? tmp = fej; +// while (tmp.kov != null) +// { +// tmp = tmp.kov; +// } +// tmp.kov = new LancElem(ertek, null, tmp); +// vege = tmp.kov; +// } +// } - public T Sorbol() - { - if (Ures) - { - throw new NincsElemKivetel(); - } - else - { - if (vege.elozo == null) - { - T ertek = vege.tart; - vege = null; - return ertek; - } - else - { - T ertek = fej.tart; - fej = fej.kov; - fej.elozo = null; - return ertek; - } - } - } -} -public class LancoltLista : Lista, IEnumerable -{ - int n; - LancElem? fej; +// public T Sorbol() +// { +// if (Ures) +// { +// throw new NincsElemKivetel(); +// } +// else +// { +// if (vege.elozo == null) +// { +// T ertek = vege.tart; +// vege = null; +// return ertek; +// } +// else +// { +// T ertek = fej.tart; +// fej = fej.kov; +// fej.elozo = null; +// return ertek; +// } +// } +// } +// } +// public class LancoltLista : Lista, IEnumerable +// { +// int n; +// LancElem? fej; - public int Elemszam - { - get { return n; } - } - public LancoltLista() - { +// public int Elemszam +// { +// get { return n; } +// } +// public LancoltLista() +// { - } - public void Felszabadit() - { +// } +// public void Felszabadit() +// { - } +// } - public void Bejar(Action muvelet) - { - for (int i = 0; i < n; i++) - { - muvelet(Indexedik(i).tart); - } - } +// public void Bejar(Action muvelet) +// { +// for (int i = 0; i < n; i++) +// { +// muvelet(Indexedik(i).tart); +// } +// } - public void Beszur(int index, T ertek) - { - if (fej == null) - { - if (index == 0) - { - Hozzafuz(ertek); - } - else - { - throw new HibasIndexKivetel(); - } - } - else - { - if (index == 0) - { - fej.elozo = new LancElem(ertek, fej, null); - fej = fej.elozo; - n++; - } - else if (index == n) - { - Hozzafuz(ertek); - n++; - } - else - { - LancElem? xd = Indexedik(index).elozo; - xd.kov = new LancElem(ertek, Indexedik(index), xd.kov); - xd.kov.kov.elozo = xd.kov; - n++; - } - } - } +// public void Beszur(int index, T ertek) +// { +// if (fej == null) +// { +// if (index == 0) +// { +// Hozzafuz(ertek); +// } +// else +// { +// throw new HibasIndexKivetel(); +// } +// } +// else +// { +// if (index == 0) +// { +// fej.elozo = new LancElem(ertek, fej, null); +// fej = fej.elozo; +// n++; +// } +// else if (index == n) +// { +// Hozzafuz(ertek); +// n++; +// } +// else +// { +// LancElem? xd = Indexedik(index).elozo; +// xd.kov = new LancElem(ertek, Indexedik(index), xd.kov); +// xd.kov.kov.elozo = xd.kov; +// n++; +// } +// } +// } - public void Hozzafuz(T ertek) - { - if (Elemszam < 1) - { - fej = new LancElem(ertek, null, null); - n++; - } - else - { - LancElem asd = Indexedik(n-1); - asd.kov = new LancElem(ertek, null, asd); - n++; - } - } +// public void Hozzafuz(T ertek) +// { +// if (Elemszam < 1) +// { +// fej = new LancElem(ertek, null, null); +// n++; +// } +// else +// { +// LancElem asd = Indexedik(n-1); +// asd.kov = new LancElem(ertek, null, asd); +// n++; +// } +// } - public T Kiolvas(int index) - { - return Indexedik(index).tart; - } +// public T Kiolvas(int index) +// { +// return Indexedik(index).tart; +// } - public void Modosit(int index, T ertek) - { - Indexedik(index).tart = ertek; - } +// public void Modosit(int index, T ertek) +// { +// Indexedik(index).tart = ertek; +// } - public void Torol(T ertek) - { - LancElem tmp = fej; - while (tmp.kov != null) - { - if (ertek.Equals(tmp.tart)) - { - tmp.elozo.kov = tmp.kov; - tmp.kov.elozo = tmp.elozo; +// public void Torol(T ertek) +// { +// LancElem tmp = fej; +// while (tmp.kov != null) +// { +// if (ertek.Equals(tmp.tart)) +// { +// tmp.elozo.kov = tmp.kov; +// tmp.kov.elozo = tmp.elozo; - } - tmp = tmp.kov; - } - } - private LancElem Indexedik(int index) - { - LancElem? tmp = fej; - for (int i = 0; i < index; i++) - { - tmp = tmp.kov; - } - return tmp; - } +// } +// tmp = tmp.kov; +// } +// } +// private LancElem Indexedik(int index) +// { +// LancElem? tmp = fej; +// for (int i = 0; i < index; i++) +// { +// tmp = tmp.kov; +// } +// return tmp; +// } - public IEnumerator GetEnumerator() - { - LancElem tmp = fej; - while(tmp != null) - { - if (tmp.tart != null) - { - yield return tmp.tart; - } - tmp = tmp.kov; - } - } +// public IEnumerator GetEnumerator() +// { +// LancElem tmp = fej; +// while(tmp != null) +// { +// if (tmp.tart != null) +// { +// yield return tmp.tart; +// } +// tmp = tmp.kov; +// } +// } - IEnumerator IEnumerable.GetEnumerator() - { - LancoltListaBejaro bejaro = new LancoltListaBejaro(this.fej); - return bejaro; - } -} -class LancoltListaBejaro : IEnumerator -{ - LancElem fej; - LancElem aktualisElem; - public T Current - { - get{ return aktualisElem.tart; } - } +// IEnumerator IEnumerable.GetEnumerator() +// { +// LancoltListaBejaro bejaro = new LancoltListaBejaro(this.fej); +// return bejaro; +// } +// } +// class LancoltListaBejaro : IEnumerator +// { +// LancElem fej; +// LancElem aktualisElem; +// public T Current +// { +// get{ return aktualisElem.tart; } +// } - object IEnumerator.Current => Current; - public LancoltListaBejaro(LancElem fej) - { - this.fej = fej; - aktualisElem = fej; - } +// object IEnumerator.Current => Current; +// public LancoltListaBejaro(LancElem fej) +// { +// this.fej = fej; +// aktualisElem = fej; +// } - public void Dispose() - { - throw new NotImplementedException(); - } +// public void Dispose() +// { +// throw new NotImplementedException(); +// } - public bool MoveNext() - { - if (aktualisElem.kov == null) - { - return false; - } - { - aktualisElem = aktualisElem.kov; - return true; - } - } +// public bool MoveNext() +// { +// if (aktualisElem.kov == null) +// { +// return false; +// } +// { +// aktualisElem = aktualisElem.kov; +// return true; +// } +// } - public void Reset() - { - aktualisElem = fej; - } -} +// public void Reset() +// { +// aktualisElem = fej; +// } +// } diff --git a/ALGA/05_BinarisKeresoFa.cs b/ALGA/05_BinarisKeresoFa.cs index 95a26b1..c7f6b0d 100644 --- a/ALGA/05_BinarisKeresoFa.cs +++ b/ALGA/05_BinarisKeresoFa.cs @@ -1,40 +1,137 @@ -using System; +// using System; +// using System.Drawing; -namespace OE.ALGA; +// namespace OE.ALGA; -public class FaElem where T : IComparable -{ - public T tart; - public FaElem? bal; - public FaElem? jobb; +// internal class FaElem where T : IComparable +// { +// public T? tart; +// public FaElem? bal; +// public FaElem? jobb; - public FaElem(T tart, FaElem? bal, FaElem? jobb) - { - this.tart = tart; - this.bal = bal; - this.jobb = jobb; - } -} -public class FaHalmaz : Halmaz where T : IComparable -{ - FaElem? gyoker; - public void Bejar(Action muvelet) - { - throw new NotImplementedException(); - } +// public FaElem(T? tart, FaElem? bal, FaElem? jobb) +// { +// this.tart = tart; +// this.bal = bal; +// this.jobb = jobb; +// } +// } +// public class FaHalmaz : Halmaz where T : IComparable +// { +// FaElem? gyoker; +// public void Bejar(Action muvelet) +// { +// throw new NotImplementedException(); +// } - public void Beszur(T ertek) - { - throw new NotImplementedException(); - } +// private static FaElem ReszfabaBeszur(FaElem p, T ertek) +// { +// if (p == null) +// { +// FaElem uj = new FaElem(ertek, null, null); +// return uj; +// } +// else +// { +// if (p.tart.CompareTo(ertek) > 0) +// { +// p.bal = ReszfabaBeszur(p.bal, ertek); +// } +// else +// { +// if (p.tart.CompareTo(ertek) < 0) +// { +// p.jobb = ReszfabaBeszur(p.jobb, ertek); +// } +// } +// return p; +// } +// } +// public void Beszur(T ertek) +// { +// gyoker = ReszfabaBeszur(gyoker, ertek); +// } - public bool Eleme(T ertek) - { - throw new NotImplementedException(); - } +// private static FaElem ReszfaEleme(FaElem p, T ertek) +// { +// FaElem? tmp = null; +// if (p == null) +// { +// return tmp; +// } +// else +// { +// if (p.tart.CompareTo(ertek) > 0) +// { +// tmp = ReszfaEleme(p.bal, ertek); +// } +// else if (p.tart.CompareTo(ertek) < 0) +// { +// tmp = ReszfabaBeszur(p.jobb, ertek); +// } +// else +// { +// return tmp; +// } +// return tmp; +// } +// } +// public bool Eleme(T ertek) +// { +// return !(ReszfaEleme(gyoker, ertek) == null); +// } +// private static FaElem KetGyerekesTorles(FaElem p, FaElem ertek) +// { +// if (p.jobb != null) +// { +// p.jobb = KetGyerekesTorles(p.jobb, ertek); +// return p; +// } +// else +// { +// ertek.tart = p.tart; +// p = p.bal; +// return p; - public void Torol(T ertek) - { - throw new NotImplementedException(); - } -} +// } +// } +// private static FaElem ReszfabolTorol(FaElem p, T ertek) +// { +// if (p == null) +// { +// throw new NincsElemKivetel(); +// } +// if (p.tart.CompareTo(ertek) < 0) +// { +// p.bal = ReszfabolTorol(p.bal, ertek); +// } +// else if (p.tart.CompareTo(ertek) > 0) +// { +// p.jobb = ReszfabolTorol(p.jobb, ertek); +// } +// else +// { +// if (p.jobb == null && p.bal == null) +// { +// p = null; +// } +// else if (p.jobb == null && p.bal != null) +// { +// p = p.bal; +// } +// else if (p.jobb != null && p.bal == null) +// { +// p = p.jobb; +// } +// else +// { +// p = KetGyerekesTorles(p, p); +// } +// } +// return p; +// } +// public void Torol(T ertek) +// { +// ReszfabolTorol(gyoker, ertek); +// } +// } diff --git a/ALGA/Adatszerkezetek/03_TombImplementacio.cs b/ALGA/Adatszerkezetek/03_TombImplementacio.cs index ac3f62b..45bd5fc 100644 --- a/ALGA/Adatszerkezetek/03_TombImplementacio.cs +++ b/ALGA/Adatszerkezetek/03_TombImplementacio.cs @@ -4,5 +4,271 @@ using System.Collections.Generic; namespace OE.ALGA.Adatszerkezetek { - // 3. heti labor feladat - Tesztek: 03_TombImplementacioTesztek.cs + public class TombVerem : Verem + { + T[] tomb; + int n = 0; + public TombVerem(int l) + { + tomb = new T[l]; + } + public bool Ures + { + get { return n == 0; } + } + + public T Felso() + { + return tomb[n - 1]; + } + + public void Verembe(T ertek) + { + if (n < tomb.Length) + { + tomb[n] = ertek; + n++; + } + else + { + throw new NincsHelyKivetel(); + } + } + + public T Verembol() + { + if (Ures) + { + throw new NincsElemKivetel(); + } + else + { + T ertek = tomb[n - 1]; + n--; + return ertek; + } + + } + } + public class TombSor : Sor + { + T[] tomb; + int n = 0; + public TombSor(int l) + { + tomb = new T[l]; + } + + public bool Ures + { + get { return n == 0; } + } + + public T Elso() + { + return tomb[0]; + } + + public void Sorba(T ertek) + { + if (n < tomb.Length) + { + tomb[n] = ertek; + n++; + } + else + { + throw new NincsHelyKivetel(); + } + } + + public T Sorbol() + { + if (Ures) + { + throw new NincsElemKivetel(); + } + else + { + T ertek = tomb[0]; + T[] tmp = new T[tomb.Length]; + n--; + for (int i = 1; i < tomb.Length; i++) + { + tmp[i - 1] = tomb[i]; + } + tomb = tmp; + return ertek; + } + } + } + public class TombLista : Lista, IEnumerable + { + T[] tomb; + int n = 0; + + public bool Ures + { + get { return n <= 0; } + } + public TombLista() + { + tomb = new T[1]; + } + + public int Elemszam + { + get { return n; } + } + + public void Bejar(Action muvelet) + { + for (int i = 0; i < n; i++) + { + muvelet?.Invoke(tomb[i]); + } + } + + public void Beszur(int index, T ertek) + { + if (index < 0) + { + throw new HibasIndexKivetel(); + } + else + { + T[] tmp = new T[tomb.Length * 2]; + for (int i = 0; i < index; i++) + { + tmp[i] = tomb[i]; + } + tmp[index] = ertek; + for (int i = index + 1; i < tomb.Length; i++) + { + tmp[i] = tomb[i - 1]; + } + tomb = tmp; + n++; + } + } + + public IEnumerator GetEnumerator() + { + foreach (T t in tomb) + { + if (t != null) + { + yield return t; + } + } + } + + public void Hozzafuz(T ertek) + { + if (n < tomb.Length) + { + tomb[n] = ertek; + n++; + } + else + { + T[] tmp = new T[tomb.Length * 2]; + for (int i = 0; i < tomb.Length; i++) + { + tmp[i] = tomb[i]; + } + tmp[n] = ertek; + tomb = tmp; + n++; + } + } + + public T Kiolvas(int index) + { + if (index < 0 || index >= tomb.Length) + { + throw new HibasIndexKivetel(); + } + return tomb[index]; + } + + public void Modosit(int index, T ertek) + { + if (index < 0 && index < tomb.Length) + { + throw new HibasIndexKivetel(); + } + else + { + tomb[index] = ertek; + } + } + + public void Torol(T ertek) + { + int m = n; + T[] tmp = new T[tomb.Length]; + int index = 0; + for (int i = 0; i < m; i++) + { + if (ertek.Equals(tomb[i])) + { + n--; + continue; + } + tmp[index] = tomb[i]; + index++; + } + tomb = tmp; + } + + IEnumerator IEnumerable.GetEnumerator() + { + TombListaBejaro bejaro = new TombListaBejaro(tomb, n); + return bejaro; + } + } + public class TombListaBejaro : IEnumerator + { + T[] e; + int n; + int aktualisindex; + T current; + public T Current + { + get { return current; } + } + public TombListaBejaro(T[] e, int n) + { + this.n = n; + this.e = e; + aktualisindex = 0; + current = e[0]; + } + + object IEnumerator.Current => Current; + + public void Dispose() + { + throw new NotImplementedException(); + } + + public bool MoveNext() + { + if (aktualisindex < n - 1) + { + aktualisindex++; + return true; + } + else + { + return false; + } + } + + public void Reset() + { + aktualisindex = -1; + } + } } diff --git a/ALGA/Adatszerkezetek/04_LancoltImplementacio.cs b/ALGA/Adatszerkezetek/04_LancoltImplementacio.cs index 6c3a8c4..1e561f6 100644 --- a/ALGA/Adatszerkezetek/04_LancoltImplementacio.cs +++ b/ALGA/Adatszerkezetek/04_LancoltImplementacio.cs @@ -4,5 +4,322 @@ using System.Collections.Generic; namespace OE.ALGA.Adatszerkezetek { - // 4. heti labor feladat - Tesztek: 04_LancoltImplementacioTesztek.cs + public class LancElem + { + public T tart; + public LancElem? kov; + public LancElem? elozo; + + public LancElem(T tart, LancElem? kov, LancElem? elozo) + { + this.tart = tart; + this.kov = kov; + this.elozo = elozo; + } + } + public class LancoltVerem : Verem + { + LancElem? fej; + LancElem? teteje; + public LancoltVerem() + { + } + public void Felszabadit() + { + + } + public bool Ures + { + get { return fej == null; } + } + + public T Felso() + { + if (Ures) + { + throw new NincsElemKivetel(); + } + else + { + LancElem? tmp = fej; + while (tmp.kov != null) + { + tmp = tmp.kov; + } + teteje = tmp; + return tmp.tart; + } + } + + public void Verembe(T ertek) + { + if (Ures) + { + fej = new LancElem(ertek, null, null); + teteje = fej; + } + else + { + teteje.kov = new LancElem(ertek, null, teteje); + teteje = teteje.kov; + } + } + + public T Verembol() + { + if (Ures) + { + throw new NincsElemKivetel(); + } + else + { + if (teteje.elozo == null) + { + T ertek = fej.tart; + fej = null; + teteje = fej; + return ertek; + } + else + { + T ertek = Felso(); + teteje.elozo.kov = null; + teteje = teteje.elozo; + return ertek; + } + } + } + } + public class LancoltSor : Sor + { + LancElem? fej; + LancElem? vege; + public bool Ures + { + get { return (vege == null); } + } + public LancoltSor() + { + + } + public void Felszabadit() + { + + } + public T Elso() + { + return fej.tart; + } + + public void Sorba(T ertek) + { + if (Ures) + { + fej = new LancElem(ertek, null, null); + vege = fej; + } + else + { + LancElem? tmp = fej; + while (tmp.kov != null) + { + tmp = tmp.kov; + } + tmp.kov = new LancElem(ertek, null, tmp); + vege = tmp.kov; + } + } + + public T Sorbol() + { + if (Ures) + { + throw new NincsElemKivetel(); + } + else + { + if (vege.elozo == null) + { + T ertek = vege.tart; + vege = null; + return ertek; + } + else + { + T ertek = fej.tart; + fej = fej.kov; + fej.elozo = null; + return ertek; + } + } + } + } + public class LancoltLista : Lista, IEnumerable + { + int n; + LancElem? fej; + + public int Elemszam + { + get { return n; } + } + public LancoltLista() + { + + } + public void Felszabadit() + { + + } + + public void Bejar(Action muvelet) + { + for (int i = 0; i < n; i++) + { + muvelet(Indexedik(i).tart); + } + } + + public void Beszur(int index, T ertek) + { + if (fej == null) + { + if (index == 0) + { + Hozzafuz(ertek); + } + else + { + throw new HibasIndexKivetel(); + } + } + else + { + if (index == 0) + { + fej.elozo = new LancElem(ertek, fej, null); + fej = fej.elozo; + n++; + } + else if (index == n) + { + Hozzafuz(ertek); + n++; + } + else + { + LancElem? xd = Indexedik(index).elozo; + xd.kov = new LancElem(ertek, Indexedik(index), xd.kov); + xd.kov.kov.elozo = xd.kov; + n++; + } + } + } + + public void Hozzafuz(T ertek) + { + if (Elemszam < 1) + { + fej = new LancElem(ertek, null, null); + n++; + } + else + { + LancElem asd = Indexedik(n - 1); + asd.kov = new LancElem(ertek, null, asd); + n++; + } + } + + public T Kiolvas(int index) + { + return Indexedik(index).tart; + } + + public void Modosit(int index, T ertek) + { + Indexedik(index).tart = ertek; + } + + public void Torol(T ertek) + { + LancElem tmp = fej; + while (tmp.kov != null) + { + if (ertek.Equals(tmp.tart)) + { + tmp.elozo.kov = tmp.kov; + tmp.kov.elozo = tmp.elozo; + + } + tmp = tmp.kov; + } + } + private LancElem Indexedik(int index) + { + LancElem? tmp = fej; + for (int i = 0; i < index; i++) + { + tmp = tmp.kov; + } + return tmp; + } + + public IEnumerator GetEnumerator() + { + LancElem tmp = fej; + while (tmp != null) + { + if (tmp.tart != null) + { + yield return tmp.tart; + } + tmp = tmp.kov; + } + } + + IEnumerator IEnumerable.GetEnumerator() + { + LancoltListaBejaro bejaro = new LancoltListaBejaro(this.fej); + return bejaro; + } + } + class LancoltListaBejaro : IEnumerator + { + LancElem fej; + LancElem aktualisElem; + public T Current + { + get { return aktualisElem.tart; } + } + + object IEnumerator.Current => Current; + public LancoltListaBejaro(LancElem fej) + { + this.fej = fej; + aktualisElem = fej; + } + + public void Dispose() + { + throw new NotImplementedException(); + } + + public bool MoveNext() + { + if (aktualisElem.kov == null) + { + return false; + } + { + aktualisElem = aktualisElem.kov; + return true; + } + } + + public void Reset() + { + aktualisElem = fej; + } + } } \ No newline at end of file diff --git a/ALGA/Adatszerkezetek/05_BinarisKeresoFa.cs b/ALGA/Adatszerkezetek/05_BinarisKeresoFa.cs index 26d2ea6..6d9e187 100644 --- a/ALGA/Adatszerkezetek/05_BinarisKeresoFa.cs +++ b/ALGA/Adatszerkezetek/05_BinarisKeresoFa.cs @@ -1,6 +1,167 @@ using System; +using System.Drawing; -namespace OE.ALGA.Adatszerkezetek +namespace OE.ALGA; + +internal class FaElem where T : IComparable { - // 5. heti labor feladat - Tesztek: 05_BinarisKeresoFaTesztek.cs + public T? tart; + public FaElem? bal; + public FaElem? jobb; + + public FaElem(T? tart, FaElem? bal, FaElem? jobb) + { + this.tart = tart; + this.bal = bal; + this.jobb = jobb; + } +} +public class FaHalmaz : Halmaz where T : IComparable +{ + FaElem? gyoker; + + private void BejarasPreOrder(FaElem p, Action muvelet) + { + if (p != null) + { + muvelet(p.tart); + BejarasPreOrder(p.bal, muvelet); + BejarasPreOrder(p.jobb, muvelet); + } + } + private void BejarasInOrder(FaElem p, Action muvelet) + { + if (p != null) + { + BejarasInOrder(p.bal, muvelet); + muvelet(p.tart); + BejarasInOrder(p.jobb, muvelet); + } + } + private void BejarasPostOrder(FaElem p, Action muvelet) + { + if (p != null) + { + BejarasPostOrder(p.bal, muvelet); + BejarasPostOrder(p.jobb, muvelet); + muvelet(p.tart); + } + } + public void Bejar(Action muvelet) + { + BejarasPreOrder(gyoker, muvelet); + } + + private static FaElem ReszfabaBeszur(FaElem p, T ertek) + { + if (p == null) + { + FaElem uj = new FaElem(ertek, null, null); + return uj; + } + else + { + if (p.tart.CompareTo(ertek) > 0) + { + p.bal = ReszfabaBeszur(p.bal, ertek); + } + else + { + if (p.tart.CompareTo(ertek) < 0) + { + p.jobb = ReszfabaBeszur(p.jobb, ertek); + } + } + return p; + } + } + public void Beszur(T ertek) + { + gyoker = ReszfabaBeszur(gyoker, ertek); + } + + private static FaElem ReszfaEleme(FaElem p, T ertek) + { + FaElem? tmp = null; + if (p == null) + { + return tmp; + } + else + { + if (p.tart.CompareTo(ertek) > 0) + { + tmp = ReszfaEleme(p.bal, ertek); + } + else if (p.tart.CompareTo(ertek) < 0) + { + tmp = ReszfaEleme(p.jobb, ertek); + } + else + { + tmp = p; + } + return tmp; + } + } + public bool Eleme(T ertek) + { + return (ReszfaEleme(gyoker, ertek) != null); + } + private static FaElem KetGyerekesTorles(FaElem p, T ertek) + { + if (p.jobb != null) + { + p.jobb = KetGyerekesTorles(p.jobb, ertek); + return p; + } + else + { + ertek = p.tart; + p = p.bal; + return p; + } + } + private static FaElem ReszfabolTorol(ref FaElem p, T ertek) + { + if (p == null) + { + throw new NincsElemKivetel(); + } + if (p.tart.CompareTo(ertek) > 0) + { + p.bal = ReszfabolTorol(ref p.bal, ertek); + } + else if (p.tart.CompareTo(ertek) < 0) + { + p.jobb = ReszfabolTorol(ref p.jobb, ertek); + } + else + { + if (p.jobb == null && p.bal == null) + { + p = null; + } + else if (p.jobb == null && p.bal != null) + { + p = p.bal; + } + else if (p.jobb != null && p.bal == null) + { + p = p.jobb; + } + else + { + p = KetGyerekesTorles(p, p.tart); + } + } + return p; + } + public void Torol(T ertek) + { + if (gyoker != null) + { + ReszfabolTorol(ref gyoker, ertek); + } + } } diff --git a/ALGA/Product.cs b/ALGA/Product.cs new file mode 100644 index 0000000..2baf25b --- /dev/null +++ b/ALGA/Product.cs @@ -0,0 +1,102 @@ +using System; +using System.Collections.Generic; +using System.Linq; +using System.Reflection; +using System.Runtime.CompilerServices; + +namespace OE.ALGA; + +public class Product +{ + [Validation(AttributeValidation.NotEmpty)] + public string Name { get; } + + [Validation(AttributeValidation.PositiveNumber)] + public decimal Price { get; } + + [Validation(AttributeValidation.NonNegative)] + public int Quantity { get; } + + [Validation(AttributeValidation.FutureDate)] + public DateTime? Experiation { get; } + + public Product(string name, decimal price, int quantity, DateTime? experiarion) + { + Name = name; + Price = price; + Quantity = quantity; + Experiation = experiarion; + } +} +public enum AttributeValidation +{ + NotEmpty, + PositiveNumber, + NonNegative, + FutureDate +} +public class ValidationAttribute : Attribute +{ + public AttributeValidation Rule { get; } + public ValidationAttribute(AttributeValidation rule) + { + Rule = rule; + } +} +public static class Validator +{ + public static void Validate(Product product) + { + bool flag = false; + var t = product.GetType(); + + foreach (var p in t.GetProperties()) + { + var value = p.GetValue(product); + + switch (p.GetCustomAttribute().Rule) + { + case AttributeValidation.NotEmpty: + if (Convert.ToString(value) == "") + { + flag = true; + } + break; + + case AttributeValidation.PositiveNumber: + if (Convert.ToInt32(value) <= 0) + { + flag = true; + } + break; + + case AttributeValidation.NonNegative: + if (Convert.ToInt32(value) < 0) + { + flag = true; + } + break; + + case AttributeValidation.FutureDate: + if (Convert.ToDateTime(value) < DateTime.Now) + { + flag = true; + } + break; + } + + } + if (flag) + { + throw new WrongProductException(); + } + else + { + Console.WriteLine("Product added successfuly"); + } + } +} +public class WrongProductException : Exception +{ + +} diff --git a/Tesztek/05_BinarisKeresoFaTesztek.cs b/Tesztek/05_BinarisKeresoFaTesztek.cs index fd13012..5d71d78 100644 --- a/Tesztek/05_BinarisKeresoFaTesztek.cs +++ b/Tesztek/05_BinarisKeresoFaTesztek.cs @@ -1,125 +1,125 @@ -//using NUnit.Framework; -//using OE.ALGA.Adatszerkezetek; +using NUnit.Framework; +using OE.ALGA.Adatszerkezetek; -//namespace OE.ALGA.Tesztek.Adatszerkezetek -//{ -// [TestFixture(Category = "Adatszerkezetek", TestName = "05 - Fa Halmaz Tesztek")] -// public class FaHalmazTesztek -// { -// [TestCase] -// public void BeszurasUres() -// { -// Halmaz v = new FaHalmaz(); -// v.Beszur(""); -// Assert.Multiple(() => -// { -// Assert.That(v.Eleme(""), Is.True); -// }); -// } -// [TestCase] -// public void Beszuras() -// { -// Halmaz v = new FaHalmaz(); -// v.Beszur(1); -// v.Beszur(3); -// v.Beszur(2); -// Assert.Multiple(() => -// { -// Assert.That(v.Eleme(1), Is.True); -// Assert.That(v.Eleme(2), Is.True); -// Assert.That(v.Eleme(3), Is.True); -// }); -// } -// [TestCase] -// public void DuplaBeszuras() -// { -// Halmaz v = new FaHalmaz(); -// v.Beszur(1); -// v.Beszur(2); -// v.Beszur(3); -// v.Beszur(2); -// Assert.Multiple(() => -// { -// Assert.That(v.Eleme(1), Is.True); -// Assert.That(v.Eleme(2), Is.True); -// Assert.That(v.Eleme(3), Is.True); -// }); -// } -// [TestCase] -// public void Torles() -// { -// Halmaz v = new FaHalmaz(); -// v.Beszur(1); -// v.Beszur(3); -// v.Beszur(2); -// v.Torol(2); -// Assert.Multiple(() => -// { -// Assert.That(v.Eleme(1), Is.True); -// Assert.That(v.Eleme(2), Is.False); -// Assert.That(v.Eleme(3), Is.True); -// }); -// } -// [TestCase] -// public void TorlesUres() -// { -// Halmaz v = new FaHalmaz(); -// v.Beszur(""); -// v.Beszur("1"); -// v.Beszur(""); -// v.Torol(""); -// Assert.Multiple(() => -// { -// Assert.That(v.Eleme(""), Is.False); -// Assert.That(v.Eleme("1"), Is.True); -// }); -// } -// [TestCase] -// public void NemletezoTorles() -// { -// Halmaz v = new FaHalmaz(); -// v.Beszur(1); -// v.Beszur(3); -// v.Beszur(2); -// Assert.Throws(() => v.Torol(0)); -// Assert.Multiple(() => -// { -// Assert.That(v.Eleme(1), Is.True); -// Assert.That(v.Eleme(2), Is.True); -// Assert.That(v.Eleme(3), Is.True); -// }); -// } -// [TestCase] -// public void DuplaTorles() -// { -// Halmaz v = new FaHalmaz(); -// v.Beszur(1); -// v.Beszur(2); -// v.Beszur(3); -// v.Beszur(2); -// v.Torol(2); -// Assert.Multiple(() => -// { -// Assert.That(v.Eleme(1), Is.True); -// Assert.That(v.Eleme(2), Is.False); -// Assert.That(v.Eleme(3), Is.True); -// Assert.That(v.Eleme(4), Is.False); -// }); -// } -// [TestCase] -// public void PreorderBejaras() -// { -// Halmaz v = new FaHalmaz(); -// v.Beszur(5); -// v.Beszur(3); -// v.Beszur(1); -// v.Beszur(8); -// v.Beszur(4); -// v.Beszur(9); -// v.Beszur(7); -// string osszefuzo = ""; -// v.Bejar(x => osszefuzo += x); -// Assert.That(osszefuzo, Is.EqualTo("5314879")); -// } -// } -//} +namespace OE.ALGA.Tesztek.Adatszerkezetek +{ + [TestFixture(Category = "Adatszerkezetek", TestName = "05 - Fa Halmaz Tesztek")] + public class FaHalmazTesztek + { + [TestCase] + public void BeszurasUres() + { + Halmaz v = new FaHalmaz(); + v.Beszur(""); + Assert.Multiple(() => + { + Assert.That(v.Eleme(""), Is.True); + }); + } + [TestCase] + public void Beszuras() + { + Halmaz v = new FaHalmaz(); + v.Beszur(1); + v.Beszur(3); + v.Beszur(2); + Assert.Multiple(() => + { + Assert.That(v.Eleme(1), Is.True); + Assert.That(v.Eleme(2), Is.True); + Assert.That(v.Eleme(3), Is.True); + }); + } + [TestCase] + public void DuplaBeszuras() + { + Halmaz v = new FaHalmaz(); + v.Beszur(1); + v.Beszur(2); + v.Beszur(3); + v.Beszur(2); + Assert.Multiple(() => + { + Assert.That(v.Eleme(1), Is.True); + Assert.That(v.Eleme(2), Is.True); + Assert.That(v.Eleme(3), Is.True); + }); + } + [TestCase] + public void Torles() + { + Halmaz v = new FaHalmaz(); + v.Beszur(1); + v.Beszur(3); + v.Beszur(2); + v.Torol(2); + Assert.Multiple(() => + { + Assert.That(v.Eleme(1), Is.True); + Assert.That(v.Eleme(2), Is.False); + Assert.That(v.Eleme(3), Is.True); + }); + } + [TestCase] + public void TorlesUres() + { + Halmaz v = new FaHalmaz(); + v.Beszur(""); + v.Beszur("1"); + v.Beszur(""); + v.Torol(""); + Assert.Multiple(() => + { + Assert.That(v.Eleme(""), Is.False); + Assert.That(v.Eleme("1"), Is.True); + }); + } + [TestCase] + public void NemletezoTorles() + { + Halmaz v = new FaHalmaz(); + v.Beszur(1); + v.Beszur(3); + v.Beszur(2); + Assert.Throws(() => v.Torol(0)); + Assert.Multiple(() => + { + Assert.That(v.Eleme(1), Is.True); + Assert.That(v.Eleme(2), Is.True); + Assert.That(v.Eleme(3), Is.True); + }); + } + [TestCase] + public void DuplaTorles() + { + Halmaz v = new FaHalmaz(); + v.Beszur(1); + v.Beszur(2); + v.Beszur(3); + v.Beszur(2); + v.Torol(2); + Assert.Multiple(() => + { + Assert.That(v.Eleme(1), Is.True); + Assert.That(v.Eleme(2), Is.False); + Assert.That(v.Eleme(3), Is.True); + Assert.That(v.Eleme(4), Is.False); + }); + } + [TestCase] + public void PreorderBejaras() + { + Halmaz v = new FaHalmaz(); + v.Beszur(5); + v.Beszur(3); + v.Beszur(1); + v.Beszur(8); + v.Beszur(4); + v.Beszur(9); + v.Beszur(7); + string osszefuzo = ""; + v.Bejar(x => osszefuzo += x); + Assert.That(osszefuzo, Is.EqualTo("5314879")); + } + } +}