# 软件工程（2019）结对编程第二次作业

## 1.组员

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

## 3.各部分功能截图

（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_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.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.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)

self.setLayout(mainLayout)
self.setWindowTitle(u"随机出题计算器   (注：保留小数点后1位)")

def closeEvent(self, event):
QtWidgets.QMessageBox.Yes,
QtWidgets.QMessageBox.No
)
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)整数出题:

• 首先点击“整数运算”单选按钮，随后点击“开始”按钮，然后屏幕上会随机出现整数运算的题目，可以在答案输入框输入答案，点击“提交”按钮，会显示出题目的正确答案，以及正确率和答对题目数量。

• 整数实现代码
# _*_ 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

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':
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)分数运算:

• 首先点击“分数运算”单选按钮，其余部分同整数运算。

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

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] == '+':
elif oprator[0] == '-':
elif oprator[0] == '*':
else:
question_list.append(question_str)

question_list.append(question_str)
return question_list


(4)退格功能

• 点击“退格”按钮以后，会清除表达式的最后一个字符。
• 退格前：
• 退格后：
• 退格代码实现
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，清除掉原有表达式。

• 清除前：

• 清除后：

• 退格代码实现

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


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

• 重启后会记忆上次答题数目以及正确率和答对题的数目，随后点击“上次记录”单选按钮，可以重做上次做错的题目。

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

# _*_ 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

lastQuestionList = []
with open('store.txt', 'r') as f: