~yacinechaouche/+junk/BZR

« back to all changes in this revision

Viewing changes to PROG/LECOMPTEESTBON/calculator_r2.py

  • Committer: yacinechaouche at yahoo
  • Date: 2015-01-14 22:23:03 UTC
  • Revision ID: yacinechaouche@yahoo.com-20150114222303-6gbtqqxii717vyka
Ajout de CODE et PROD. Il faudra ensuite ajouter ce qu'il y avait dan TMP

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
def evaluate(s):
 
2
    first,second,third = tokenize(s)
 
3
    if is_number(first):
 
4
        #print "%s is a number -> put in var1" % first
 
5
        left = int(first)
 
6
    else:
 
7
        #print "%s is not a number -> eval" % first
 
8
        left = evaluate(first[1:-1])
 
9
        # if division by zero or division had reminders,
 
10
        # consider the whole expression wrong
 
11
        # This will speed up calculations by approx 2x
 
12
        if left == None:
 
13
            return
 
14
 
 
15
    if is_operand(second):
 
16
        #print "%s is an operation -> put in operation" % second
 
17
        operation = second
 
18
        
 
19
    else:
 
20
        print "%s is not an operation, skipping (this will crash)" % second
 
21
        return
 
22
        
 
23
    if is_number(third):
 
24
        #print "%s is a number -> put in var2" % third
 
25
        right = int(third)
 
26
    else:
 
27
        #print "%s is not a number -> eval" % third
 
28
        right = evaluate(third[1:-1])
 
29
        if right == None:
 
30
            return 
 
31
    result = do_op(left,operation,right)
 
32
    #print "we return the value of %s %s %s which is %s" % (left,operation,right,result)
 
33
    return result
 
34
 
 
35
def tokenize(s):
 
36
    #print "tokenize",s
 
37
    if s[0] not in ('(',')') :
 
38
        first  = read_number(s)        
 
39
        second = s[len(first)]
 
40
        third  = s[len(first)+1:]
 
41
    else:
 
42
        n   = 0
 
43
        pos = 0
 
44
        for x in s:
 
45
            if x == '(':
 
46
                n += 1
 
47
            if x == ')':
 
48
                n -= 1
 
49
            if n == 0:
 
50
                break
 
51
            pos += 1
 
52
        first  = s[0:pos+1]
 
53
        second = s[pos+1]
 
54
        third   = s[pos+2:]
 
55
    #print "tokenization : %s, %s , %s" % (first,second,third)
 
56
    return first,second,third
 
57
 
 
58
def read_number(s):
 
59
    c = ""
 
60
    while s[0].isdigit():
 
61
        c += s[0]
 
62
        s = s[1:]
 
63
    return c
 
64
 
 
65
def do_op(left,operation,right):
 
66
 
 
67
    # we may have None in one of those,
 
68
    # for example when dividing by 0
 
69
    
 
70
    if not (left and right) :
 
71
        return
 
72
    
 
73
    if operation == "+" :
 
74
        return left + right
 
75
    
 
76
    elif operation == "*" :
 
77
        return left * right
 
78
    
 
79
    elif operation == "/" :
 
80
        if right != 0 : 
 
81
            result =  left / right
 
82
            # we may only allow net divisions
 
83
            if not left % right :
 
84
                return result
 
85
    
 
86
    elif operation == "-" :
 
87
        return left - right
 
88
    
 
89
def is_number(s):    
 
90
    return s.isdigit()
 
91
 
 
92
def is_operand(s):
 
93
    return s in ["+","/","*","-"]
 
94
 
 
95
 
 
96
def main():
 
97
    import sys
 
98
    print evaluate(sys.argv[1])
 
99
 
 
100
if __name__ == "__main__":
 
101
    main()