~yacinechaouche/+junk/BZR

« back to all changes in this revision

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