Visualiseur de recherche Fibonacci utilisant PyQt5

from PyQt5.QtWidgets import *

from PyQt5 import QtCore, QtGui

from PyQt5.QtGui import *

from PyQt5.QtCore import *

import sys

class Window(QMainWindow):

    

    number = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15]

    

    desired = 14

    def __init__(self):

        super().__init__()

        

        self.setWindowTitle("Fibonacci Search")

        

        self.setGeometry(100, 100, 600, 400)

        

        self.UiComponents()

        

        self.show()

    

    def UiComponents(self):

        

        self.start = False

        

        self.divide = False

        self.fib_search = True

        

        self.label_list = []

        

        self.fib1 = 1

        self.fib2 = 0

        self.fib = self.fib1 + self.fib2

        self.offset = -1

        

        c = 0

        

        for i in self.number:

            

            label = QLabel(str(i), self)

            

            label.setStyleSheet("border : 1px solid black;

                                 background : white;")

            

            label.setAlignment(Qt.AlignTop)

            

            

            

            

            label.setGeometry(50 + c * 30, 50, 20, i * 10 + 10)

            

            self.label_list.append(label)

            

            c = c + 1

        

        self.search_button = QPushButton("Start Search", self)

        

        self.search_button.setGeometry(100, 270, 100, 30)

        

        self.search_button.clicked.connect(self.search_action)

        

        pause_button = QPushButton("Pause", self)

        

        pause_button.setGeometry(100, 320, 100, 30)

        

        pause_button.clicked.connect(self.pause_action)

        

        self.result = QLabel("To search : " + str(self.desired), self)

        

        self.result.setGeometry(320, 280, 250, 40)

        

        self.result.setStyleSheet("border : 3px solid black;")

        

        self.result.setFont(QFont('Times', 10))

        

        self.result.setAlignment(Qt.AlignCenter)

        

        timer = QTimer(self)

        

        timer.timeout.connect(self.showTime)

        

        timer.start(300)

    

    def showTime(self):

        

        if self.start:

            

            if self.fib_search:

                

                

                if self.fib < len(self.number):

                    self.fib2 = self.fib1

                    self.fib1 = self.fib

                    self.fib = self.fib2 + self.fib1

                    self.result.setText("Searching Fibonacci number >=" +

                                                        str(self.desired))

                

                else:

                    self.result.setText("Fibonacci found, searching number")

                    self.fib_search = False

                    self.divide = True

            

            if self.divide:

                if self.fib <= 1:

                    self.result.setText("Not found")

                    self.start = False

                    return

                i = min(self.offset + self.fib2, len(self.number) - 1)

                self.label_list[i].setStyleSheet("border : 1px solid black;"

                                                 "background-color : grey")

                

                

                

                if (self.number[i] < self.desired):

                    self.fib = self.fib1

                    self.fib1 = self.fib2

                    self.fib2 = self.fib - self.fib1

                    self.offset = i

                

                

                

                elif (self.number[i] > self.desired):

                    self.fib = self.fib2

                    self.fib1 = self.fib1 - self.fib2

                    self.fib2 = self.fib - self.fib1

                

                else:

                    self.result.setText("Found at : " + str(i))

                    self.label_list[i].setStyleSheet(

                                 "border : 2px solid green;"

                                 "background-color : lightgreen;")

                    self.start = False

    

    def search_action(self):

        

        self.start = True

        

        self.result.setText("Started searching...")

    

    def pause_action(self):

        

        self.start = False

        

        self.result.setText("Paused")

App = QApplication(sys.argv)

window = Window()

sys.exit(App.exec())

Laisser un commentaire

Votre adresse e-mail ne sera pas publiée. Les champs obligatoires sont indiqués avec *

Aller en haut