Python's easy-to-write calculator program

Source: Internet
Author: User

Calculator ideas

1. Input formula required

2. Whether the test formula contains letters

3, if the inclusion of letters will need to re-enter, otherwise the next operation

4, detection of the existence of parentheses, if there is parentheses, the value of the parentheses inside the priority operation, and then assigned to the original position, no then the priority operation, the result of the final operation is taken out

Flow chart

650) this.width=650; "src=" Http://s3.51cto.com/wyfs02/M02/75/97/wKiom1Y9X5jzd3J1AAIx39nu4dQ270.jpg "title=" Calculator. png "alt=" wkiom1y9x5jzd3j1aaix39nu4dq270.jpg "/>


The specific code is as follows:#!/usr/bin/env python# -*- coding: utf-8 -*-__author__ =  ' Weibinf ' import re    #导入模块def  kuohao (self):    #判断是否存在括号      Self = list (self)     if self.count (') ')  != 0 :         for k,v in enumerate (self):             if v ==  ")":                 x = k                 a = self[0:k]                 q = a.count ("(")                  qq = 0      &nbSp;          for k,v in enumerate (a):                      if v ==  "(":                         z = k                          qq +=1                         if q == qq :                              b = self[z+1:x]                             c =  str (Fushu (b))                              del self[z:x+1]                              self.insert (z,c)                               return kuohao (self)     else:         return fushu (self) Def fushu: #取出所有运算符和相应运算数字     temp =   '     expp = []    self = list (self)      i =&nbsP;0    for k,v in enumerate (self):         if v ==  ' + '  or v ==  '-'  or v ==  ' * '  or  v ==  '/':             if k  == 0 or self[k-1] ==  "+"  or self[k-1] ==  "-"  or  self[k-1] ==  "*"   or self[k-1] ==  "/"  :                 temp += v             else:                 expp.append (int (temp))                  expp.append (v)                  temp =  '         else:             temp += v         if i == len (self)  -1 :             expp.append (int (temp))         i +=1     return zhengshu (expp) Def get_aob (a, o, b): #运算方式      if o ==  ' + ':         return a + b     elif o ==  '-':         return  a - b    elif o ==  ' * ':         return a * b    elif o ==  '/':       &nbSp; return a / bdef zhengshu (exp_list): #运算公式     if len (exp_ List)  == 1:        return exp_list[0]     else:        q = exp_list.count ("*")          w = exp_list.count ("/")          if q != 0 or w != 0:             for k,v in enumerate (exp_list):                 if v ==  "*"  or v ==   "/":                     a = exp_list[k - 1]                     o = exp_list[k]                     b = exp_list[k+1 ]                     del exp_list[k]                     del exp_list[k]                     exp_list[k-1] = get_aob (A,  O, B)                      return zhengshu (exp_list)          else:            for k,v in  Enumerate (exp_list):  &Nbsp;              if v ==   "+"  or v ==  "-":                     a = exp_list[k - 1]                     o  = exp_list[k]                     b = exp_list[k+1]                     del exp_list[k]                      del exp_list[k]                     exP_LIST[K-1] = GET_AOB (a, o, b)                      return zhengshu (exp_list)   #   return zhengshu (exp_list) while true:    calc = raw_input ("\033[ 31m Enter the formula you want to calculate:  \033[0m "). Replace ('   ', '). Strip ()     #获取用户输入     a  = re.findall (' [a-za-z]+ ', calc)   #正则匹配是否存在字母     if not len (Calc)  == 0 :  #判断用户输入是否为空         if len (a)  ==  0:  #判断用户输入字符串是否存在字母             print   "\033[31m program operation results:  \033[0m", Kuohao (Calc)              print  ' \033[43;1mpython calculator results: \033[0m ', eval (calc)          else:            print  "\033[31m is not entered correctly and the arithmetic string contains letters. \033[0m "    else:        print " \033[ 31m not entered correctly, please re-enter ... \033[0m "        print " \033[31m-\033[0m " * 50


Python's easy-to-write calculator program

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.