bd_lab1
unknown
python
3 years ago
4.0 kB
7
Indexable
import sys
from PyQt5.QtWidgets import QApplication, QWidget, QLabel, QLineEdit, QPushButton, QTableWidget, QTableWidgetItem, QVBoxLayout, QHBoxLayout
"""
23. Дан одномерный массив Xn.
Найти произведение из нечетных по значению элементов массива.
Сформировать новый массив из элементов массива Xn, значения которых больше В
"""
class ArrayApp(QWidget):
def __init__(self):
super().__init__()
# Create widgets
self.n_label = QLabel('Введите число элементов массива:')
self.n_edit = QLineEdit()
self.create_table_button = QPushButton('Создать таблицу')
self.array_table = QTableWidget()
self.summary_label = QLabel('Произведение нечетных элементов массива:')
self.summary_field = QLineEdit()
# Create widgets for B value
self.b_label = QLabel('Введите число B:')
self.b_edit = QLineEdit()
self.filter_button = QPushButton('Отфильтровать')
self.filtered_table = QTableWidget()
# Set layout
layout = QVBoxLayout()
layout.addWidget(self.n_label)
layout.addWidget(self.n_edit)
layout.addWidget(self.create_table_button)
layout.addWidget(self.array_table)
layout.addWidget(self.summary_label)
layout.addWidget(self.summary_field)
# Create layout for B value widgets
b_layout = QHBoxLayout()
b_layout.addWidget(self.b_label)
b_layout.addWidget(self.b_edit)
b_layout.addWidget(self.filter_button)
layout.addLayout(b_layout)
layout.addWidget(self.filtered_table)
self.setLayout(layout)
# Connect button signals to slots
self.create_table_button.clicked.connect(self.create_table)
self.filter_button.clicked.connect(self.filter_table)
def create_table(self):
n = int(self.n_edit.text())
self.array_table.setRowCount(1)
self.array_table.setColumnCount(n)
for i in range(n):
item = QTableWidgetItem()
self.array_table.setItem(0, i, item)
self.array_table.resizeColumnsToContents()
# Connect table signal to slot
self.array_table.cellChanged.connect(self.update_summary)
def update_summary(self):
n = self.array_table.columnCount()
total = 1
for i in range(n):
item = self.array_table.item(0, i)
if item is not None and item.text() != '' and float(item.text()) % 2 == 1:
total *= float(item.text())
if total == 1:
self.summary_field.setText('')
else:
self.summary_field.setText(str(total))
def filter_table(self):
b = float(self.b_edit.text())
# Get array elements greater than B
n_cols = self.array_table.columnCount()
filtered_items = []
for i in range(n_cols):
item = self.array_table.item(0, i)
if item is not None and item.text() != '':
value = float(item.text())
if value > b:
filtered_items.append(value)
# Set up filtered table
n_cols = len(filtered_items)
self.filtered_table.setRowCount(1)
self.filtered_table.setColumnCount(n_cols)
for i in range(n_cols):
item = QTableWidgetItem(str(filtered_items[i]))
self.filtered_table.setItem(0, i, item)
self.filtered_table.resizeColumnsToContents()
if __name__ == '__main__':
app = QApplication(sys.argv)
array_app = ArrayApp()
array_app.setGeometry(100, 100, 500, 300)
array_app.setWindowTitle("Лабораторная работа 1. Шевчук МВА-220")
array_app.show()
sys.exit(app.exec_())
Editor is loading...