전개 공식

        (ax + b)(cx + d) = acx^2 + (ad + bc)x + bd

에 기반을 둔 쉬운 곱셉법(일명 카라슈바 곱셈법)이다.

여기서 특히 a == b or a == c or b == d or c == d 인 경우이면

      ad + bc = s(c + d)  or ad + bc = a(b + d)

      or ad + bc = b(s + c) or ad + bc = c(a + b)

로 더 쉬운 곱셈 계산이 가능하다.

 

#!/usr/bin/python
# -*- coding: utf-8 -*-


# Filename: ezMult_02.py
#
# Execute: python ezMult_02.py
#
# See: http://zetcode.com/gui/pyqt4/widgets2/
# See: http://zetcode.com/gui/pyqt4/


import sys
import sys, random
from PyQt4 import QtGui, QtCore

class Example(QtGui.QWidget):
   
    def __init__(self):
        super(Example, self).__init__()
       
        self.ngame = 1
        self.npoint = 0
        self.doneflag = False
       
        self.initUI()
       
    def initUI(self):
        self.rect = QtCore.QRect(0, 30, 300, 220)
        self.xa = random.randint(11, 99)
        self.ya = random.randint(11, 99)
        self.lblXA = QtGui.QLabel(self)
        self.lblYA = QtGui.QLabel(self)
        self.lblYA.setFont(QtGui.QFont('Decorative', 16))
        self.lblXA.setFont(QtGui.QFont('Decorative', 16))
        self.lblXA.setText(str(self.xa))
        self.lblYA.setText(str(self.ya))
        self.lblXA.move(80, 60)
        self.lblYA.move(80, 80)

        self.lblAA = QtGui.QLabel(self)
        self.lblAA.setFont(QtGui.QFont('Decorative', 16))
        self.lblAA.setText("")
        self.lblAA.move(65, 90)

        self.lblBA = QtGui.QLabel(self)
        self.lblBA.setFont(QtGui.QFont('Decorative', 16))
        self.lblBA.setText("")
        self.lblBA.move(105, 110)

        self.lblCA = QtGui.QLabel(self)
        self.lblCA.setFont(QtGui.QFont('Decorative', 16))
        self.lblCA.setText("")
        self.lblCA.move(105, 120)

        self.lblOpa = QtGui.QLabel(self)
        self.lblOpa.setFont(QtGui.QFont('Decorative', 16))
        self.lblOpa.setText("X")
        self.lblOpa.move(40, 80)


        self.x = random.randint(11, 99)
        self.y = random.randint(11, 99)
        self.lblX = QtGui.QLabel(self)
        self.lblY = QtGui.QLabel(self)
        self.lblX.setText(str(self.x))
        self.lblY.setText(str(self.y))
        self.lblX.move(160, 40)
        self.lblY.move(160, 60)
        self.lblOp = QtGui.QLabel(self)
        self.lblOp.setText("X")
        self.lblOp.move(120, 60)
       
       
        self.lbl = QtGui.QLabel(self)
        self.lbl.move(60, 40)
        self.lbl.setHidden(True)

        self.lblScore = QtGui.QLabel(self)
        self.lblScore.setText("Score:")
        self.lblScore.move(10, 10)

        self.lblMsg = QtGui.QLabel(self)
        self.lblMsg.setText("Your first game")
        self.lblMsg.move(10, 255)

        self.lblScore.setFont(QtGui.QFont('Times New Roman', 12))
        self.lblMsg.setFont(QtGui.QFont('Times New Roman', 12))
       
       
        self.qleA = QtGui.QLineEdit(self)
        self.qleA.setFont(QtGui.QFont('Decorative', 20))
        self.qleA.setMaxLength(4)

        self.qleA.move(165, 90)
        self.qleA.resize(80, 30)

        self.qleB = QtGui.QLineEdit(self)
        self.qleB.setFont(QtGui.QFont('Decorative', 20))
        self.qleB.setMaxLength(3)
       
        self.qleB.move(165, 125)
        self.qleB.resize(60, 30)

        self.qleC = QtGui.QLineEdit(self)
        self.qleC.setFont(QtGui.QFont('Decorative', 20))
        self.qleC.setMaxLength(4)
       
        self.qleC.move(165, 170)
        self.qleC.resize(80, 30)

        if self.isSimple(self.xa, self.ya):
            self.redrawSimpleSubTable()
        else:
            self.redrawSubTable()
           
        if self.isSimple(self.x, self.y):
            self.redrawSimpleTable()
        else:
            self.redrawTable()
           
        self.btnExit = QtGui.QPushButton('Exit', self)
        self.btnExit.move(310, 60)
        self.btnExit.clicked.connect(self.doExit)
        self.btnExit.setEnabled(False)    

        self.btnMore = QtGui.QPushButton('More', self)
        self.btnMore.move(310, 120)
        self.btnMore.clicked.connect(self.moreGame)
        self.btnMore.setEnabled(False)  

        self.btnConfirm = QtGui.QPushButton('Ok', self)
        self.btnConfirm.move(310, 180)
        self.btnConfirm.clicked.connect(self.scoreGame)

        self.btnExit.setFont(QtGui.QFont('Arial', 12))
        self.btnMore.setFont(QtGui.QFont('Arial', 12))
        self.btnConfirm.setFont(QtGui.QFont('Arial', 12))


        self.qleA.textChanged[str].connect(self.onChanged)
       
        self.setGeometry(300, 300, 420, 280)
        self.setWindowTitle('Training Tool for Easy Multiplication')
        self.show()


    def moreGame(self):  
        self.update(self.rect)
          
        self.xa = random.randint(11, 99)
        self.ya = random.randint(11, 99)
        self.lblXA.setText(str(self.xa))
        self.lblYA.setText(str(self.ya))
       
        self.x = random.randint(11, 99)
        self.y = random.randint(11, 99)
        self.lblX.setText(str(self.x))
        self.lblY.setText(str(self.y))
       
        self.qleA.setText("")
        self.qleB.setText("")
        self.qleC.setText("")
       
        if self.isSimple(self.xa, self.ya):
            self.redrawSimpleSubTable()
        else:
            self.redrawSubTable()
           
        if self.isSimple(self.x, self.y):
            self.redrawSimpleTable()
        else:
            self.redrawTable()

        self.show()
        self.qleA.setFocus(True)    # See: http://pyqt.sourceforge.net/Docs/PyQt4/qwidget.html
        self.btnMore.setEnabled(False)  
        self.btnConfirm.setEnabled(True) 

        if self.ngame == 1:
            self.lblMsg.setText("Your first game")
        elif self.ngame == 2:
            self.lblMsg.setText("Your second game")
        elif self.ngame == 3:
            self.lblMsg.setText("Your third game")
        elif self.ngame == 4:
            self.lblMsg.setText("Your fourth game")
        elif self.ngame == 5:
            self.lblMsg.setText("Your fFifh game")
        else:
            self.lblMsg.setText("Your %d-th game" % self.ngame)
        self.lblMsg.adjustSize()


    def scoreGame(self):
        c = int(self.qleC.text())
       
        if self.isSimple(self.x, self.y):
            if c == self.x*self.y:
                 self.npoint += 1
                 self.lblMsg.setText("Right!")
                 if self.npoint == 5 and self.npoint == self.ngame:
                     self.lblMsg.setText("Congratulation!!")
                 self.lblMsg.adjustSize()
            else:
                s3 = "%d -> %d" % (c,  self.x*self.y)
                self.lblMsg.setText("Wrong! (%s)" % s3)
                self.lblMsg.adjustSize()
        else:
            a = int(self.qleA.text())
            b = int(self.qleB.text())
            if a == int(self.x / 10)*int(self.y / 10)*100 + (self.x % 10)*(self.y % 10) and \
                         b == int(self.x / 10)*(self.y % 10) + (self.x % 10)*int(self.y / 10) and \
                         c == self.x*self.y:
                 self.npoint += 1
                 self.lblMsg.setText("Right!")
                 if self.npoint == 5 and self.npoint == self.ngame:
                     self.lblMsg.setText("Congratulation!!")
                 self.lblMsg.adjustSize()
            else:
                s1 = ""
                if a != int(self.x / 10)*int(self.y / 10)*100 + (self.x % 10)*(self.y % 10):
                    s1 = "%d -> %d" % (a,  int(self.x / 10)*int(self.y / 10)*100 + (self.x % 10)*(self.y % 10))
                s2 = ""
                if b !=int(self.x / 10)*(self.y % 10) + (self.x % 10)*int(self.y / 10):
                    if len(s1) > 0:
                        s2 = ", "
                    s2 += "%d -> %d" % (b, int(self.x / 10)*(self.y % 10) + (self.x % 10)*int(self.y / 10))
                s3 = ""
                if c != self.x*self.y:
                    if len(s2) > 0:
                        s3 = ", "
                    s3 += "%d -> %d" % (c,  self.x*self.y)
                self.lblMsg.setText("Wrong! (%s%s%s)" % (s1, s2, s3))
                self.lblMsg.adjustSize()
       
        self.lblScore.setText("Your current score is %d in %d tries," % (20*self.npoint, self.ngame))
        self.lblScore.adjustSize()

        self.ngame += 1

        if self.ngame <= 5:
            self.btnMore.setEnabled(True)   
            self.btnMore.setFocus(True)    
        else:
            self.btnExit.setEnabled(True)  
            self.btnExit.setFocus(True)  
            self.btnMore.setEnabled(False)    
        self.btnConfirm.setEnabled(False)   


    def isSimple(self, x, y):
        if int(x/10) == int(y/10) or x % 10 == y % 10    \
                    or int(x/10) == x % 10 or int(y/10) == y % 10:
            return True
        else:
            return False


    def redrawSimpleSubTable(self):
        self.lblAA.setHidden(True)
        self.lblBA.setHidden(True)
        ca = int(self.xa) * int(self.ya)
        if len(str(ca)) == 3:
            self.lblCA.setText(str(ca))
            self.lblCA.move(72, 109)
            self.lblCA.adjustSize()
        else:
            self.lblCA.setText(str(ca))
            self.lblCA.move(60, 109)
            self.lblCA.adjustSize()


    def redrawSimpleTable(self):
        self.qleA.setHidden(True)
        self.qleB.setHidden(True)
        c = int(self.x) * int(self.y)
        if len(str(c)) == 3:
            self.qleC.setMaxLength(3)
            self.qleC.move(213, 110)
            self.qleC.resize(60, 30)
        else:
            self.qleC.setMaxLength(4)
            self.qleC.move(195, 110)
            self.qleC.resize(80, 30)


    def redrawSubTable(self):
        self.lblAA.setHidden(False)
        self.lblBA.setHidden(False)
        aa = int(self.xa/10) * int(self.ya / 10) * 100 + (self.xa % 10) * (self.ya % 10)
        if len(str(aa)) == 3:
            self.lblAA.setText(str(aa))
            self.lblAA.move(72, 109)
            self.lblAA.adjustSize()
        else:
            self.lblAA.setText(str(aa))
            self.lblAA.move(60, 109)
            self.lblAA.adjustSize()

        ba = int(self.xa % 10) * int(self.ya / 10) +  int(self.xa / 10) * int(self.ya %10)
        if len(str(ba)) == 1:
             self.lblBA.setText(str(ba))
             self.lblBA.move(84, 133)
             self.lblBA.adjustSize()
        elif len(str(ba)) == 2:
             self.lblBA.setText(str(ba))
             self.lblBA.move(72, 133)
             self.lblBA.adjustSize()
        else:
             self.lblBA.setText(str(ba))
             self.lblBA.move(60, 133)
             self.lblBA.adjustSize()

        ca = int(self.xa) * int(self.ya)
        if len(str(ca)) == 3:
             self.lblCA.setText(str(ca))
             self.lblCA.move(72, 164)
             self.lblCA.adjustSize()
        else:
             self.lblCA.setText(str(ca))
             self.lblCA.move(60, 164)
             self.lblCA.adjustSize()


    def redrawTable(self):
        self.qleA.setHidden(False)
        self.qleB.setHidden(False)
        a = int(self.x/10) * int(self.y / 10)
        if len(str(a)) == 1:
            self.qleA.setMaxLength(3)
            self.qleA.move(213, 110)
            self.qleA.resize(60, 30)
        else:
            self.qleA.setMaxLength(4)
            self.qleA.move(195, 110)
            self.qleA.resize(80, 30)

        b = int(self.x % 10) * int(self.y / 10) +  int(self.x / 10) * int(self.y %10)
        if len(str(b)) == 1:
             self.qleB.setMaxLength(1)
             self.qleB.move(235, 145)
             self.qleB.resize(20, 30)
        elif len(str(b)) == 2:
             self.qleB.setMaxLength(2)
             self.qleB.move(213, 145)
             self.qleB.resize(40, 30)
        else:
             self.qleB.setMaxLength(3)
             self.qleB.move(195, 145)
             self.qleB.resize(60, 30)

        c = int(self.x) * int(self.y)
        if len(str(c)) == 3:
            self.qleC.setMaxLength(3)
            self.qleC.move(213, 190)
            self.qleC.resize(60, 30)
        else:
            self.qleC.setMaxLength(4)
            self.qleC.move(195, 190)
            self.qleC.resize(80, 30)


    def paintEvent(self, e):
        qp = QtGui.QPainter()
        qp.begin(self)
        self.drawLines(qp)
        qp.end()
       
    def drawLines(self, qp):
        penA = QtGui.QPen(QtCore.Qt.black, 1, QtCore.Qt.SolidLine)
        qp.setPen(penA)
        if self.isSimple(self.xa, self.ya):
            qp.drawLine(37, 103, 107, 103)
        else:
            qp.drawLine(37, 103, 107, 103)
            qp.drawLine(37, 156, 107, 156)
       
        pen = QtGui.QPen(QtCore.Qt.black, 2, QtCore.Qt.SolidLine)
        qp.setPen(pen)
        if int(self.x/10) == int(self.y/10) or self.x % 10 == self.y % 10    \
                    or int(self.x/10) == self.x % 10 or int(self.y/10) == self.y % 10:
            qp.drawLine(170, 100, 270, 100)
        else:
            qp.drawLine(170, 100, 270, 100)
            qp.drawLine(170, 185, 270, 185)

        qp.setFont(QtGui.QFont('Decorative', 20))
        self.lblX.setFont(QtGui.QFont('Decorative', 20))
        self.lblY.setFont(QtGui.QFont('Decorative', 20))
        self.lblX.setText(str(self.x))
        self.lblY.setText(str(self.y))
        self.lblX.move(230, 40)
        self.lblX.adjustSize()
        self.lblY.move(230, 70)
        self.lblY.adjustSize()
        self.lblOp.setFont(QtGui.QFont('Decorative', 20))
        self.lblOp.move(170, 70)
        self.lblOp.adjustSize()

 

    def onChanged(self, text):

        self.lbl.setText(text)
        self.lbl.adjustSize()       
       
    def doExit(self):
        sys.exit(0)


def main():
        app = QtGui.QApplication(sys.argv)
    ex = Example()
    sys.exit(app.exec_())


if __name__ == '__main__':
    main()

 

실행 중인 창:

 

 

 

Posted by Scripter

댓글을 달아 주세요