1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
| using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace Курсова_ООП_Марина
{
public class Multitude<T>
{
private List<T> _list;
Multitude()
{
_list = new List<T>();
}
public Multitude(params T[] args)
: this()
{
_list.AddRange(args);
}
public Multitude(IEnumerable<T> mas)
: this()
{
_list.AddRange(mas);
}
public void Add(T elem)
{
_list.Add(elem);
}
public void Delete(T elem)
{
_list.Remove(elem);
}
public Multitude<T> Intersect(Multitude<T> Source)
{
return new Multitude<T>(_list.Intersect(Source._list));
}
public Multitude<T> Union(Multitude<T> Source)
{
return new Multitude<T>(_list.Union(Source._list));
}
public Multitude<T> Except(Multitude<T> Source)
{
return new Multitude<T>(_list.Except(Source._list));
}
public override string ToString()
{
return string.Join(",", _list);
}
}
class Program
{
private static int Error()
{
int checkError;
while (true)
{
try
{
checkError = Convert.ToInt16(Console.ReadLine());
break;
}
catch
{
Console.WriteLine("\nПомилка введення! Повторiть спробу!");
}
}
return checkError;
}
static void Main(string[] args)
{
int sizeMn1, sizeMn2, menu, choice, element;
Console.WriteLine("_____________Меню:_____________");
Console.WriteLine("1. Додати елемент в множину");
Console.WriteLine("2. Видалити елемент iз множини");
Console.WriteLine("3. Знайти перетин множин");
Console.WriteLine("4. Знайти об'єднання множин");
Console.WriteLine("5. Знайти рiзницю множин");
Console.WriteLine("6. Вихiд");
Console.WriteLine();
Console.WriteLine("Введiть розмiр першої множини");
sizeMn1 = Error();
Console.WriteLine("Введiть елементи першої множини\n");
int[] firstSet = new int[sizeMn1];
for (int i = 0; i < sizeMn1; i++)
{
Console.Write("Елемент[{0}] = ", i);
firstSet[i] = Error();
}
Console.WriteLine("\nВведiть розмiр другої множини");
sizeMn2 = Error();
Console.WriteLine("Введiть елементи другої множини\n");
int[] secondSet = new int[sizeMn2];
for (int i = 0; i < sizeMn2; i++)
{
Console.Write("Елемент[{0}] = ", i);
secondSet[i] = Error();
}
Multitude<int> _mt1 = new Multitude<int>(firstSet);
Console.WriteLine("Множина 1: {0}", _mt1);
Multitude<int> _mt2 = new Multitude<int>(secondSet);
Console.WriteLine("Множина 2: {0}", _mt2);
do
{
Console.WriteLine("\nВиберiть, що потрiбно зробити\n");
menu = Error();
switch (menu)
{
case 1:
Console.WriteLine("Виберiть множину");
choice = Error();
Console.WriteLine("Введiть новий елемент");
element = Error();
if (choice == 1)
{
_mt1.Add(element);
Console.WriteLine("Множина пiсля додавання елементу: {0}", _mt1);
}
else
{
_mt2.Add(element);
Console.WriteLine("Множина пiсля додавання елементу: {0}", _mt2);
}
break;
case 2:
Console.WriteLine("Виберiть множину");
choice = Error();
Console.WriteLine("Введiть елемент, який потрiбно видалити");
element = Error();
if (choice == 1)
{
_mt1.Delete(element);
Console.WriteLine("Множина пiсля видалення елементу: {0}", _mt1);
}
else
{
_mt2.Delete(element);
Console.WriteLine("Множина пiсля видалення елементу: {0}", _mt2);
}
break;
case 3:
Console.WriteLine("Перетин множин: {0}", _mt1.Intersect(_mt2));
break;
case 4:
Console.WriteLine("Об'єднання множин: {0}", _mt1.Union(_mt2));
break;
case 5:
Console.WriteLine("Рiзниця множин: {0}", _mt1.Except(_mt2));
break;
}
} while (menu != 6);
}
}
} |