软件工程(2019)结对编程第二次作业

1.组员

  1. 吕浩杰 163401010521
  2. 刁子其 163401010520

2.Github

3.各部分功能截图

(1)界面设计

  • 单选按钮:整数运算,分数运算,上次记录
  • 点击按钮:退格,清除,开始,下一题,提交
  • 显示框:出题框,正确答案显示框,正确率显示框,题目数量显示框,答对题目数量显示框
  • 输入框:答案输入框
    1078908-20170508203934254-1820540593.png
  • 代码实现
import sys
import os

import math

from PyQt5 import QtWidgets, QtGui, QtCore

class Button(QtWidgets.QToolButton):
    def __init__(self, text, parent=None):
        super(Button, self).__init__(parent)

        self.setSizePolicy(QtWidgets.QSizePolicy.Expanding, QtWidgets.QSizePolicy.Preferred)
        self.setText(text)

    def sizeHint(self):
        size = super(Button, self).sizeHint()
        size.setHeight(size.height() + 20)
        size.setWidth(max(size.width(), size.height()))
        return size


class Calculator(QtWidgets.QWidget):

    def __init__(self, parent=None):
        super(Calculator, self).__init__(parent)


        self.predStr = ""
        self.succStr = ""
        self.questionSum = 0
        self.correctCount = 0
        self.correctPercent = 0
        self.fraction_list = []
        self.errorQuestionList = []
        self.lastQuestionList = []
        self.index = 3




        global lcd
        lcd = QtWidgets.QTextBrowser()
        lcd.setFixedHeight(60)
        lcd.setFont(QtGui.QFont("Microsoft YaHei", 20))
        lcd.setText('0'.decode('utf-8'))

        global lcd_correct_answer
        lcd_correct_answer = QtWidgets.QTextBrowser()
        lcd_correct_answer.setFixedHeight(40)
        lcd_correct_answer.setFont(QtGui.QFont("Microsoft YaHei", 13))
        lcd_correct_answer.setText("".decode('utf-8'))

        global lcd_correct_percent
        lcd_correct_percent = QtWidgets.QTextBrowser()
        lcd_correct_percent.setFixedHeight(40)
        lcd_correct_percent.setFont(QtGui.QFont("Microsoft YaHei", 13))
        lcd_correct_percent.setText("".decode('utf-8'))

        global lcd_question_sum
        lcd_question_sum = QtWidgets.QTextBrowser()
        lcd_question_sum.setFixedHeight(40)
        lcd_question_sum.setFont(QtGui.QFont("Microsoft YaHei", 13))
        lcd_question_sum.setText("".decode('utf-8'))

        global lcd_correct_count
        lcd_correct_count = QtWidgets.QTextBrowser()
        lcd_correct_count.setFixedHeight(40)
        lcd_correct_count.setFont(QtGui.QFont("Microsoft YaHei", 13))
        lcd_correct_count.setText('0'.decode('utf-8'))

        self.intButton = QtWidgets.QRadioButton(u"整数运算")
        self.fractionButton = QtWidgets.QRadioButton(u"分数数运算")
        self.lastErrorQuestionButton = QtWidgets.QRadioButton(u"上次记录")

        self.backspaceButton = self.createButton(u"退格", self.backspaceClicked)
        self.clearAllButton = self.createButton(u"清除", self.clearAll)

        self.start = self.createButton(u"开始", self.start)
        self.next_question = self.createButton(u"下一题", self.next_question)

        self.label1 = QtWidgets.QLabel(u'请输入答案')
        self.label2 = QtWidgets.QLabel(u'正确答案')
        self.label3 = QtWidgets.QLabel(u'正确率')
        self.label4 = QtWidgets.QLabel(u'当前题目数量')
        self.label5 = QtWidgets.QLabel(u'答对数目')
        self.submitButton = self.createButton(u"提交", self.submit_answer)
        self.inputLabel = QtWidgets.QLineEdit(u'')
        font = self.inputLabel.font()
        font.setPointSize(font.pointSize() + 8)
        self.inputLabel.setFixedHeight(40)
        self.inputLabel.setFont(font)



        mainLayout = QtWidgets.QGridLayout()
        mainLayout.setSizeConstraint(QtWidgets.QLayout.SetFixedSize)

        mainLayout.addWidget(lcd, 0, 0, 1, 8)
        mainLayout.addWidget(self.intButton, 1, 0, 1, 2)
        mainLayout.addWidget(self.fractionButton, 1, 3, 1, 2)
        mainLayout.addWidget(self.lastErrorQuestionButton, 1, 6, 1, 2)

        mainLayout.addWidget(self.backspaceButton, 2, 0, 1, 4)
        mainLayout.addWidget(self.clearAllButton, 2, 4, 1, 4)

        mainLayout.addWidget(self.start, 3, 0, 1, 4)
        mainLayout.addWidget(self.next_question, 3, 4, 1, 4)

        mainLayout.addWidget(self.label1, 4, 0, 1, 2)
        mainLayout.addWidget(self.inputLabel, 4, 2, 1, 2)
        mainLayout.addWidget(self.submitButton, 4, 4, 1, 4)

        mainLayout.addWidget(self.label2, 5, 0, 1, 2)
        mainLayout.addWidget(lcd_correct_answer, 5, 2, 1, 2)
        mainLayout.addWidget(self.label3, 5, 4, 1, 2)
        mainLayout.addWidget(lcd_correct_percent, 5, 6, 1, 2)

        mainLayout.addWidget(self.label4, 6, 0, 1, 2)
        mainLayout.addWidget(lcd_question_sum, 6, 2, 1, 2)
        mainLayout.addWidget(self.label5, 6, 4, 1, 2)
        mainLayout.addWidget(lcd_correct_count,6, 6, 1, 2)

        self.setLayout(mainLayout)
        self.setWindowTitle(u"随机出题计算器   (注:保留小数点后1位)")
        
            def closeEvent(self, event):
        reply = QtWidgets.QMessageBox.question(self, '确认退出', '你确定要退出么?',
                                               QtWidgets.QMessageBox.Yes,
                                               QtWidgets.QMessageBox.No
                                               )
        if reply == QtWidgets.QMessageBox.Yes:
            self.errorQuestionList.insert(0, str(self.correctPercent))
            self.errorQuestionList.insert(1, str(self.questionSum))
            self.errorQuestionList.insert(2, str(self.correctCount))
            Question_write.question_write(self.errorQuestionList)
            event.accept()
        else:
            event.ignore()

    def createButton(self, text, member):
        button = Button(text)
        button.clicked.connect(member) 
        return button

(2)整数出题:

  • 首先点击“整数运算”单选按钮,随后点击“开始”按钮,然后屏幕上会随机出现整数运算的题目,可以在答案输入框输入答案,点击“提交”按钮,会显示出题目的正确答案,以及正确率和答对题目数量。
    1108157-20170509192219176-228228177.png
    1108157-20170509192227535-83641306.png
  • 整数实现代码
# _*_ coding: utf-8 _*_
__author__ = 'XiaoDong-Hu and Huanli-Xu'
__date__ = '2017/4/30 16:01'


pri = {'+': 1, '-': 1,
      '*': 2, '/': 2,
      '\0': 0
      }

class Stack:
    def __init__(self):
        self.items = []

    def isEmpty(self):
        return len(self.items)==0

    def push(self, item):
        self.items.append(item)

    def pop(self):
        return self.items.pop()

    def top(self):
        if not self.isEmpty():
            return self.items[len(self.items)-1]

    def size(self):
        return len(self.items)

    def printf(self):
        for i in self.items:
            print i

def readNumber(str, currentIndex):
    int_num = 0
    decimal_num = 0.0
    intCount = 0
    decimalCount = 0
    currentIndex1 = 0
    for i in range(currentIndex, len(str)):
        if str[i] < '0' or str[i] > '9':
            currentIndex1 = i
            break
        intCount = intCount + 1
    for i in range(currentIndex, currentIndex + intCount):
        index = currentIndex + intCount - i - 1
        times = 10**(index)
        digit = int(str[i]) * times
        int_num += digit

    if str[currentIndex1] == '.':
        for i in range(currentIndex1 + 1, len(str)):
            if str[i] < '0' and str[i] > '9':
                break
            decimalCount = decimalCount + 1
        for i in range(currentIndex + 1, currentIndex + 1 + decimalCount):
            digit = int(str[i]) * 1/(10^(currentIndex + decimalCount - i))
            decimal_num += digit
        return int_num + decimal_num
    else:
        return int_num

def Calculating(infixStr):
    opnd = Stack()
    optr = Stack()

    optr.push('\0')
    infixStr = infixStr + '\0'
    i = 0
    while not optr.isEmpty():
        if infixStr[i] >= '0' and infixStr[i] <= '9':
            number = readNumber(infixStr, i)
            opnd.push(number)
            i = i + len(str(number)) -1
        else:
            if pri[infixStr[i]] > pri[optr.top() ]:
                optr.push(infixStr[i])
            elif pri[infixStr[i]] <= pri[optr.top()] and optr.top() != '\0':
                opr2 = float(opnd.top())
                opnd.pop()
                opr1 = opnd.top()
                opnd.pop()
                if optr.top() == '+':
                    op = opr1 + opr2
                    opnd.push(op)
                    opnd.printf()
                if optr.top() == '-':
                    op = opr1 - opr2
                    opnd.push(op)
                if optr.top() == '*':
                    op = opr1 * opr2
                    opnd.push(op)
                if optr.top() == '/':
                    op = opr1 / opr2
                    opnd.push(op)
                optr.pop()
                i = i - 1
            else:
                optr.pop()
        i = i + 1

    print opnd.top()
    return opnd.top()
  • 随机出题实现代码
# _*_ coding: utf-8 _*_
__author__ = 'XiaoDong-Hu and Huanli-Xu'
__date__ = '2017/5/6 20:26'


import random
from fractions import Fraction


def Random_int_question():
    oprator_list = ['', '+', '-', '*', '/']
    num_size = random.randint(3, 4)
    opr_size = num_size - 1
    question_str = ""
    i = 0
    while i < num_size:
        question_str += str(random.randint(1, 15)) + str(oprator_list[random.randint(1, 4)])
        i = i + 1
    question_str = question_str[0: len(question_str) - 1]
    return question_str

(3)分数运算:

  • 首先点击“分数运算”单选按钮,其余部分同整数运算。
    1108157-20170509192241379-1353719320.png

  • 随机出题并计算实现代码

def Random_fraction_question():
    oprator_list = ['', '+', '-', '*', '/']
    num_size = 2
    question_str = ""
    question_list = []
    i = 0
    opr_num = []
    oprator = []
    while i < num_size:
        f = Fraction(random.randint(1, 15), random.randint(1, 15))
        opr_num.append(f)
        oprator.append(oprator_list[random.randint(1, 4)])
        if f.numerator >= f.denominator:
            int_num = f.numerator / f.denominator
            numerator = f.numerator % f.denominator
            question_str += str(int_num) + "\'" + str(numerator) + '/' + str(f.denominator) + " " + str(oprator[i]) + " "
        else:
            question_str  += str(f) + " " +str(oprator[i]) + " "
        i = i + 1
    question_str = question_str[0: len(question_str) - 2] 
    if oprator[0] == '+':
        answer = opr_num[0] + opr_num[1]
    elif oprator[0] == '-':
        answer = opr_num[0] - opr_num[1]
    elif oprator[0] == '*':
        answer = opr_num[0] * opr_num[1]
    else:
        answer = opr_num[0] / opr_num[1]
    print answer
    if answer.numerator >= answer.denominator:
        answer_ = str(answer.numerator/answer.denominator) + "\'" + str(answer.numerator%answer.denominator) + "/" + str(answer.denominator)
        question_list.append(question_str)
        question_list.append(answer_)

    question_list.append(question_str)
    question_list.append(answer)
    return question_list

(4)退格功能

  • 点击“退格”按钮以后,会清除表达式的最后一个字符。
  • 退格前:
    1078908-20170515133637978-232269761.png
  • 退格后:
    1078908-20170515133643510-401157572.png
  • 退格代码实现
def backspaceClicked(self):
    self.predStr = self.predStr[0:len(self.predStr) - 1]
    if len(self.predStr) == 0:
        lcd.setText('0'.decode('utf-8'))
    lcd.setText(self.predStr)

(5)清屏

  • 在点击“清除”按钮以后,屏幕上会置0,清除掉原有表达式。
  • 清除前:
    1078908-20170515132112947-710448979.png
  • 清除后:
    1078908-20170515132133260-14383424.png

  • 退格代码实现
def clearAll(self):
    self.predStr = ""
    self.succStr = ""
    lcd.setText('0'.decode('utf-8'))

(6)重启以后保持上次记录

  • 重启后会记忆上次答题数目以及正确率和答对题的数目,随后点击“上次记录”单选按钮,可以重做上次做错的题目。
    1108157-20170509192258441-1737635860.png
    1108157-20170509192311582-572146248.png

  • 保持上次记录的代码实现

# _*_ coding: utf-8 _*_
__author__ = 'XiaoDong-Hu and Huanli-Xu'
__date__ = '2017/5/7 15:29'

import os


def question_write(errorList):
    with open('store.txt', 'w') as f:
        for item in errorList:
            item = item + '\n'
            f.write(item)
    f.close()


# _*_ coding: utf-8 _*_
__author__ = 'XiaoDong-Hu and Huanli-Xu'
__date__ = '2017/5/7 15:29'


import os


def question_read():
    lastQuestionList = []
    with open('store.txt', 'r') as f:
       for item in f.readlines():
           item = item[0:len(item) - 1]
           lastQuestionList.append(item)
    f.close()
    return lastQuestionList

4.心得体会

  • 认真学会一门计算机语言并且精通与它,是很重要的!我在编写程序的过程中,如何生成带括号的四则运算表达式,想了半天没想起来。在网上搜索一下,发现自己没有完全看懂,一些算法不会,自己尝试编写,但是没有成功,最后不得已放弃了。在写生成小数表达式的函数时,也是遇到了各种错误,不过好在最终解决了。但是自己的代码重复的地方太多,没有想到好的办法解决,只能写成这个样子,还有这个代码有许多漏洞,比如说能生成重复的表达式,一些表达式结果不是很正确等等,要改进的地方还有很多。希望自己能多多写写代码,增强自己的代码能力,提高自己的算法思想
  • 通过这次实验,我更深刻的理解了团队合作的重要性,这次实验对于我个人来说是很有难度的,在做这次实验的过程中,遇到了很多方面的问题,也发现了自己很多的不足之处,而这种团队合作正好给我们提供了有利的条件,我们可以借助队友的帮忙,从而更快的完成实验。这次实验中,我发现了自己在专业知识方面很薄弱,希望以后在团队合作中能学到更多知识!

转载于:https://www.cnblogs.com/haojie0616/p/10810171.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值