İkinci dereceden denklemin köklerini bulan küçük bir betik

Başlatan guopx, 22 Aralık 2013 - 00:15:08

« önceki - sonraki »

0 Üyeler ve 2 Ziyaretçi konuyu incelemekte.

guopx

#!/usr/bin/env python
from PyQt4 import QtCore, QtGui
from PyQt4.QtGui import *
import sys
import math


class YeniPencere(QMainWindow):
    def __init__(self):
        super(YeniPencere, self).__init__()
        self.cw = QWidget(self)
        self.initUI()

    def initUI(self):   
        self.setWindowTitle("Denklem Cozucu")
        self.resize(600, 400)
        etiket = QtGui.QLabel("Hesaplanmasini istediginiz denklemin katsayilarini yerlestiriniz")
        etiket.setStyleSheet('color: red; font: bold 14px')
        Button = QtGui.QPushButton("Kapat")
        Button.clicked.connect(QtCore.QCoreApplication.instance().quit)
        global a
        a = QtGui.QLineEdit(self)
        global b
        b = QtGui.QLineEdit(self)
        global c
        c = QtGui.QLineEdit(self)
        a.setStyleSheet('color: red; font: bold 40px')
        b.setStyleSheet('color: red; font: bold 40px')
        c.setStyleSheet('color: red; font: bold 40px')
        a.textChanged[str].connect(self.hesapla)
        b.textChanged[str].connect(self.hesapla)
        c.textChanged[str].connect(self.hesapla)
       
        x2 =  QtGui.QLabel("x^2+")
        x2.setStyleSheet('color: blue; font: bold 40px')
        x = QtGui.QLabel("x+")
        x.setStyleSheet('color: blue; font: bold 40px')

        hbox1 = QtGui.QHBoxLayout()
        hbox1.addStretch(1)
        hbox1.addWidget(Button)
       
        hbox2 = QtGui.QHBoxLayout()
        hbox2.addStretch(1)
        hbox2.addWidget(a)
        hbox2.addWidget(x2)
        hbox2.addWidget(b)
        hbox2.addWidget(x)
        hbox2.addWidget(c)
       
        kok1 = QtGui.QLabel("x1=")
        kok1.setStyleSheet('color: blue; font: bold 40px')
     
        global hes1
        hes1 = QtGui.QLabel("katsayi gir")
        hes1.setStyleSheet('color: red; font: bold 40px')

        kok2 = QtGui.QLabel("x2=")
        kok2.setStyleSheet('color: blue; font: bold 40px')
       
        global hes2
        hes2 = QtGui.QLabel("katsayi gir")
        hes2.setStyleSheet('color: red; font: bold 40px')

        global durum
        durum = QtGui.QLabel("katsayi gir")
        durum.setStyleSheet('color: red; font: bold 15px')

        hbox3 = QtGui.QHBoxLayout()
        hbox3.addWidget(kok1)
        hbox3.addWidget(hes1)
       
        hbox4 = QtGui.QHBoxLayout()
        hbox4.addWidget(kok2)
        hbox4.addWidget(hes2)
       
        hbox5 = QtGui.QHBoxLayout()
        hbox5.addWidget(durum)

        vbox = QtGui.QVBoxLayout()
        #vbox.addStretch(1)
        vbox.addWidget(etiket)
        vbox.addLayout(hbox2)
        vbox.addLayout(hbox3)
        vbox.addLayout(hbox4)
        vbox.addLayout(hbox5)
        vbox.addLayout(hbox1)

        self.cw.setLayout(vbox)
        self.setCentralWidget(self.cw)   
        self.show()
    def hesapla(self):
        try:
            control = int(a.text()),int(b.text()),int(c.text())
            delta = (int(b.text())**2)-4*int(a.text())*int(c.text())
            print delta
            if delta < 0:
               durum.setText("Diskriminant negatif, reel kok yok, kokler karmasik sayi.")
               hes1.setText("("+"-"+"%s"%(int(b.text()))+"+"+"karekok"+"("+"%s"%(math.fabs(delta))+")"+"i"+")"+"/"+"%s"%(2*int(a.text())))
               hes2.setText("("+"-"+"%s"%(int(b.text()))+"-"+"karekok"+"("+"%s"%(math.fabs(delta))+")"+"i"+")"+"/"+"%s"%(2*int(a.text())))
            if delta == 0:
               durum.setText("Diskriminant sifir, cift katli tek bir reel kok var.")
               hes1.setText("%s"%(((-1*int(b.text()))+(math.sqrt(delta)))/(2*int(a.text()))))
               hes2.setText("%s"%(((-1*int(b.text()))-(math.sqrt(delta)))/(2*int(a.text()))))

            if delta > 0:
               durum.setText("Diskriminant pozitif, iki farkli reel kok var.")
               if math.sqrt(delta) == int(math.sqrt(delta)):
                  hes1.setText("%s"%(((-1*int(b.text()))+(math.sqrt(delta)))/(2*int(a.text()))))
                  hes2.setText("%s"%(((-1*int(b.text()))-(math.sqrt(delta)))/(2*int(a.text()))))

               else:
                  hes1.setText("("+"-"+"%s"%(int(b.text()))+"+"+"karekok"+"("+"%s"%(delta)+")"+")"+"/"+"%s"%(2*int(a.text()))+" = "+"%s"%(((-1*int(b.text()))+(math.sqrt(delta)))/(2*int(a.text()))))
                  hes2.setText("("+"-"+"%s"%(int(b.text()))+"-"+"karekok"+"("+"%s"%(delta)+")"+")"+"/"+"%s"%(2*int(a.text()))+" = "+"%s"%(((-1*int(b.text()))-(math.sqrt(delta)))/(2*int(a.text()))))

        except ValueError:
              hes1.setText("katsayi gir")
              hes2.setText("katsayi gir")
              durum.setText("katsayi gir")

uygulama = QApplication(sys.argv)
pencere = YeniPencere()
uygulama.exec_()

Sanal kökleri de bulabiliyor, temeli basit diskriminant hesabı zaten :)
Bir arkadaşın isteği üzerine yazmıştım. Maksat paylaşım olsun :)
Sevdiklerine sevdiğini söyle çok geç olmadan.