Using Python to implement calculator functions

Source: Internet
Author: User

# Implement a complex subtraction precedence operation with parentheses
# mainly used in regular expressions, while loops, lists of several methods.
# to implement the required arithmetic functions the code is divided into two parts, and one is a function that implements the subtraction operation without parentheses Add_sub_mul_div
# The other one is a loop plus 6 lines of code to constantly remove the inner parentheses and complete the parentheses inside the expression, which will call the function continuously Add_sub_mul_div
# Instance A = ' 1-2* (3+4/((5-6*7) +8) *9/10-(11+12) *13*14/15/(16-17)/18/19) *20 '
# operation Result A =-146.67385897694427
# Beginner Python code may be complicated ... Will humbly accept the advice and continue to learn!ps: Morning spent four hours to write subtraction and continue to verify the case ...
# Two hours in the afternoon to write the function of the parentheses and invoke the subtraction verification instance, each time after the completion of the run, but there are various errors ...
# such as the processing of a positive negative integer, and the beginning of the write function multiplication operation from left to right does not take into account ... Wait a minute

# code Body:

Import re
A = ' 1-2* (3+4/((5-6*7) +8) *9/10-(11+12) *13*14/15/(16-17)/18/19) *20 '
b = Re.findall (' ([\d\.] +|-[\d\.] +|\+|/|\*|-|\ (|\) ', a) # Displays the positive, negative, and subtraction operators in a string individually in the list

# due to direct copying over the format is too messy, directly pasted.

Using Python to implement calculator functions

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.