Files
ALGA/ALGA/Paradigmak/01_ImperativParadigma.cs
Lendaia Mirai 887f659b57 commit
2025-09-25 15:46:23 +02:00

152 lines
3.1 KiB
C#

using System;
using System.Collections;
using System.Collections.Generic;
namespace OE.ALGA.Paradigmak
{
public interface IVegrehajthato
{
void Vegrehajtas();
}
public interface IFuggo
{
public bool FuggosegTeljesul
{
get;
}
}
public class TaroloMegteltKivetel : Exception
{
}
public class FeladatTarolo<T> : IEnumerable<T> where T : IVegrehajthato
{
internal T[] tarolo;
internal int n;
public FeladatTarolo(int l)
{
tarolo = new T[l];
n = 0;
}
public void Felvesz(T t)
{
if (tarolo.Length == 0)
{
throw new TaroloMegteltKivetel();
}
else
{
if (n < tarolo.Length)
{
tarolo[n] = t;
n++;
}
else
{
throw new TaroloMegteltKivetel();
}
}
}
public IEnumerator GetEnumerator()
{
foreach (T t in tarolo)
{
if (t != null)
{
yield return t;
}
}
}
virtual public void MindentVegrehajt()
{
foreach (T t in tarolo)
{
if (t != null)
{
t.Vegrehajtas();
}
}
}
IEnumerator<T> IEnumerable<T>.GetEnumerator()
{
FeladatTaroloBejaro<T> bejaro = new FeladatTaroloBejaro<T>(tarolo, n);
return bejaro;
}
}
public class FuggoFeladatTarolo<T> : FeladatTarolo<T>, IEnumerable<T> where T : IVegrehajthato, IFuggo
{
public FuggoFeladatTarolo(int l) : base(l)
{
}
override public void MindentVegrehajt()
{
foreach (T t in tarolo)
{
if (t != null)
{
if (t.FuggosegTeljesul)
{
t.Vegrehajtas();
}
}
}
}
}
public class FeladatTaroloBejaro<T> : IEnumerator<T>
{
T[] tarolo;
int n;
int aktualisindex;
public FeladatTaroloBejaro(T[] tarolo, int n)
{
this.tarolo = tarolo;
this.n = n;
aktualisindex = 0;
}
public T Current
{
get { return tarolo[aktualisindex]; }
}
object IEnumerator.Current => throw new NotImplementedException();
public void Dispose()
{
throw new NotImplementedException();
}
public bool MoveNext()
{
if (aktualisindex < n-1)
{
aktualisindex++;
return true;
}
else
{
return false;
}
}
public void Reset()
{
aktualisindex = -1;
}
}
}