Skip to content

Instantly share code, notes, and snippets.

@somahargitai
Created March 6, 2017 22:09
Show Gist options
  • Save somahargitai/c2d6f43dd44ccf020c79dbb1c2ae42da to your computer and use it in GitHub Desktop.
Save somahargitai/c2d6f43dd44ccf020c79dbb1c2ae42da to your computer and use it in GitHub Desktop.
NumberSets
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace NumberSets
{
class Program
{
static void Main(string[] args)
{
CustomSet pozitív_páros_számok = new CustomSet(new List<Delegate>{
new Func< int, bool > (Divide2),
new Func< int, bool > (GreaterThan0) });
Console.WriteLine("A 3 eleme-e a halmaznak? Válasz:" + pozitív_páros_számok.IsElement(3).ToString());
Console.WriteLine("A 7 eleme-e a halmaznak? Válasz:" + pozitív_páros_számok.IsElement(7).ToString());
Console.WriteLine("A 16 eleme-e a halmaznak? Válasz:" + pozitív_páros_számok.IsElement(16).ToString());
Console.WriteLine("A 0 eleme-e a halmaznak? Válasz:" + pozitív_páros_számok.IsElement(0).ToString());
Console.WriteLine("Új elemek hozzáadása: 1, 2 , 3, 4, 5, 6, 7");
pozitív_páros_számok.Add(new int[] { 1, 2 , 3, 4, 5, 6, 7});
Console.WriteLine("A 3 eleme-e a halmaznak? Válasz:" + pozitív_páros_számok.IsElement(3).ToString());
Console.WriteLine("Az 5 eleme-e a halmaznak? Válasz:" + pozitív_páros_számok.IsElement(5).ToString());
Console.ReadLine();
}
public static bool Divide2 (int number)
{
if(number % 2 == 0) { return true; }
else { return false; }
}
public static bool GreaterThan0(int number)
{
if (number > 0 ) { return true; }
else { return false; }
}
}
class CustomSet
{
public CustomSet(List<Delegate> conditions)
{
int num = 0;
foreach(Delegate condition in conditions)
{
setConditions.Add(num, condition);
num++;
}
}
private Dictionary<int, Delegate> setConditions = new Dictionary<int, Delegate>();
private HashSet<int> otherElements = new HashSet<int>();
public bool IsElement( int number )
{
bool allConditionTrue = true;
for (int condition_ci = 0; condition_ci < setConditions.Count; condition_ci++)
{
if (!(bool)setConditions[condition_ci].DynamicInvoke(number))
{
allConditionTrue = false;
break;
}
}
if (allConditionTrue)
return true;
if (otherElements.Contains(number))
return true;
return false;
}
public bool Add( int[] newElements )
{
for (int newElements_ci = 0; newElements_ci < newElements.Length; newElements_ci++)
{
for (int condition_ci = 0; condition_ci < setConditions.Count; condition_ci++)
{
if ((bool)setConditions[condition_ci].DynamicInvoke(newElements[newElements_ci]))
break;
}
if (!otherElements.Contains(newElements[newElements_ci]))
{
otherElements.Add(newElements[newElements_ci]);
}
}
return true;
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment