1.程序结构图
2.实现源码
1 import re 2 3 def add_func(x,y): 4 return eval(x)+eval(y) 5 6 def subtract_func(x,y): 7 return eval(x)-eval(y) 8 9 def multiple_func(x,y): 10 return eval(x)*eval(y) 11 12 def division_func(x,y): 13 return eval(x)/eval(y) 14 15 def power_func(x,y): 16 return eval(x)**eval(y) 17 18 while True: 19 content = input("请输入你想要处理的计算式子: >>>") 20 est = re.search('[^0-9+\-*/().]', content) 21 if est: 22 print("you input invalid character") 23 receive = input("please input any character to continue or input q to quit >>>") 24 if receive == "q": 25 exit() 26 else: 27 continue 28 else: 29 break 30 31 print(eval(content)) 32 bracket_switch = False 33 cal_switch = False 34 power_flag = False 35 func_list = [add_func, subtract_func, multiple_func, division_func, power_func] 36 cal_character_list = ["\+", "-", "\*", "/", "\*\*"] 37 38 while True: 39 matching_bracket = re.search("\([^()]+\)", content) 40 if matching_bracket: 41 matching_content = matching_bracket.group() 42 accept = matching_content[1:-1] 43 else: 44 accept = content 45 bracket_switch = True 46 while True: 47 if "**" in accept: 48 cal_matching = re.search("-?[\d.]+\*\*-?[\d.]+", accept) 49 elif "/" in accept: 50 cal_matching = re.search("-?[\d.]+/-?[\d.]+", accept) 51 elif "*" in accept: 52 cal_matching = re.search("-?[\d.]+\*-?[\d.]+", accept) 53 else: 54 cal_matching = re.search("-?[\d.]+[+\-]-?[\d.]+", accept) 55 56 if cal_matching: 57 result = cal_matching.group() 58 # print(result) 59 filter_one = re.search("\d.+\d", result).group() 60 filter_two = re.search("[+\-*/]{2,3}", filter_one) 61 if filter_two: 62 judge = filter_two.group() 63 if "**" in filter_one: 64 choice = 4 65 power_flag = True 66 cal_character = cal_character_list[choice] 67 elif "*" in judge: 68 choice = 2 69 cal_character = cal_character_list[choice] 70 elif "/" in judge: 71 choice = 3 72 cal_character = cal_character_list[choice] 73 else: 74 if "+" in filter_one: 75 choice = 0 76 cal_character = cal_character_list[choice] 77 elif "-" in filter_one: 78 choice = 1 79 cal_character = cal_character_list[choice] 80 elif "*" in filter_one: 81 choice = 2 82 cal_character = cal_character_list[choice] 83 elif "/" in filter_one: 84 choice = 3 85 cal_character = cal_character_list[choice] 86 87 if not power_flag: 88 count_result = func_list[choice](re.findall(".+%s" %cal_character, result)[0][:-1], re.findall("%s.+" %cal_character, filter_one)[0][1:]) 89 # print(count_result) 90 if (choice == 2 or choice == 3) and result.count("-") == 2: 91 accept = re.sub("%s%s%s" % (re.findall(".+%s" %cal_character, result)[0][:-1], cal_character,re.findall("%s.+" %cal_character, result)[0][1:]), "+"+str(count_result), accept, 1) 92 else: 93 accept = re.sub("%s%s%s" % (re.findall(".+%s" %cal_character, result)[0][:-1], cal_character, re.findall("%s.+" %cal_character, filter_one)[0][1:]), str(count_result), accept, 1) 94 else: 95 count_result = func_list[choice](re.findall(".+%s" % cal_character, result)[0][:-2], re.findall("%s.+" % cal_character, result)[0][2:]) 96 accept = re.sub("%s%s%s" % (re.findall(".+%s" % cal_character, result)[0][:-2], cal_character,re.findall("%s.+" % cal_character, result)[0][2:]), str(count_result), accept, 1) 97 power_flag = False 98 99 else: 100 print(content) 101 content = re.sub("\([^()]+\)", str(count_result), content, 1) 102 103 #去除括号后,解决+-;--的情况 104 double_character = re.search('[\-+]{2}', content) 105 if double_character: 106 if "+-" in content: 107 content = re.sub("[\-+]{2}", "-", content) 108 elif "--" in content: 109 content = re.sub("[\-+]{2}", "+", content) 110 111 if bracket_switch: 112 content = re.sub(".+", str(count_result), content) 113 cal_switch = True 114 break 115 116 if cal_switch: 117 break 118 119 print(content)View Code