Implement a simple calculator with Python

Source: Internet
Author: User

Calculator Development Requirements

    1. Implementation of subtraction and extension priority resolution
    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 inside (), +,-, *,/character Numbers and formulas, and the results must be consistent with the results of the actual calculator.



#coding =utf-8import redef Sub_calc (Formula): "" Calculates the value of a subtraction expression "" "flag = True while flag:m = Re.search (' \ \ D+\.? \d*[*/]-?\d+\.? \d* ', formula) #先匹配乘除 Example: 5.12 * -123.12,-5.12/ -123.12 If m:sub_mul_div = M.group () re s = CALC_MULTI_DIVD (sub_mul_div) formula = Formula.replace (sub_mul_div,res) # Replaces the result of the multiplication expression, loops to determine if there is a multiplication expression Print "*" * 10, "multiplication expression:", Formula Else:print "*" * 10, "multiplication calculated" flag = False print "*" * 10, "the expression after calculating multiplication in parentheses", formula result = Calc_add_stract (Formula) #计算加减法 return result def CALC_MULTI_DIVD (formula) : "" "computes a single multiplication expression" "" Print "*" * 20, "single multiplication expression", formula F_list = Re.split (' [*/] ', formula) print "*" * 20, "single multiply In addition to the expression list ", f_list if ' * ' in formula:res = float (f_list[0]) * FLOAT (f_list[1]) elif '/' in formula:res = Float (f_list[0])/float (f_list[1]) return str (RES) def calc_add_stract (Formula): "" Calculates the addition and subtraction, computes "" "by reduce 22" "" Print "*" * 20, "no sign of subtraction expression:", formula #对符号进行处理 formula = Formula.replace ('---', ' + ') formula = Formula.replace (' + + ') Formula = Formula.replace ('-+ ', '-') formula = Formula.replace (' +-', '-') print "*" * 20, "add minus expression with sign:", formula #对一个加减表达式,-Becomes +-, in order to make a split list for the reduce calculation formula = Formula.replace ('-', ' +-') Formula_list = formula.split (' + ') pr int "*" * 20, "list divided by plus sign:", Formula_list #第一个数为负数, the first field in the list is empty if formula_list[0] = = ": Formula_list = Formula_lis T[1:] "" result = 0 for i in formula_list:if '-' in i:stract_list = I.split ('-') result = Float (stract_list[0])-float (stract_list[1]) + result Else:result + = float (i) "" "Res Ult = reduce (lambda x,y:float (x) + float (y), formula_list) print "*" * 20, "Add minus expression results:", result return Str (result) def Calc (Formula): Flag = True while flag:m = Re.search (' \ ([^ ()]*\) ', formula) if M:print "*" * 5, "calculate the match to the bracket expression:", M.GRoup () Sub_formula = M.group (). Strip (' () ') res = Sub_calc (Sub_formula) #先计算括号内的表达式 F Ormula = Formula.replace (M.group (), res) #将表达式计算出来的结果替换原括号, once again cycle to determine if there are parentheses Else:flag = False print "* "* 5," The final post-brace expression: ", formula result = Sub_calc (Formula) return result if __name__ = = ' __main__ ': formula = "1-2 * ((60-30 + ( -9-2-5-2*3-5/3-40*4/2-3/5+6*3) * ( -9-2-5-2*5/3 + 7/3*99/4*2998 +10 * 568/14))-( -4*3)/(16-3*2)) "Print" Expression to evaluate: ", formula #去掉所有的空格 formula = re.sub (' \s ',", formula) print "expression after whitespace removed", Formula res = cal C (Formula) print "final result:", res

Implement a simple calculator with Python

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.