DiscreteMathematics/Lab_2/window_4.py

142 lines
4.1 KiB
Python
Raw Normal View History

2024-03-09 17:44:57 +02:00
from PyQt5 import QtCore, QtWidgets
def unite(*a):
b = []
for i in a:
for j in i:
if j not in b:
b.append(j)
return b
def intersect(*a):
b = []
for i in a:
for j in i:
if all([j in k for k in a]):
b.append(j)
def difference(a, b):
c = []
for i in a:
if i not in b:
c.append(i)
return c
def universal_set(set_X, set_Y):
universal = []
for x in set_X:
for y in set_Y:
universal.append((x, y))
return universal
def reverse(relation):
res = []
for i in relation:
res.append(i[::-1])
return res
def form_table(relation, label):
from prettytable import PrettyTable
table = PrettyTable()
if relation:
a = [i[0] for i in relation]
b = [i[1] for i in relation]
matrix = [[0 for j in range(len(a))] for i in range(len(a))]
for i in relation:
m = a.index(i[0])
n = b.index(i[1])
matrix[m][n] = 1
if len(set([" "] + a)) < len([" "] + a):
table.field_names = [i for i in range(len(matrix) + 1)]
table.add_row([" "] + a)
else:
table.field_names = [" "] + a
for i in range(len(matrix)):
table.add_row([b[i]] + matrix[i])
else:
table.field_names = [" "]
label.setText(table.get_string())
class Window(QtWidgets.QWidget):
def __init__(self):
super(QtWidgets.QWidget, self).__init__()
self.setWindowTitle("Вікно 4")
self.gridLayout = QtWidgets.QGridLayout(self)
self.label = QtWidgets.QLabel(self)
self.label.setText("Оберіть операцію")
self.label.setAlignment(QtCore.Qt.AlignCenter)
self.gridLayout.addWidget(self.label, 0, 0, 1, 2)
self.button_union = QtWidgets.QPushButton(self)
self.button_union.setText("R S")
self.gridLayout.addWidget(self.button_union, 1, 0, 1, 1)
self.button_intersection = QtWidgets.QPushButton(self)
self.button_intersection.setText("R ∩ S")
self.gridLayout.addWidget(self.button_intersection, 2, 0, 1, 1)
self.button_difference = QtWidgets.QPushButton(self)
self.button_difference.setText("R \\ S")
self.gridLayout.addWidget(self.button_difference, 3, 0, 1, 1)
self.button_universal_difference = QtWidgets.QPushButton(self)
self.button_universal_difference.setText("U \\ R")
self.gridLayout.addWidget(self.button_universal_difference, 4, 0, 1, 1)
self.button_reverse = QtWidgets.QPushButton(self)
self.button_reverse.setText("S⁻¹")
self.gridLayout.addWidget(self.button_reverse, 5, 0, 1, 1)
self.tableView_relation = QtWidgets.QLabel(self)
self.tableView_relation.setAlignment(QtCore.Qt.AlignCenter)
self.gridLayout.addWidget(self.tableView_relation, 1, 1, 5, 1)
QtCore.QMetaObject.connectSlotsByName(self)
self.set_functions()
self.set_R = set()
self.set_S = set()
self.set_A = set()
self.set_B = set()
def set_functions(self):
self.button_union.clicked.connect(lambda: form_table(unite(self.set_R, self.set_S), self.tableView_relation))
self.button_intersection.clicked.connect(lambda: form_table(intersect(self.set_R, self.set_S),
self.tableView_relation))
self.button_difference.clicked.connect(lambda: form_table(difference(self.set_R, self.set_S),
self.tableView_relation))
self.button_universal_difference.clicked.connect(lambda: form_table(
difference(universal_set(self.set_A, self.set_B), self.set_R), self.tableView_relation)
)
self.button_reverse.clicked.connect(lambda: form_table(reverse(self.set_S), self.tableView_relation))
def set_sets(self, granddaughter, godmother, set_A, set_B):
self.set_R = granddaughter
self.set_S = godmother
self.set_A = set_A
self.set_B = set_B