home *** CD-ROM | disk | FTP | other *** search
/ Freelog Special Freeware 31 / FreelogHS31.iso / ArgentCompta / Bankperfect / bp.exe / Scripts / Calc / calc.py < prev   
Encoding:
Python Source  |  2005-12-18  |  2.5 KB  |  73 lines

  1. from math import sqrt,pi,sin,cos,tan,cosh,sinh,tanh,atan,asin,acos,atan2,degrees,radians,e,log,log10
  2.  
  3. last_dms = ""
  4. dic_dms = {"░":"'","'":"\"","\"":"░","":"░"}
  5.  
  6. def Focus():D.SetFocus();D.SelStart = len(D.Text)
  7.  
  8. def KeyPress(S, K):
  9.   global last_dms
  10.   if K.Value in "+-*/": last_dms = ""
  11.   if ord(K.Value) == 13:
  12.     K.Value = None
  13.     Eval(1)
  14.  
  15. def Key(S):
  16.   global last_dms
  17.   c = S.Caption
  18.   T = D.Text
  19.   if c == "DMS":
  20.     if T == "": Focus();return
  21.     l = T[-1]
  22.     if l in "░'\"": D.Text = T[:-1] + dic_dms[l]
  23.     else:
  24.       if "░" in T: last_dms = dic_dms[last_dms]
  25.       else: last_dms = "░"
  26.       D.Text += last_dms
  27.   else:
  28.     if not c in "0123456789": last_dms = ""
  29.     if len(c) > 2 or c == "ln": c += "("
  30.     if c == "¼": D.Text = T[:-1]
  31.     elif c == "AC": D.Text = ""
  32.     else: D.Text += {"e":"e^","╓":"sqrt(","p":"pi","EE":"E","Log(":"Log("}.get(c, c.lower())
  33.   Focus()
  34.  
  35. def Eval(S):
  36.   global last_dms
  37.   D.Font.Color = 0
  38.   if S != 1 and not "=" in D.Text: return
  39.   D.Text = s = D.Text.replace("=", "")
  40.   dms = "░" in s or "'" in s or "\"" in s
  41.   rp = {"░":"*1.0+","'":"/60.0+","\"":"/3600.0",",":".","▓":"**2","^":"**","%":"*0.01","/":"*1.0/","û":"-","deg(":"degrees(","rad(":"radians(","Log(":"log10(","ln(":"log("}
  42.   for k in rp: s = s.replace(k, rp[k])
  43.   if s[-1] == "+": s = s[:-1]
  44.   try: 
  45.     R = str(eval(s))
  46.     if dms:
  47.       R = float(R)
  48.       d = int(R)
  49.       R = (R - d) * 60
  50.       m = int(R)
  51.       s = int((R - m) * 60)
  52.       D.Text = "%d░ %d' %d\"" %(d, m, s)
  53.     else: D.Text = R
  54.     last_dms = ""
  55.   except: D.Font.Color = 0x000000CC
  56.   Focus()
  57.  
  58. f = CreateComponent("TForm", None)
  59. f.SetProps(Position="poMainFormCenter", Width=510, Height=300, Caption="Calculatrice BankPerfect", BorderStyle="bsSingle", BorderIcons=["biSystemMenu"])
  60. f.Font.Name = "Arial"
  61. f.Font.Size = 11
  62. D = CreateComponent("TEdit", f)
  63. D.SetProps(Parent=f, Left=20, Top=20, Width=470, Color = 0x00CEFFF4, OnChange=Eval, OnKeyPress=KeyPress)
  64.  
  65. for i, c in enumerate(["AC","¼","╓","DMS","EE","cos","Acos","cosH","7","8","9","/","%","sin","Asin","sinH","4","5","6","*","^","tan","Atan","tanH","1","2","3","û","(","Deg","Rad","p",".","0","=","+",")","ln","Log","e"]):
  66.   p = CreateComponent("TBitBtn", f)
  67.   p.SetProps(Parent=f, Left=20+60*(i%8), Top=60+40*(i/8), Width=50, Height=30, Caption=c, OnClick=Key)
  68.   if c in "lnLoge": p.Font.Color = 0x00CC0000
  69.   elif c in "AtanHAcosHAsinHDegRadp": p.Font.Color = 0x00007700
  70.   elif c in "AC¼+û*/=╓DMS%^EE()": p.Font.Color = 0x000000CC
  71.   if c in "¼╓p": p.Font.Name = "Symbol"
  72.  
  73. f.ShowModal()