• 开始学习python了,第一个程序是一个逆波兰式的表达式分析器


    做了许多年的asp,这两年又是一直在做t-sql,一直想学点新的东西,C#/ASP.NET是必然的了,不过没有给我多大的惊喜,一切都是老套路,倒是python,让我眼前一亮,特别是源代码格式缩进成了语法要求,让我这个写程序十分注意缩进的人非常喜欢。
    这里是我第一个python程序,是一个逆波兰式的表达式分析器,完全照抄另外一个C语言版本的,原作者看到了请见谅哈!
    非常简陋,什么错误检测都没有,纯粹是练笔的东东:
     1#My first python
     2import sys
     3def parse(str):
     4    """Expression Parser"""
     5    (s, exp, i, str) = ([], [], 0, "(" + str + ")#")
     6    while str[i] != "#":
     7        if (str[i] >= '0' and str[i] <= '9'or (str[i] >= 'a' and str[i] <= 'z'):
     8            exp.append(str[i])
     9
    10        elif str[i] == '(':
    11            s.append(str[i])
    12
    13        elif str[i] == ')':
    14            while s[-1!= '(':
    15                exp.append(s.pop())
    16            s.pop()
    17
    18        elif str[i] in ['+''-']:
    19            while s[-1!= '(':
    20                exp.append(s.pop())
    21            s.append(str[i])
    22
    23        elif str[i] in ['*''/']:
    24            while s[-1in ['*''/']:
    25                exp.append(s.pop())
    26            s.append(str[i])
    27        i += 1
    28    print '=============='
    29    print exp
    30
    31if __name__ == "__main__":
    32    parse("a-b*c/(3+6)")
    33

    要是python支持t-sql那样的between语法就好了。
  • 相关阅读:
    hdu 3018
    poj 1833 排列
    poj 1256 Anagram
    CF 548B Mike and Fun
    CF 548A
    【冰茶几专题】F
    【冰茶几专题】C
    535 C.Tavas and karafs
    [WA]cf 534 D. Handshakes
    cf 534C. Polycarpus' Dice
  • 原文地址:https://www.cnblogs.com/ifan/p/1237010.html
Copyright © 2020-2023  润新知