Calculator--python Regular Expressions

Source: Internet
Author: User

Using regular expressions

1, realize subtraction and extension number priority analysis
2, the 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 (), +,-, *, /symbols and formulas (cannot invoke lazy implementations such as eval, etc.), the result of the operation, the result must be consistent with the results of the actual calculator.

This is calculated using regular expressions in three steps:

1, search for the innermost () such as the above expression first is ( -40/5)

2, then remove (), start calculation, calculate first */

3, the final calculation +-, and then the result of the calculation-8.0 instead of the previous ( -40/5)

The specific code is as follows:

#-*-coding:utf-8-*-"@auther: Starry@file:work.py@time:2018/4/8 20:32 ' import re ' develop a simple Python calculator 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 must be Parse the inside (), +,-, *,/symbols and formulas (can not call eval and other similar functions lazy implementation), the result of the operation, the result must be consistent with the results of the actual calculator "' Def mulanddiv (s):" ' no parentheses, but there is a +-*/expression, only the calculation */Operator:p Aram S: An expression with *,/, + or-: return: The result of the calculation ' while Re.search (R ' [\*/] ', s): ss = Re.search (R ' [\ D\.] +[\*/]-? [\d\.] + ', s). Group () if ' * ' in ss:result = str (float (ss[:ss.index (' * ')]) *float (Ss[ss.index (' * ') + 1:])) E Lse:result = str (float (ss[:ss.index ('))])/float (Ss[ss.index ('/') + 1:])) s = s.replace (ss, result) re    Turn Sdef addandsub (s): ' calculates that only the +-expression is likely to appear '--', which will replace all two of them with a +:p Aram S: only +-the expression: return: The result of the calculation ' ' Array = Re.findall (R ' ([\d\.] +|-|\+) ', s) for I in range (len (array)-1): if array[i] = = '-' and array[i+1] = = '-': array[I] = array[i+1] = ' + ' for I in range (len (array)): if array[i] = = '-': array[i] = ' + ' array    [I+1] = str (float (array[i+1]) *-1) sum = 0.0; For a in array:if a! = ' + ': sum + = float (a) return str (SUM) def calculator (s): "' Calculates the innermost (), the result    Replace the previous value. :p Aram S: The expression to be evaluated: return: The answer of the calculation "while" (' in S:SS = Re.search (R ') ([^ ()]+\) ', s). Group () SSS =    Addandsub (Mulanddiv (Ss[1:-1])) s = s.replace (ss,sss) return Addandsub (Mulanddiv (s)); if __name__ = = ' __main__ ': SS = input (). Replace (', ') # SS = ' 1-2 * ((60-30 + ( -40/5) * (9-2*5/3 + 7/3*99/4*2998 +10 * 568/14))-( -4*3)/ (16-3*2)) '. Replace (', ') Try:print (Calculator (ss)) Except:print ("The expression is wrong!") ") Print (eval (ss))

  

  

Calculator--python Regular Expressions

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.