public static void Read(ref Set sc, StreamReader rd) { string s = rd.ReadToEnd(); string[] t = s.Split(' '); for (int i = 0; i < t.Length; i++) sc.Add(int.Parse(t[i])); }
public static Set operator /(Set s1, Set s2) { Set s = new Set(); for (int i = 0; i < s1.elements.Count; i++) if (s2.Find(s1.elements[i]) == -1) s.Add(s1.elements[i]); return s; }
public static Set operator |(Set s1, Set s2) { Set s = new Set(); int i = 0, j = 0; for (; (i < s1.elements.Count) && (j < s2.elements.Count); ) if (s1.elements[i] < s2.elements[j]) { s.Add(s1.elements[i]); i++; } else { s.Add(s2.elements[j]); j++; } for (; i < s1.elements.Count; i++) s.Add(s1.elements[i]); for (; j < s2.elements.Count; j++) s.Add(s2.elements[j]); return s; }
public static Set operator -(Set s1, Set s2) { Set s = new Set(); s1.elements.Sort(); s2.elements.Sort(); int i = 0, j = 0; for (; (i < s1.elements.Count) && (j < s2.elements.Count); ) { if (s1.elements[i] == s2.elements[j]) { i++; j++; } else { for (; (j < s2.elements.Count) && (s1.elements[i] > s2.elements[j]); j++) ; if (s1.elements[i] < s2.elements[j]) { s.Add(s1.elements[i]); i++; } } } return s; }
public static void Write(Set sc, StreamWriter wr) { wr.WriteLine("Elements of set:\r\n" + sc.ToString()); sc.Elements.Clear(); }
static void Main(string[] args) { StreamWriter wr = new StreamWriter("result.txt"); Set scl = new Set(); Set scl1 = new Set(); int n = 0; while (n != 7) { n = Visualisation.Choice(); if (n != 7) { switch (n) { case 1: { StreamReader rd = new StreamReader("set.txt"); StreamReader rd1 = new StreamReader("set1.txt"); Visualisation.Read(ref scl, rd); Visualisation.Read(ref scl1, rd1); Set t = scl | scl1; wr.WriteLine("First set:"); Visualisation.Write(scl, wr); wr.WriteLine("Second set:"); Visualisation.Write(scl1, wr); wr.WriteLine("Result:"); Visualisation.Write(t, wr); } break; case 2: { StreamReader rd = new StreamReader("set.txt"); StreamReader rd1 = new StreamReader("set1.txt"); Visualisation.Read(ref scl, rd); Visualisation.Read(ref scl1, rd1); Set t = scl & scl1; wr.WriteLine("First set:"); Visualisation.Write(scl, wr); wr.WriteLine("Second set:"); Visualisation.Write(scl1, wr); wr.WriteLine("Result:"); Visualisation.Write(t, wr); } break; case 3: { StreamReader rd = new StreamReader("set.txt"); StreamReader rd1 = new StreamReader("set1.txt"); Visualisation.Read(ref scl, rd); Visualisation.Read(ref scl1, rd1); Set t = scl / scl1; wr.WriteLine("First set:"); Visualisation.Write(scl, wr); wr.WriteLine("Second set:"); Visualisation.Write(scl1, wr); wr.WriteLine("Result:"); Visualisation.Write(t, wr); } break; case 4: { StreamReader rd = new StreamReader("set.txt"); StreamReader rd1 = new StreamReader("set1.txt"); Visualisation.Read(ref scl, rd); Visualisation.Read(ref scl1, rd1); Set t = scl + scl1; wr.WriteLine("First set:"); Visualisation.Write(scl, wr); wr.WriteLine("Second set:"); Visualisation.Write(scl1, wr); wr.WriteLine("Result:"); Visualisation.Write(t, wr); } break; case 5: { StreamReader rd = new StreamReader("set.txt"); StreamReader rd1 = new StreamReader("set1.txt"); Visualisation.Read(ref scl, rd); Visualisation.Read(ref scl1, rd1); Set t = scl * scl1; wr.WriteLine("First set:"); Visualisation.Write(scl, wr); wr.WriteLine("Second set:"); Visualisation.Write(scl1, wr); wr.WriteLine("Result:"); Visualisation.Write(t, wr); } break; case 6: { StreamReader rd = new StreamReader("set.txt"); StreamReader rd1 = new StreamReader("set1.txt"); Visualisation.Read(ref scl, rd); Visualisation.Read(ref scl1, rd1); Set t = scl - scl1; wr.WriteLine("First set:"); Visualisation.Write(scl, wr); wr.WriteLine("Second set:"); Visualisation.Write(scl1, wr); wr.WriteLine("Result:"); Visualisation.Write(t, wr); } break; } } } wr.Close(); }