软件工程第二次作业——结对编程

1.组员:

  • 胡效东 1500802103
  • 许欢莉 1500802121 博客链接

2.代码链接(coding链接):

  • 随机出题界面软件

3.各部分功能截图

(1)界面设计:

  • 单选按钮:整数运算,分数运算,上次记录
  • 点击按钮:退格,清除,开始,下一题,提交
  • 显示框:出题框,正确答案显示框,正确率显示框,题目数量显示框,答对题目数量显示框
  • 输入框:答案输入框
    软件工程第二次作业——结对编程_第1张图片
  • 代码实现
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)整数出题:

  • 首先点击“整数运算”单选按钮,随后点击“开始”按钮,然后屏幕上会随机出现整数运算的题目,可以在答案输入框输入答案,点击“提交”按钮,会显示出题目的正确答案,以及正确率和答对题目数量。
    软件工程第二次作业——结对编程_第2张图片
    软件工程第二次作业——结对编程_第3张图片

  • 整数实现代码


# _*_ 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)分数运算:

  • 首先点击“分数运算”单选按钮,其余部分同整数运算。
    软件工程第二次作业——结对编程_第4张图片

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


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)退格功能

  • 点击“退格”按钮以后,会清除表达式的最后一个字符。
  • 退格前:
    软件工程第二次作业——结对编程_第5张图片

  • 退格后:
    软件工程第二次作业——结对编程_第6张图片

  • 退格代码实现

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,清除掉原有表达式。
  • 清除前:
    软件工程第二次作业——结对编程_第7张图片

  • 清除后:
    软件工程第二次作业——结对编程_第8张图片

  • 退格代码实现

def clearAll(self):
    self.predStr = ""
    self.succStr = ""
    lcd.setText('0'.decode('utf-8'))

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

  • 重启后会记忆上次答题数目以及正确率和答对题的数目,随后点击“上次记录”单选按钮,可以重做上次做错的题目。
    软件工程第二次作业——结对编程_第9张图片
    软件工程第二次作业——结对编程_第10张图片

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


# _*_ 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.心得体会

  • 通过此次结对编程,我们熟悉了两人合作的模式,提高了代码开发的效率;
  • 与此同时,我们互相帮助,互相教对方,在能力上的互补;
  • 我们一边编程,一边共享知识和经验,有效地在实践中进行学习。
    软件工程第二次作业——结对编程_第11张图片

你可能感兴趣的:(软件工程第二次作业——结对编程)