Chinaunix首页 | 论坛 | 博客
  • 博客访问: 328581
  • 博文数量: 83
  • 博客积分: 3193
  • 博客等级: 中校
  • 技术积分: 1679
  • 用 户 组: 普通用户
  • 注册时间: 2006-04-03 12:04
文章分类

全部博文(83)

文章存档

2013年(2)

2012年(6)

2011年(72)

2010年(2)

2009年(1)

分类: Python/Ruby

2012-02-28 13:52:10

       习惯了用C编写计算器,还真不大习惯用其他高级语言编写。最近终于开始学习python了,先编写个计算器牛刀小试验了一下(还有许多功能没有添加,为以后升级留了些代码):
::::::::::::::
__init__.py
::::::::::::::
#!/usr/bin/python

import os
import os.path
import re
import sys
import string
from optparse import OptionParser

VERSION = ("%prog: version 1.0")

EXIT_SUCCESS = 0
EXIT_IO_ERROR = 1
EXIT_OPTIONS_ERROR = 2
EXIT_INTERNAL_ERROR = 3
EXIT_VALIDATION_ERROR = 4
EXIT_ERROR = 5      # Converson, Process, or Unknown Error

#Description: make input line from string to a list,which includes both numbers and operational character
def makeline(old_line):
        line1=old_line
        line1=line1.replace('+',' ')
        line1=line1.replace('-',' ')
        line1=line1.replace('*',' ')
        line1=line1.replace('/',' ')
        nums=line1.split(' ')

        line2=list(old_line)
        op=[':']
        numbers=list('0123456789')
        for option in line2:
                if option in numbers:
                        continue
                else:
                        op.append(option)

        count=1
        line=[':']
        for my_num in nums:
                line.append(my_num)
                if count < len(op):
                        line.append(op[count])
                count=count+1
        #switch string to numbers
        for m in line:
                if m.isdigit():
                        line[line.index(m)]=string.atoi(m)
        del line[0]
        return line
       
#Description: calculate with 2 numbers and one operational character
def ops(op,left,right):
        if op == '+':
                return left+right
        if op == '-':
                return left-right
        if op == '*':
                return left*right
        if op == '/':
                return left/right

#Description: push the calculate result into the list and delete the useless element in the list
def monobasic_ops(op,newline):
        pos=newline.index(op)
        left=newline[pos-1]
        right=newline[pos+1]
        newline[pos-1]=ops(op,left,right)
        del newline[pos+1]
        del newline[pos]
        return newline
                       
def main():
       
        desc = ("Calculation on +,-,*,/   Example:                                                                "
        "       14+21                                                                                             "
        "       Answer: 14+21 = 35\n")

        usage = ("usage: %prog [-f|--file] filname\n"
                 "       %prog [-q|--quit]")


        parser = OptionParser(version=VERSION, description=desc, usage=usage)
        parser.add_option("-f","--file", dest="filename", help="write report to FILE",metavar="FILE")
        parser.add_option("-q","--quit", action="store_false", dest="verbose", default=True, help="log out")
        (options, args) = parser.parse_args()
               
        if len(sys.argv) != 1:
                parser.print_help()
                sys.exit(EXIT_SUCCESS)

        line=raw_input()
       
        newline=makeline(line)
        length=len(newline)

        while length > 1:
                flag=0
                #Calculate with '*' or '/':
                for m in newline:
                        if m == '*' or m == '/':
                                flag=1  #calculate '*' or '/' first
                                newline=monobasic_ops(m,newline)
                                break
               
                #Calculate with '+' or '-':
                if flag == 0:
                        for m in newline:
                                if m == '+' or m == '-':
                                        newline=monobasic_ops(m,newline)
                                        break
                       
                length=len(newline)
        print "Answer: %s = %d " % (line, newline[0])


阅读(2405) | 评论(0) | 转发(0) |
给主人留下些什么吧!~~