re module based calculator


Calculator development requirements

  1. Realize addition, subtraction, multiplication and division and extension number priority analysis
  2. User input 1 - 2 * ( (60-30 +(-40/5) * (9-2*5/3 + 7 /3*99/4*2998 +10 * 568/14 )) - (-4* 3)/ (16-3*2) ) and other similar formulas, you must parse the (),+,-,*,/ symbols and formulas inside by yourself, and get the result after the operation, and the result must be the same as that obtained by the real calculator results are consistent.

1  # 1. The formula used to calculate a+b. where a and b are integers or finite decimals. No spaces and parentheses. There are no other four calculations. 
2  
3  
4  import re,functools
 5  
6  def addtion():
 7      formula = input( " Please enter a formula (strictly according to a+b) format: " )   #Get a formula string 
8      num_list = re.split( " \ + " ,formula) #It   should be noted here that the plus sign is a meta character, and an escape character must be added before it. The result is a list [a,b] 
9      print (num_list)
 10      return float(num_list[0]) + float(num_list[1 ])
 11  
12  print (addtion())
a+b

 

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=325600405&siteId=291194637