自動生成四則運算題目 python實現

2022-07-05 12:42:10 字數 4096 閱讀 2319

專案倉庫: 位址

僅包含四則運算

結果不能為負數

數字大小在 100 以內

支援真分數運算

專案檔案結構如下:

模組功能

main.py

主函式(表示式生成, 表示式的求解)

maintest.py

測試函式(單元測試)

1. 分析與設計

本設計設計棧的使用, 逆波蘭表示式(字尾表示式)

表示式式生成

仔細分析有如下特點:

具體實現步驟

利用 python 的字串來儲存表示式

隨機生成乙個運算數

再隨機選擇乙個四則運算符

重複步驟 1 和 2

為了美觀和操作方便, 表示式中運算子和運算數使用空格隔開

求解表示式

中綴表示式轉換為字尾表示式, 再進行求值

具體**實現

表示式生成**

def makeformula(upperlimit=100, fraction=false) -> str:

if fraction:

upperlimit = 20

count = randint(4, 8)

else:

count = randint(1, 3)

build = ""

number1 = randint(1, upperlimit)

build += str(number1)

for i in range(count):

if fraction and (i+1) % 2:

operation = 3

elif fraction:

operation = randint(1, 2)

else:

operation = randint(1, 3)

number2 = randint(1, upperlimit)

op = ' ' + op[operation] + ' '

build += op + str(number2)

return build

中綴表示式轉換為字尾表示式

def getpostfixexpression(infixexpr: str) -> list[str]:

# 記錄操作符優先順序

prec =

postfixlist =

operatorstack =

# 以空格分割表示式, 並轉為字元陣列

tokenlist = infixexpr.split()

# 中綴表示式轉換為字尾表示式

for token in tokenlist:

if token in "+-*/":

while operatorstack and \

prec[operatorstack[-1]] >= prec[token]:

elif token == '(':

elif token == ')':

toptoken = operatorstack.pop()

while toptoken != '(':

toptoken = operatorstack.pop()

else:

while operatorstack:

return postfixlist

求解字尾表示式

def solvingpostfixexpression(postfixlist: list[str]):

operandstack =

# 計算字尾表示式

for token in postfixlist:

if token in "+-*/":

operand2 = operandstack.pop()

operand1 = operandstack.pop()

try:

result = domath(token, operand1, operand2)

except:

return "error: dividend cannot be 0"

else:

return operandstack.pop()

def domath(op: str, number1, number2):

if op == '+':

return number1 + number2

elif op == '-':

return number1 - number2

elif op == '*':

return number1 * number2

else:

return fraction(number1, number2)

主要使用unittest進行單元測試

具體**如下

class mytestcase(unittest.testcase):

def test_solvingpostfixexpression_valid(self):

test = solvingpostfixexpression(

['3', '5', '/', '1', '6', '/', '-'])

result = fraction(13, 30)

self.assertequal(test, result)

test = solvingpostfixexpression(

['3', '15', '*', '20', '4', '/', '-'])

result = 40

self.assertequal(test, result)

def test_solvingpostfixexpression_invalid(self):

test = solvingpostfixexpression(['2', '0', '/'])

result = "error: dividend cannot be 0"

self.assertequal(test, result)

test = solvingpostfixexpression(

['6', '5', '10', '2', '/', '-', '/'])

result = "error: dividend cannot be 0"

self.assertequal(test, result)

def test_getpostfixexpression(self):

test = getpostfixexpression("22 / 2")

result = ["22", "2", "/"]

self.assertequal(test, result)

test = getpostfixexpression("38 - 5 * 6")

result = ['38', '5', '6', '*', '-']

self.assertequal(test, result)

test = getpostfixexpression("( 16 - 9 ) * 6")

result = ['16', '9', '-', '6', '*']

self.assertequal(test, result)

if __name__ == '__main__':

unittest.main()

直接使用 pycharm 自帶的效能測試工具進行效能分析

執行100萬次的分析結果如下:

自動生成四則運算題目

主要功能 用於計算小學四則運算。設計思想 乙個能處理四則運算的程式,實現語言c 可以處理實數。源 include stdafx.h include calc win.h include calc windlg.h ifdef debug define new debug new endif afxe...

自動生成四則運算題目

1 include2 include3 include 4 define n 30 130201139白天涯 5main 631 printf d t t q 32 33break 34 35break 36 case2 3751 printf 0.2f t d 52 53break 54 55br...

四則運算題目自動生成程式

四則運算表示式生成器 目錄 專案參與者 羅泉水3118005101陳鴻暢3118005087psp2.1 personal software process stages 預估耗時 分鐘 實際耗時 分鐘 planning 計畫30 estimate 估計這個任務需要多少時間 30developmen...