PyQT实战-实现随机生成多项式并计算

老师布置作业,让给家里的初中小朋友出多项式,并批改,懒得算,敲个代码做个UI让自己算吧。。。
刚学的QT派上用场了,主要就两个文件,一个是界面,一个是算法,合起来就OK了!
界面的py文件:

# -*- coding: utf-8 -*-

# Form implementation generated from reading ui file 'Duoxs.ui'
#
# Created by: PyQt5 UI code generator 5.15.1
#
# WARNING: Any manual changes made to this file will be lost when pyuic5 is
# run again.  Do not edit this file unless you know what you are doing.


from PyQt5 import QtCore, QtGui, QtWidgets


class Ui_MainWindow(object):
    def setupUi(self, MainWindow):
        MainWindow.setObjectName("MainWindow")
        MainWindow.resize(800, 600)
        self.centralwidget = QtWidgets.QWidget(MainWindow)
        self.centralwidget.setObjectName("centralwidget")
        self.label_Polynomial = QtWidgets.QLabel(self.centralwidget)
        self.label_Polynomial.setGeometry(QtCore.QRect(110, 90, 36, 16))
        self.label_Polynomial.setObjectName("label_Polynomial")
        self.lineEdit_Polynomial = QtWidgets.QLineEdit(self.centralwidget)
        self.lineEdit_Polynomial.setGeometry(QtCore.QRect(190, 90, 431, 20))
        self.lineEdit_Polynomial.setObjectName("lineEdit_Polynomial")
        self.lineEdit_Compute_Result = QtWidgets.QLineEdit(self.centralwidget)
        self.lineEdit_Compute_Result.setGeometry(QtCore.QRect(190, 170, 133, 20))
        self.lineEdit_Compute_Result.setObjectName("lineEdit_Compute_Result")
        self.label_Compute_Result = QtWidgets.QLabel(self.centralwidget)
        self.label_Compute_Result.setGeometry(QtCore.QRect(90, 180, 48, 16))
        self.label_Compute_Result.setObjectName("label_Compute_Result")
        self.lineEdit_question_number = QtWidgets.QLineEdit(self.centralwidget)
        self.lineEdit_question_number.setGeometry(QtCore.QRect(200, 50, 133, 20))
        self.lineEdit_question_number.setObjectName("lineEdit_question_number")
        self.label_question_number = QtWidgets.QLabel(self.centralwidget)
        self.label_question_number.setGeometry(QtCore.QRect(101, 52, 48, 16))
        self.label_question_number.setObjectName("label_question_number")
        self.lineEdit_Correct_Result = QtWidgets.QLineEdit(self.centralwidget)
        self.lineEdit_Correct_Result.setGeometry(QtCore.QRect(190, 220, 133, 20))
        self.lineEdit_Correct_Result.setObjectName("lineEdit_Correct_Result")
        self.lineEdit_Correct_Number = QtWidgets.QLineEdit(self.centralwidget)
        self.lineEdit_Correct_Number.setGeometry(QtCore.QRect(221, 355, 133, 20))
        self.lineEdit_Correct_Number.setObjectName("lineEdit_Correct_Number")
        self.label_Correct_Number = QtWidgets.QLabel(self.centralwidget)
        self.label_Correct_Number.setGeometry(QtCore.QRect(110, 360, 72, 16))
        self.label_Correct_Number.setObjectName("label_Correct_Number")
        self.lineEdit_Help = QtWidgets.QLineEdit(self.centralwidget)
        self.lineEdit_Help.setGeometry(QtCore.QRect(180, 270, 441, 20))
        self.lineEdit_Help.setObjectName("lineEdit_Help")
        self.pushButton_Begin = QtWidgets.QPushButton(self.centralwidget)
        self.pushButton_Begin.setGeometry(QtCore.QRect(410, 40, 75, 23))
        self.pushButton_Begin.setObjectName("pushButton_Begin")
        self.lineEdit_Error_Number = QtWidgets.QLineEdit(self.centralwidget)
        self.lineEdit_Error_Number.setGeometry(QtCore.QRect(510, 360, 133, 20))
        self.lineEdit_Error_Number.setObjectName("lineEdit_Error_Number")
        self.label_Error_Number = QtWidgets.QLabel(self.centralwidget)
        self.label_Error_Number.setGeometry(QtCore.QRect(420, 360, 72, 16))
        self.label_Error_Number.setObjectName("label_Error_Number")
        self.lineEdit_Accuracy = QtWidgets.QLineEdit(self.centralwidget)
        self.lineEdit_Accuracy.setGeometry(QtCore.QRect(330, 420, 113, 20))
        self.lineEdit_Accuracy.setObjectName("lineEdit_Accuracy")
        self.label_Accuracy = QtWidgets.QLabel(self.centralwidget)
        self.label_Accuracy.setGeometry(QtCore.QRect(250, 420, 54, 12))
        self.label_Accuracy.setObjectName("label_Accuracy")
        self.pushButton_Confirm = QtWidgets.QPushButton(self.centralwidget)
        self.pushButton_Confirm.setGeometry(QtCore.QRect(420, 170, 75, 23))
        self.pushButton_Confirm.setObjectName("pushButton_Confirm")
        self.pushButton_Correct_Result = QtWidgets.QPushButton(self.centralwidget)
        self.pushButton_Correct_Result.setGeometry(QtCore.QRect(39, 220, 121, 23))
        self.pushButton_Correct_Result.setObjectName("pushButton_Correct_Result")
        self.pushButton_Help = QtWidgets.QPushButton(self.centralwidget)
        self.pushButton_Help.setGeometry(QtCore.QRect(50, 270, 75, 23))
        self.pushButton_Help.setObjectName("pushButton_Help")
        self.label_variable = QtWidgets.QLabel(self.centralwidget)
        self.label_variable.setGeometry(QtCore.QRect(110, 140, 24, 16))
        self.label_variable.setObjectName("label_variable")
        self.lineEdit_variable = QtWidgets.QLineEdit(self.centralwidget)
        self.lineEdit_variable.setGeometry(QtCore.QRect(190, 130, 431, 20))
        self.lineEdit_variable.setObjectName("lineEdit_variable")
        MainWindow.setCentralWidget(self.centralwidget)
        self.menubar = QtWidgets.QMenuBar(MainWindow)
        self.menubar.setGeometry(QtCore.QRect(0, 0, 800, 23))
        self.menubar.setObjectName("menubar")
        MainWindow.setMenuBar(self.menubar)
        self.statusbar = QtWidgets.QStatusBar(MainWindow)
        self.statusbar.setObjectName("statusbar")
        MainWindow.setStatusBar(self.statusbar)

        self.retranslateUi(MainWindow)
        QtCore.QMetaObject.connectSlotsByName(MainWindow)

    def retranslateUi(self, MainWindow):
        _translate = QtCore.QCoreApplication.translate
        MainWindow.setWindowTitle(_translate("MainWindow", "MainWindow"))
        self.label_Polynomial.setText(_translate("MainWindow", "多项式"))
        self.label_Compute_Result.setText(_translate("MainWindow", "计算结果"))
        self.label_question_number.setText(_translate("MainWindow", "题目数量"))
        self.label_Correct_Number.setText(_translate("MainWindow", "正确题目数量"))
        self.pushButton_Begin.setText(_translate("MainWindow", "开始"))
        self.label_Error_Number.setText(_translate("MainWindow", "错误题目数量"))
        self.label_Accuracy.setText(_translate("MainWindow", "准确率"))
        self.pushButton_Confirm.setText(_translate("MainWindow", "确定"))
        self.pushButton_Correct_Result.setText(_translate("MainWindow", "显示正确答案"))
        self.pushButton_Help.setText(_translate("MainWindow", "求助"))
        self.label_variable.setText(_translate("MainWindow", "变量"))

这里的布局我是用QT designer做的,然后使用:
pyuic5 -o xx.py xx.ui将UI文件转换为py文件。
算法的py文件,代码比较简单,我就不解释了,直接上代码:

import random
import sys
from duoxs import  Ui_MainWindow
from PyQt5.QtWidgets import QMainWindow,QApplication

class Duoxiangshi(QMainWindow,Ui_MainWindow):
    # 初始化函数
    def __init__(self):
        super(Duoxiangshi, self).__init__()
        self.setupUi(self)
        self.pushButton_Begin.clicked.connect(lambda:self.Translate())
        self.pushButton_Correct_Result.clicked.connect(lambda:self.showcorrectresult())
        self.pushButton_Help.clicked.connect(lambda:self.showhelp())
        self.pushButton_Confirm.clicked.connect(lambda:self.Compute())

    类变量
    correctnu = 0
    errornu = 0
#生成多项式
    def generate(self):
        xy = ['x', 'y', '']
        abc = ['a', 'b', 'c', 'd', '', '']
        ch = ['+', '-']
        qian = ['(', '']
        flag = random.choice([0, 1])
        if flag == 0:
            # xy多项式,一共6个因式
            yin1 = str(random.randint(2, 9)) + random.choice(xy) + random.choice(xy) + random.choice(xy)
            yin2 = str(random.randint(2, 9)) + random.choice(xy) + random.choice(xy) + random.choice(xy)
            yin3 = str(random.randint(2, 9)) + random.choice(xy) + random.choice(xy) + random.choice(xy)
            yin4 = str(random.randint(2, 9)) + random.choice(xy) + random.choice(xy) + random.choice(xy)
            yin5 = str(random.randint(2, 9)) + random.choice(xy) + random.choice(xy) + random.choice(xy)
            yin6 = str(random.randint(2, 9)) + random.choice(xy) + random.choice(xy) + random.choice(xy)
            qiank = random.choice(qian)
            houk = '' if qiank == '' else ')'
            qiank2 = random.choice(qian)
            houk2 = '' if qiank2 == '' else ')'
            qiank3 = random.choice(qian)
            houk3 = '' if qiank3 == '' else ')'
            #因式合并
            res = qiank + yin1 + random.choice(ch) + yin2 + houk + random.choice(ch) + qiank2 + yin3 + random.choice(
                ch) + yin4 + houk2 + random.choice(ch) + qiank3 + yin5 + random.choice(ch) + yin6 + houk3

        else:
            #abcd多项式,一共4个因式
            yin1 = str(random.randint(2, 9)) + random.choice(abc) + random.choice(abc) + random.choice(abc)
            yin2 = str(random.randint(2, 9)) + random.choice(abc) + random.choice(abc) + random.choice(abc)
            yin3 = str(random.randint(2, 9)) + random.choice(abc) + random.choice(abc) + random.choice(abc)
            yin4 = str(random.randint(2, 9)) + random.choice(abc) + random.choice(abc) + random.choice(abc)
            qiank = random.choice(qian)
            houk = '' if qiank == '' else ')'
            qiank2 = random.choice(qian)
            houk2 = '' if qiank2 == '' else ')'
            # 因式合并
            res = qiank + yin1 + random.choice(ch) + yin2 + houk + random.choice(ch) + qiank2 + yin3 + random.choice(
                ch) + yin4 + houk2
        return res, flag

    suanshi=''
    zuizhongshi=''
    result = 0
    # 将字符串加上乘号,并对未知数赋值
    def Translate(self):
        res, flag = self.generate()
        ss = list(res)
        le = len(ss)-1
        s2 = []
        shuzi = []
        for i in range(1, 10):
            shuzi.append(str(i))
        zimu = ['a', 'b', 'c', 'd', 'x', 'y']
        #将乘法的符号加进去
        for i in range(le):
            if (ss[i] in shuzi or ss[i] in zimu) and (ss[i + 1] in shuzi or ss[i + 1] in zimu):
                s2.append(ss[i])
                s2.append('*')
            else:
                s2.append(ss[i])
        s2.append(ss[-1])  # 将字符串的最后一个元素添加上去(未遍历到)
        #将加上乘法符号的字符串组合
        for x in s2:
            Duoxiangshi.suanshi += x

        self.lineEdit_Polynomial.setText(Duoxiangshi.suanshi)
        #根据flag判断多项式类型并进行赋值
        if flag == 0:
            xx = str(random.randint(1, 10))
            yy = str(random.randint(1, 20))
            print("多项式为:",Duoxiangshi.suanshi, "------","进行赋值","x=",xx,"y=",yy)
            t=("x={}  y={}").format(xx,yy)
            self.lineEdit_variable.setText(t)
            zuizhong=Duoxiangshi.suanshi.replace('x',xx)
            Duoxiangshi.zuizhongshi=zuizhong.replace('y',yy)

        else:
            aa = str(random.randint(1, 10))
            bb = str(random.randint(1, 20))
            cc = str(random.randint(1, 10))
            dd = str(random.randint(1, 20))
            print("多项式为:",Duoxiangshi.suanshi, "----", "进行赋值","a=", aa, "b=", bb, "c=", cc, "d=", dd)
            t = ("a= {}   b={}  c={}  d={}").format(aa,bb,cc,dd)
            self.lineEdit_variable.setText(t)
            zuizhong1=Duoxiangshi.suanshi.replace('a',aa)
            zuizhong2=zuizhong1.replace('b',bb)
            zuizhong3=zuizhong2.replace('c',cc)
            Duoxiangshi.zuizhongshi=zuizhong3.replace('d',dd)
        Duoxiangshi.result = str(eval(Duoxiangshi.zuizhongshi))


    #准确率计算
    def accu(self):
        print("*"*100)
        accu="{:.2f}".format(Duoxiangshi.correctnu/(Duoxiangshi.correctnu+Duoxiangshi.errornu))
        self.lineEdit_Accuracy.setText(str(accu))


    def Compute(self):
        mycompute=self.lineEdit_Compute_Result.text()
        print("计算", Duoxiangshi.zuizhongshi, " = ", Duoxiangshi.result)
        if Duoxiangshi.result==mycompute:
            Duoxiangshi.correctnu += 1
            print("计算正确",Duoxiangshi.correctnu)
            self.lineEdit_Correct_Number.setText(str(Duoxiangshi.correctnu))
        else:
            Duoxiangshi.errornu += 1
            self.lineEdit_Error_Number.setText(str(Duoxiangshi.errornu))
            print("计算错误",Duoxiangshi.errornu)

        # 当答题数量等于预设的题目数量时,显示完成
        # 但是不知道为什么再次点击确定会退出**************************
        timunu=int(self.lineEdit_question_number.text())
        if Duoxiangshi.correctnu+Duoxiangshi.errornu==timunu:
            self.lineEdit_question_number.setText("Completed!")

        # 计算完成之后重新设置变量
        Duoxiangshi.suanshi = ''
        Duoxiangshi.zuizhongshi = ''
        self.accu()
        self.Translate()
        self.lineEdit_Compute_Result.setText('')
        self.lineEdit_Correct_Result.setText('')
        self.lineEdit_Help.setText('')

    #显示正确答案
    def showcorrectresult(self):
        self.lineEdit_Correct_Result.setText(str(Duoxiangshi.result))
    # 显示计算的式子
    def showhelp(self):
        self.lineEdit_Help.setText(Duoxiangshi.zuizhongshi)

if __name__ == '__main__':
    app = QApplication(sys.argv)
    w = Duoxiangshi()
    w.setWindowTitle("多项式计算")
    w.show()
    sys.exit(app.exec_())

最后的UI打开后:
在这里插入图片描述
界面很丑,仅供参考!
完整的代码在gitee仓库:https://gitee.com/ximulalala/Duoxiangshi.git
欢迎交流讨论!

  • 6
    点赞
  • 7
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值