3 Python Lab Calculator: Subtraction

Source: Internet
Author: User

#--time:2017/12/31 1:25
#--file:day18 Calculator job. py
#__author: "CFP"
#date: 2017/12/30
Import re
Source= ' 1+ (2*2)/11+3++ (2+-2) +2 '


# #判断子串是否合法
def check_string (source):
Flat = True
If Re.search (' [a-za-z] ', source):
Print ("Invalid illegal characters! ")
Flat = False
If Source.count ("(")! = Source.count (")"):
Print ("Expression error, parenthesis not closed! ")
Flat = False
return flat

# #将子串格式化
def format_string (source):
Source = Source.replace ("", "")
Source = Source.replace ("+ +", "+")
Source = Source.replace ("-+", "-")
Source = Source.replace ("+-", "-")
Source = Source.replace ("/+", "/")
Source = Source.replace ("*+", "*")
return source

# #乘除法函数, pass in one () content at a time
def calc_mul_div (String):
Mul_regular = ' \d+\.? \d*\*\d+\.? \d* ' # #乘法匹配模式去掉 ()
Div_regular = ' \d+\.? \d*\/\d+\.? \d* ' # #乘法匹配模式去掉 ()

# #循环进入条件, when () this string has an * number
While Re.findall (' \* ', string):

# #一次匹配一组args * args data
expression = Re.search (mul_regular,string). Group ()

# #计算args * Args This set of data
I,v = Expression.split (' * ')
Mul_result = str (float (i) *float (v))

# #将计算后的一组args * args data; replace args * args
String = String.Replace (Expression,mul_result)

# # #将子串在进行格式化下
String = String.Format (String)

While Re.findall (' \ \ ', string):
expression = Re.search (div_regular,string). Group ()
I,v = Expression.split ('/')
Div_result = str (float (i)/float (v))
String = String.Replace (Expression,div_result)
String = String.Format (String)
return string

# #加减法函数, pass in one () content at a time
def calc_add_sub (String):
Add_sub_regular = ' \d+\.? \d*\+\d+\.? \d* '
Add_sub1_regular = ' \d+\.? \d*\-\d+\.? \d* '
While Re.findall (' \+ ', string):
expression = Re.search (add_sub_regular,string). Group ()
I,v = expression.split (' + ')
Add_result = str (float (i) +float (v))
String=string.replace (Expression,add_result)
String = format_string (String)
While Re.findall (' \-', string):
expression1 = Re.search (Add_sub1_regular, String)
Try
Expression1=expression1.group ()
Except Attributeerror:
Break
I,v = Expression1.split ("-")
Sub_result = str (float (i)-float (v))
String=string.replace (Expression1,sub_result)
String=format_string (String)
return string

# #子串通过检查函数进入if语句
If check_string (source):
Print ("Souece:", source)
Source = format_string (source)
Print ("Format:", source)

# #打印子串通过eval得到数据用于验证, the results of the code are consistent
Print (' eval: ', eval (source))

# #当子串有 () into the loop until there is no (); Execute Else statement
While Re.search (' \ (', source):

# #匹配最深处的 () data and pass it to the subtraction function to resolve () data
Strs_regular = ' \ ([^ ()]+\) '
STRs = Re.search (strs_regular,source). Group ()
Strs_result = Calc_mul_div (STRs)
Strs_result = Calc_add_sub (Strs_result)

# #将 () Remove both sides (and), leaving only the contents, and replace the original ()
Strs_result = Strs_result[1:-1]
Source=source.replace (Strs,strs_result)
Source = format_string (source)
Else
Strs_result = Calc_mul_div (source)
Strs_result = Calc_add_sub (Strs_result)
Source=source.replace (Source,strs_result)
Source = format_string (source)

Print ("calculator:", source)



3 Python Lab Calculator: Subtraction

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.