Page 1 sur 1

Evaluateur expressions mathématiques en ligne

Publié : dim. 19/févr./2017 10:32
par zaphod_b
Un petit code de débutant pour évaluer des expressions mathématiques qui utilise le moteur sympygamma en python.
Nécessite l'accès au réseau.
C'est pas transcendant, c'est super simple.
J'ai testé avec : +,-,/,^(puissance), parenthèses, sin, cos,....

Code : Tout sélectionner

;
;PB 5.51 x64/x86 - windows 10 pro - x64
;évaluateur expressions mathématiques en ligne
;utilise le moteur en python sympygamma
;by zaphod - 02/2017
;
EnableExplicit
;{- Enumerations / DataSections
;{ Windows
Enumeration
  #Window_0
EndEnumeration
;}
;{ Gadgets
Enumeration
  #Frame3D_0
  #Frame3D_1
  #text_0
  #String_1
  #String_2
  #Button_3
EndEnumeration
;}
Enumeration
  #evnt
EndEnumeration

;}
If InitNetwork()=0
  MessageRequester("Erreur","Pas de réseau")
  End
EndIf
;
Procedure findtext(s.s)
  Protected p,r.s,q.s,i
  q="<script type="+#DQUOTE$+"math/tex; mode=display"+#DQUOTE$+">"
  ;
  If FindString(s,"Can't handle the input") Or FindString(s,"Invalid input")
    SetGadgetText(#String_2,"ERREUR")
  ElseIf FindString(s,"data-approximation=")
    p=FindString(s,"data-approximation=")+20
    i=0
    Repeat
      r=Mid(s,p+i,1)
      i+1
    Until r=#DQUOTE$
    r=Mid(s,p,i-1)
    SetGadgetText(#String_2,r)
  Else
    p=FindString(s,q)
    p+Len(q)
    i=0
    If p
      Repeat
        r=Mid(s,p+i,1)
        i+1
      Until r="<"
      r=Mid(s,p,i-1)
      SetGadgetText(#String_2,r)
    EndIf
  EndIf
EndProcedure

Procedure.s getweb(f.s)
  Protected b.s,*buffer,taille
  ;
  f=ReplaceString(f,"+","%2B")
  f=ReplaceString(f,"/","%2F") 
  f=ReplaceString(f,"^","**") 
  ;
  *Buffer = ReceiveHTTPMemory("http://www.sympygamma.com/input/?i="+f)
  If *Buffer
    Taille = MemorySize(*Buffer)
    b=PeekS(*Buffer, Taille, #PB_UTF8)
    findtext(b)
    FreeMemory(*Buffer)
  Else
    MessageRequester("Erreur","Pas de réception...")
  EndIf
;
EndProcedure

Procedure enter()
  Protected formu.s
  formu=GetGadgetText(#String_1)
          If formu<>""
            getweb(formu)
          EndIf 
EndProcedure


Procedure OpenWindow_Window_0()
  If OpenWindow(#Window_0, 389, 167, 380, 210,"Evaluateur de formule",#PB_Window_SystemMenu|#PB_Window_MinimizeGadget|#PB_Window_TitleBar|#PB_Window_ScreenCentered)
      FrameGadget(#Frame3D_0, 10, 10, 360, 65, "Formule :")
      StringGadget(#String_1, 20, 35, 305, 25, "")
      FrameGadget(#Frame3D_1, 10, 90, 360, 65, "Résultat :")
      StringGadget(#String_2, 20, 110, 340, 25,"")
      ButtonGadget(#Button_3, 330, 34, 30, 27, "=")
      TextGadget(#text_0,15,180,250,25,"Utilise SYMPYGAMMA : www.sympygamma.com")
      ;
      AddKeyboardShortcut(#Window_0,#PB_Shortcut_Return,#evnt)
  EndIf
EndProcedure

OpenWindow_Window_0()

;{- Event loop
Repeat
  Select WaitWindowEvent()
   ; ///////////////////
    Case #PB_Event_Gadget
      If EventGadget()=#Button_3
          enter()
      EndIf
      ; ////////////////////////
    Case #PB_Event_Menu
      If EventMenu()=#evnt
      enter()  
      EndIf
      
    Case #PB_Event_CloseWindow
      If EventWindow()= #Window_0
          CloseWindow(#Window_0)
          Break
      EndIf
  EndSelect
ForEver
;
;}


Re: Evaluateur expressions mathématiques en ligne

Publié : dim. 19/févr./2017 12:39
par falsam
C'est simple avec un code structuré compréhensible à la première lecture. J'aime.
Merci zaphod_b ^^