建二叉树和怎么运算结果的代码链接已经表明,本文的重点不是这个
首先就是将二叉树序列化,这个文件命名为package.py
import pickle def package(*args): """serialisation""" ''' A package named pickle was used. ''' t1 = open("exercises.py", 'wb') '''open the file''' '''Pay attention, the file will be create if it was not existed''' pickle.dump(args, t1) '''dump() will save the args into file''' t1.close() def read(): '''do not forget to close the file''' t2 = open('exercises.py', 'rb') b = pickle.load(t2) '''Read it''' t2.close() print(b) '''Thus, we create a file to save a binary in to a binary file. ''' def delete_content(): """ After save the binary tree, we cannot run the code correctly """ with open("exercises.py", 'r+') as file: file.truncate(0) file.close() ''' the file was empty'''
然后就是检查括号和运算符是否有误!这个文件命名为check.py
def check(expre): lst1 = list(expre) l = len(lst1) a = 0 b = 0 lst = list(expre) ''' use a list to save the data of expression. ''' for j in lst: if j == "+" or j == "-" or j == "*" or j == "/": a += 1 elif j == "(" or j == ")": b += 1 else: pass if lst[0] != "(" or lst[-1] != ")": return 1 if b > 2*a: return 1 elif b < 2 * a: return 0 for i in range(1,l): if lst1[i] in "0123456789" and lst1[i-1] == "(" and lst1[i+1] == ")": return 0 '''it will find some brackets mismatched like (1+2)) or 1+2''' def operator_missing(expre): expre1 = list(expre) lst = [] for j in expre1: if j == "+": lst.append("+") if j == "-": lst.append("-") if j == "*": lst.append("*") if j == "/": lst.append("/") if j == "1": lst.append("1") if j == "2": lst.append("2") if j == "3": lst.append("3") if j == "4": lst.append("4") if j == "5": lst.append("5") if j == "6": lst.append("6") if j == "7": lst.append("7") if j == "8": lst.append("8") if j == "9": lst.append("9") if j == "0": lst.append("0") l = len(lst) if l == 1: pass if l > 1: for i in range(1, l): if lst[i] in "0123456789" and lst[i-1] in "0123456789": return 1 else: pass
最后一个就是创建一个文件test.py作为主程序,其余的文件均作为包来调用
import os import unittest import check import package import binary_tree class TestException(unittest.TestCase): """ Moreover, some unit tests are necessary """ ''' unittest was used in this case ''' def test(self): formulas = input("Enter your equations: ") ''' input an equation, and go on. ''' if check.check(formulas) == 0: """ using check() to check the Exception """ raise Exception("Not a valid expression, operator missing.") elif check.operator_missing(formulas) == 1: ''' using operator_missing() to check another Exception''' raise Exception("Not a valid expression, operator missing.") elif check.check(formulas) == 1: ''' using check() to check brackets mismatched.''' raise Exception("Not a valid expression, brackets mismatched.") else: fTree = binary_tree.BuildTree(formulas) eTree = fTree.createTree() ''' build a tree ''' value = fTree.evaluate(eTree) ''' get the result value ''' if os.path.getsize("exercises.py") == 0: package.package(eTree) package.read() '''if the file has no content, binary tree will be save''' else: package.read() package.delete_content() '''if the file has content, it will be delete and become empty.''' '''package.delete_content()''' '''it will delete the content of binary file''' print(value) if __name__ == "__main__": unittest.main()