DiscreteMathematics/Lab_2/window_3.py

125 lines
4.7 KiB
Python
Raw Normal View History

2024-03-09 17:44:57 +02:00
from PyQt5 import QtCore, QtWidgets
def form_table(relation, label):
from prettytable import PrettyTable
table = PrettyTable()
a = [i[0] for i in relation]
b = [i[1] for i in relation]
# print(f"A: {a}\nB: {b}")
matrix = [[0 for j in range(len(b))] for i in range(len(a))]
for i in relation:
m = a.index(i[0])
n = b.index(i[1])
matrix[m][n] = 1
table.field_names = [" "] + a
for i in range(len(matrix)):
table.add_row([b[i]] + matrix[i])
label.setText(table.get_string())
class Window(QtWidgets.QWidget):
def __init__(self):
super(QtWidgets.QWidget, self).__init__()
self.setWindowTitle("Вікно 3")
self.gridLayout = QtWidgets.QGridLayout(self)
self.label_A = QtWidgets.QLabel(self)
self.label_A.setText("A")
self.label_A.setAlignment(QtCore.Qt.AlignCenter)
self.gridLayout.addWidget(self.label_A, 0, 0, 1, 1)
self.label_B = QtWidgets.QLabel(self)
self.label_B.setText("B")
self.label_B.setAlignment(QtCore.Qt.AlignCenter)
self.gridLayout.addWidget(self.label_B, 0, 1, 1, 1)
self.list_A = QtWidgets.QLabel(self)
self.gridLayout.addWidget(self.list_A, 1, 0, 1, 1)
self.list_B = QtWidgets.QLabel(self)
self.gridLayout.addWidget(self.list_B, 1, 1, 1, 1)
self.label_grand_daughter = QtWidgets.QLabel(self)
self.label_grand_daughter.setText("Внучка")
self.label_grand_daughter.setAlignment(QtCore.Qt.AlignCenter)
self.gridLayout.addWidget(self.label_grand_daughter, 2, 0, 1, 2)
self.table_grand_daughter = QtWidgets.QLabel(self)
self.table_grand_daughter.setAlignment(QtCore.Qt.AlignCenter)
self.gridLayout.addWidget(self.table_grand_daughter, 3, 0, 1, 2)
self.label_godmother = QtWidgets.QLabel(self)
self.label_godmother.setText("Хрещена мати")
self.label_godmother.setAlignment(QtCore.Qt.AlignCenter)
self.gridLayout.addWidget(self.label_godmother, 4, 0, 1, 2)
self.table_godmother = QtWidgets.QLabel(self)
self.table_godmother.setAlignment(QtCore.Qt.AlignCenter)
self.gridLayout.addWidget(self.table_godmother, 5, 0, 1, 2)
QtCore.QMetaObject.connectSlotsByName(self)
self.set_A = set()
self.set_B = set()
self.female_names = set()
self.male_names = set()
self.grandparent_granddaughter_relation = set()
self.godmother_godchild_relation = set()
def set_sets(self, set_A, set_B, female_names, male_names):
self.set_A = set_A
self.set_B = set_B
self.female_names = female_names
self.male_names = male_names
self.list_A.setText("\n".join(set_A))
self.list_B.setText("\n".join(set_B))
def form_relations(self):
for grandparent in self.set_A:
for granddaughter in self.set_B:
if (
grandparent != granddaughter
and granddaughter in self.female_names
and (granddaughter, grandparent) not in self.grandparent_granddaughter_relation
and (grandparent, granddaughter) not in self.grandparent_granddaughter_relation
and not any(grandparent == i[0] for i in self.grandparent_granddaughter_relation)
and not any(granddaughter == i[1] for i in self.grandparent_granddaughter_relation)
):
self.grandparent_granddaughter_relation.add((grandparent, granddaughter))
for godmother in self.set_A:
for godchild in self.set_B:
if (
godmother in self.female_names
and godmother != godchild
and (godmother, godchild) not in self.grandparent_granddaughter_relation
and (godchild, godmother) not in self.grandparent_granddaughter_relation
and (godchild, godmother) not in self.godmother_godchild_relation
and (godmother, godchild) not in self.godmother_godchild_relation
and not any(godchild == i[1] for i in self.godmother_godchild_relation)
and not any(godmother == i[0] for i in self.godmother_godchild_relation)
):
self.godmother_godchild_relation.add((godmother, godchild))
form_table(self.grandparent_granddaughter_relation, self.table_grand_daughter)
form_table(self.godmother_godchild_relation, self.table_godmother)
def get_granddaughter(self):
return self.grandparent_granddaughter_relation
def get_godmother(self):
return self.godmother_godchild_relation