码迷,mamicode.com
首页 > 其他好文 > 详细

计算器 暂时没解决小数问题

时间:2020-03-04 21:02:06      阅读:57      评论:0      收藏:0      [点我收藏+]

标签:style   print   dal   exception   ==   eva   val   except   find   

 1 import re
 2 
 3 num_one = (98798465*5465+4654-5464*45616846+84654)
 4 print(eval(num_one))
 5 
 6 total = 0
 7 print(num_one)
 8 while True:
 9     try:
10         a = re.search(\((\-)(\d+), num_one)
11         if a:
12             num_one = num_one.replace(a.group(1), ‘‘, 1)
13             num_one = num_one.replace(), ‘‘)
14             num_one = num_one + str(-2 * int(a.group(2))) + )
15             print(num_one)
16         first = re.search([*|/], num_one)
17         print(first.group())
18         try:
19             if first.group() == *:
20                 num1 = re.findall((\d+)\*(\d+), num_one)
21                 total1 = int(num1[0][0]) * int(num1[0][1])
22                 # total1 = int(num1[0][0]) * int(num1[0][1])
23                 total += total1
24                 num_one = re.sub(\d+\*\d+, str(total1), num_one, 1)
25                 print(num_one, 11111)
26             elif first.group() == /:
27                 num1 = re.findall((\d+)/(\d+), num_one)
28                 total1 = int(num1[0][0]) / int(num1[0][1])
29                 total += total1
30                 num_one = re.sub(\d+\/\d+, str(total1), num_one, 1)
31                 print(num_one)
32         except Exception as f:
33             break
34     except Exception as d:
35         break
36 while True:
37     try:
38         first = re.search(\d+([+|-]), num_one)
39         print(first.group(1))
40         try:
41             if first.group(1) == +:
42                 num1 = re.findall((\d+)\+(\d+), num_one)
43                 total1 = int(num1[0][0]) + int(num1[0][1])
44                 # total1 = int(num1[0][0]) * int(num1[0][1])
45                 total += total1
46                 num_one = re.sub(\d+\+\d+, str(total1), num_one, 1)
47                 print(num_one, 222222)
48             elif first.group(1) == -:
49                 num1 = re.findall((\d+)\-(\d+), num_one)
50                 total1 = int(num1[0][0]) - int(num1[0][1])
51                 total += total1
52                 if total1 < 0 and len(re.findall(\d+, num_one)) > 2:
53                     num_one = re.sub(\d+\-\d+, str(abs(total1)), num_one, 1)
54                     num_one = num_one.replace(), ‘‘)
55                     num_one = num_one + str(total1 * 2) + )
56                 elif total1 < 0 and len(re.findall(\d+, num_one)) <= 2:
57                     num_one = str(total1)
58                     break
59                 else:
60                     num_one = re.sub(\d+\-\d+, str(total1), num_one, 1)
61                     print(num_one, 33333)
62         except Exception as f:
63             break
64     except Exception as d:
65         break
66 num_one = re.sub([()], ‘‘, num_one)
67 print(num_one)
68 输出:
69 290683253989
70 290683253989

 

计算器 暂时没解决小数问题

标签:style   print   dal   exception   ==   eva   val   except   find   

原文地址:https://www.cnblogs.com/ch2020/p/12412014.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!