Calculatrice en ligne de commande

Partagez votre expérience de PureBasic avec les autres utilisateurs.
Avatar de l’utilisateur
falsam
Messages : 7244
Inscription : dim. 22/août/2010 15:24
Localisation : IDF (Yvelines)
Contact :

Calculatrice en ligne de commande

Message par falsam »

Juste pour le plaisir de coder, je vous propose une calculatrice en ligne de commande.

Exemples.
(10+2+5+7+8)/5
min(4,5,1,6,2)
pi*pow(2,2) (Surface d'un cercle de 2 cm de rayon)
4*pi*pow(2,2) (Surface d'une sphère de 2 cm de rayon)

Les fonctions actuelles.
pi, cos, acos, sin, asin, tan, atan, exp, log, max, min, pow, round

:idea: L'astuce consiste à utiliser JavaScript pour le retour du résultat. Vous pouvez ajouter facilement d'autres fonctionnalités.

Code : Tout sélectionner

EnableExplicit

Enumeration fonts
  #fontGlobal  
EndEnumeration

Enumeration window
  #mf
EndEnumeration

Enumeration gadget
  #web
  #source
  #send
EndEnumeration

Enumeration shortcuts
  #enter  
  #escape
EndEnumeration

Global HTML.s, myBrowser.IWebBrowser2

; Summary
Declare send()
Declare setUrl(source.s)
Declare exit()

; Fonts
LoadFont(#fontGlobal, "", 14)
SetGadgetFont(#PB_Default, FontID(#fontGlobal))

; Form
OpenWindow(#mf, 0, 0, 500, 120, "Calculatrice ligne de commande", #PB_Window_SystemMenu | #PB_Window_ScreenCentered)

; View Result
WebGadget (#web, 0, 0, 500, 80, "")
SetGadgetAttribute(#web, #PB_Web_BlockPopups, #True)  
SetGadgetAttribute(#web, #PB_Web_BlockPopupMenu, #True)
  
myBrowser = GetWindowLong_(GadgetID(#web), #GWL_USERDATA)
myBrowser\put_Silent(#True) ;Suppress error warnings in WebGadget
myBrowser\put_Left(0) 

; Enter user data & Send (Button Send or Enter)
StringGadget(#source, 5, 82, 405, 28, "0")
SetGadgetColor(#source, #PB_Gadget_BackColor, RGB(230, 230, 230))

ButtonGadget(#send, 415, 82, 80, 28, "Send")
GadgetToolTip(#send, "Enter")

SetActiveGadget(#source)

; Triggers
BindGadgetEvent(#send, @send())
AddKeyboardShortcut(#mf, #PB_Shortcut_Return, #enter)
BindEvent(#PB_Event_Menu, @send(), #mf, #enter)

BindEvent(#PB_Event_CloseWindow, @exit())
AddKeyboardShortcut(#mf, #PB_Shortcut_Escape, #escape)
BindEvent(#PB_Event_Menu, @exit(), #mf, #escape)

; Init WebGadget
Send()

; Loop
Repeat : Until WaitWindowEvent() = #PB_Event_CloseWindow

Procedure send()
  Protected buffer.s = GetGadgetText(#source)
  Protected source.s
  
  ; A small preparation : We use the Javascript Math library
  source = ReplaceString(buffer, "pi", "Math.PI", #PB_String_NoCase) : buffer = source
  source = ReplaceString(buffer, "cos", "Math.cos", #PB_String_NoCase) : buffer = source
  source = ReplaceString(buffer, "acos", "Math.acos", #PB_String_NoCase) : buffer = source  
  source = ReplaceString(buffer, "sin", "Math.sin", #PB_String_NoCase) : buffer = source
  source = ReplaceString(buffer, "asin", "Math.asin", #PB_String_NoCase) : buffer = source
  source = ReplaceString(buffer, "tan", "Math.tan", #PB_String_NoCase) : buffer = source
  source = ReplaceString(buffer, "atan", "Math.atan", #PB_String_NoCase) : buffer = source
  source = ReplaceString(buffer, "exp", "Math.exp", #PB_String_NoCase) : buffer = source  
  source = ReplaceString(buffer, "log", "Math.log", #PB_String_NoCase) : buffer = source  
  source = ReplaceString(buffer, "max", "Math.max", #PB_String_NoCase) : buffer = source
  source = ReplaceString(buffer, "min", "Math.min", #PB_String_NoCase) : buffer = source   
  source = ReplaceString(buffer, "pow", "Math.pow", #PB_String_NoCase) : buffer = source  
  source = ReplaceString(buffer, "round", "Math.round", #PB_String_NoCase) : buffer = source
      
  setUrl(source)
  SetActiveGadget(#source)
EndProcedure

; Send & Exec source
Procedure setUrl(source.s)  
  ; HTML
  HTML = "<meta http-equiv='X-UA-Compatible' content='IE=edge' />" 
  HTML + "<body bgcolor='#E6E6E6' scroll=no>"
  
  ; result
  HTML + "<div id='result' style='color:#000000; font-size:30px'>Oops Error !!</div>"
  
  HTML + "</body>"
  
  ; JavaScript
  HTML + "<script>" ;+ #CRLF$
  
  HTML + "var buffer = " + source + ";"
  
  ; Result Area
  HTML + "document.getElementById('result').innerHTML=buffer;"
  
  HTML + "</script>"
    
  SetGadgetItemText(#web, #PB_Web_HtmlCode , HTML)
EndProcedure

Procedure exit()
  End  
EndProcedure
- Touche Entrée pour voir le résultat.
- Touche Escape pour quitter l'utilitaire.

Enjoy ;)
Configuration : Windows 11 Famille 64-bit - PB 6.03 x64 - AMD Ryzen 7 - 16 GO RAM
Vidéo NVIDIA GeForce GTX 1650 Ti - Résolution 1920x1080 - Mise à l'échelle 125%
Avatar de l’utilisateur
falsam
Messages : 7244
Inscription : dim. 22/août/2010 15:24
Localisation : IDF (Yvelines)
Contact :

Re: Calculatrice en ligne de commande

Message par falsam »

J'ai remplacé le StringGadget() par un ScintillaGadget() et ajouté l'autocomplétion des fonctions mathématiques utilisables.

Code : Tout sélectionner

EnableExplicit

Enumeration fonts
  #fontGlobal  
EndEnumeration

Enumeration window
  #mf
EndEnumeration

Enumeration gadget
  #web
  #source
  #send
EndEnumeration

Enumeration shortcuts
  #enter  
  #escape
EndEnumeration

Global HTML.s, myBrowser.IWebBrowser2

; keywords
Global KeyWord.s = "pi|cos|acos|sin|asin|tan|atan|exp|log|max|min|pow|round"
Global KeyWordSep.s = "|"

; Summary
Declare send()
Declare setUrl(source.s)

Declare ScintillaCallBack(Gadget, *scinotify.SCNotification)
Declare ScintillaProperties(Gadget)

Declare exit()

; Fonts
LoadFont(#fontGlobal, "", 14)
SetGadgetFont(#PB_Default, FontID(#fontGlobal))

; Form
OpenWindow(#mf, 0, 0, 500, 120, "Calculatrice ligne de commande", #PB_Window_SystemMenu | #PB_Window_ScreenCentered)

; View Result
WebGadget (#web, 0, 0, 500, 50, "")
SetGadgetAttribute(#web, #PB_Web_BlockPopups, #True)  
SetGadgetAttribute(#web, #PB_Web_BlockPopupMenu, #True)

myBrowser = GetWindowLong_(GadgetID(#web), #GWL_USERDATA)
myBrowser\put_Silent(#True) ;Suppress error warnings in WebGadget
myBrowser\put_Left(0) 

; Enter user data & Send (Button Send or Enter)
If InitScintilla()
  ScintillaGadget(#source, 5, 60, 405, 50, @ScintillaCallBack())
  ScintillaProperties(#source) 
  ScintillaSendMessage(#source, #SCI_SETTEXT, 0, UTF8("0"))
  
  ; Disable the TAB key and special characters
  RemoveKeyboardShortcut(#mf, #PB_Shortcut_Tab)
  
  AddKeyboardShortcut(#mf, #PB_Shortcut_Control+#PB_Shortcut_B, 0)
  AddKeyboardShortcut(#mf, #PB_Shortcut_Control+#PB_Shortcut_G, 0)
  AddKeyboardShortcut(#mf, #PB_Shortcut_Control+#PB_Shortcut_E, 0)
  AddKeyboardShortcut(#mf, #PB_Shortcut_Control+#PB_Shortcut_R, 0)
  AddKeyboardShortcut(#mf, #PB_Shortcut_Control+#PB_Shortcut_O, 0)
  AddKeyboardShortcut(#mf, #PB_Shortcut_Control+#PB_Shortcut_P, 0)
  AddKeyboardShortcut(#mf, #PB_Shortcut_Control+#PB_Shortcut_Q, 0)
  AddKeyboardShortcut(#mf, #PB_Shortcut_Control+#PB_Shortcut_S, 0)
  AddKeyboardShortcut(#mf, #PB_Shortcut_Control+#PB_Shortcut_F, 0)
  AddKeyboardShortcut(#mf, #PB_Shortcut_Control+#PB_Shortcut_H, 0)
  AddKeyboardShortcut(#mf, #PB_Shortcut_Control+#PB_Shortcut_K, 0)
  AddKeyboardShortcut(#mf, #PB_Shortcut_Control+#PB_Shortcut_W, 0)  
  AddKeyboardShortcut(#mf, #PB_Shortcut_Control+#PB_Shortcut_N, 0)  
EndIf

ButtonGadget(#send, 415, 60, 80, 50, "Send")
GadgetToolTip(#send, "Enter")


; Triggers
BindGadgetEvent(#send, @send())
AddKeyboardShortcut(#mf, #PB_Shortcut_Return, #enter)
BindEvent(#PB_Event_Menu, @send(), #mf, #enter)

BindEvent(#PB_Event_CloseWindow, @exit())
AddKeyboardShortcut(#mf, #PB_Shortcut_Escape, #escape)
BindEvent(#PB_Event_Menu, @exit(), #mf, #escape)

; Init WebGadget
Send()

; Loop
Repeat : Until WaitWindowEvent() = #PB_Event_CloseWindow

Procedure send()
  Protected buffer.s = GetGadgetText(#source)
  Protected source.s
  
  ; A small preparation : We use the Javascript Math library
  source = ReplaceString(buffer, "pi", "Math.PI", #PB_String_NoCase) : buffer = source
  source = ReplaceString(buffer, "cos", "Math.cos", #PB_String_NoCase) : buffer = source
  source = ReplaceString(buffer, "acos", "Math.acos", #PB_String_NoCase) : buffer = source  
  source = ReplaceString(buffer, "sin", "Math.sin", #PB_String_NoCase) : buffer = source
  source = ReplaceString(buffer, "asin", "Math.asin", #PB_String_NoCase) : buffer = source
  source = ReplaceString(buffer, "tan", "Math.tan", #PB_String_NoCase) : buffer = source
  source = ReplaceString(buffer, "atan", "Math.atan", #PB_String_NoCase) : buffer = source
  source = ReplaceString(buffer, "exp", "Math.exp", #PB_String_NoCase) : buffer = source  
  source = ReplaceString(buffer, "log", "Math.log", #PB_String_NoCase) : buffer = source  
  source = ReplaceString(buffer, "max", "Math.max", #PB_String_NoCase) : buffer = source
  source = ReplaceString(buffer, "min", "Math.min", #PB_String_NoCase) : buffer = source   
  source = ReplaceString(buffer, "pow", "Math.pow", #PB_String_NoCase) : buffer = source  
  source = ReplaceString(buffer, "round", "Math.round", #PB_String_NoCase) : buffer = source
  
  setUrl(source)
  SetActiveGadget(#source)
EndProcedure

; Send & Exec source
Procedure setUrl(source.s)  
  ; HTML
  HTML = "<meta http-equiv='X-UA-Compatible' content='IE=edge' />" 
  HTML + "<body bgcolor='#E6E6E6' scroll=no>"
  
  ; result
  HTML + "<div id='result' style='color:#000000; font-size:30px'>Oops Error !!</div>"
  
  HTML + "</body>"
  
  ; JavaScript
  HTML + "<script>" ;+ #CRLF$
  
  HTML + "var buffer = " + source + ";"
  
  ; Result Area
  HTML + "document.getElementById('result').innerHTML=buffer;"
  
  HTML + "</script>"
  
  SetGadgetItemText(#web, #PB_Web_HtmlCode , HTML)
EndProcedure

;-
;- U.T. Utility
Procedure MakeUTF8Text(Text.s)
  Static Buffer.s
  Buffer = Space(StringByteLength(Text, #PB_UTF8) + 1)
  PokeS(@buffer, text, -1, #PB_UTF8)
  ProcedureReturn @buffer
EndProcedure

;-
;- U.T. Scintilla
Procedure ScintillaCallBack(Gadget, *scinotify.SCNotification)
  Protected SciCurrentPos, SciWordStartPos
  
  Select *scinotify\nmhdr\code		  
    Case #SCN_CHARADDED      
      ;- Autocompletion
      Select *scinotify\ch
        Case 'a' To 'z'   
          SciCurrentPos = ScintillaSendMessage(Gadget, #SCI_GETCURRENTPOS)
          SciWordStartPos = ScintillaSendMessage(Gadget, #SCI_WORDSTARTPOSITION, SciCurrentPos, 1)
          ScintillaSendMessage(Gadget, #SCI_AUTOCSHOW, SciCurrentPos - SciWordStartPos, MakeUTF8Text(KeyWord))   
      EndSelect
  EndSelect  
EndProcedure

; Setup gadget Scintilla
Procedure ScintillaProperties(Gadget)
  ; Setup Style
  ScintillaSendMessage(Gadget, #SCI_STYLESETFORE, #STYLE_DEFAULT, RGB(0, 0, 0))    
  ScintillaSendMessage(Gadget, #SCI_STYLESETBACK, #STYLE_DEFAULT, RGB(220, 220, 220))
  ScintillaSendMessage(Gadget, #SCI_STYLESETFONT,#STYLE_DEFAULT, @"Lucida Console") 
  ScintillaSendMessage(Gadget, #SCI_STYLESETSIZE, #STYLE_DEFAULT, 15)
  ScintillaSendMessage(Gadget, #SCI_STYLECLEARALL)  
  
  ; Hide margins
  ScintillaSendMessage(Gadget, #SCI_SETMARGINWIDTHN, 0, 0)  
  ScintillaSendMessage(Gadget, #SCI_SETMARGINWIDTHN, 1, 0)     
  
  ; Setup autocompletion 
  ScintillaSendMessage(Gadget, #SCI_AUTOCSETMAXHEIGHT, 40)
  ScintillaSendMessage(Gadget, #SCI_AUTOCSETMAXWIDTH, 350)
  ScintillaSendMessage(Gadget, #SCI_AUTOCSETAUTOHIDE, #True)
  ScintillaSendMessage(Gadget, #SCI_AUTOCSETCHOOSESINGLE, #True)
  ScintillaSendMessage(Gadget, #SCI_AUTOCSETIGNORECASE, #True)
  
  ; Character separating each word from the list of keywords
  ScintillaSendMessage(Gadget, #SCI_AUTOCSETSEPARATOR, Asc(KeyWordSep)) 
  
  ; Character selecting the word from the autocompletion list : Space (or tab)
  ScintillaSendMessage(Gadget, #SCI_AUTOCSETFILLUPS, 0, @" ")
  
  ; Sort the autocompletion list
  ScintillaSendMessage(Gadget, #SCI_AUTOCSETORDER, #SC_ORDER_PERFORMSORT) 
EndProcedure

Procedure exit()
  End  
EndProcedure
Configuration : Windows 11 Famille 64-bit - PB 6.03 x64 - AMD Ryzen 7 - 16 GO RAM
Vidéo NVIDIA GeForce GTX 1650 Ti - Résolution 1920x1080 - Mise à l'échelle 125%
Avatar de l’utilisateur
SPH
Messages : 4726
Inscription : mer. 09/nov./2005 9:53

Re: Calculatrice en ligne de commande

Message par SPH »

pow(2,2048) = Infinity
C'est pour cela que je ne calcule rien avec PB... 8)
http://HexaScrabble.com/
!i!i!i!i!i!i!i!i!i!
!i!i!i!i!i!i!
!i!i!i!
//// Informations ////
Intel Core i7 4770 64 bits - GTX 650 Ti
Version de PB : 6.00 - 64 bits
Avatar de l’utilisateur
Kwai chang caine
Messages : 6962
Inscription : sam. 23/sept./2006 18:32
Localisation : Isere

Re: Calculatrice en ligne de commande

Message par Kwai chang caine »

Merci pour le partage 8)
ImageLe bonheur est une route...
Pas une destination

PureBasic Forum Officiel - Site PureBasic
Avatar de l’utilisateur
Zorro
Messages : 2185
Inscription : mar. 31/mai/2016 9:06

Re: Calculatrice en ligne de commande

Message par Zorro »

ouaip , c'est pratique le javascript :mrgreen:

bien vu pour l'astuce ....
la meme en python ? :D
Image
Image
Site: http://michel.dobro.free.fr/
Devise :"dis moi ce dont tu as besoin, je t'expliquerai comment t'en passer"
Avatar de l’utilisateur
falsam
Messages : 7244
Inscription : dim. 22/août/2010 15:24
Localisation : IDF (Yvelines)
Contact :

Re: Calculatrice en ligne de commande

Message par falsam »

Zorro a écrit :la meme en python ?
Python n'étant pas interprété par le navigateur, je ne pourrais répondre à ta demande. Et c'est tant mieux parce que je n'aime pas Python :mrgreen:
Configuration : Windows 11 Famille 64-bit - PB 6.03 x64 - AMD Ryzen 7 - 16 GO RAM
Vidéo NVIDIA GeForce GTX 1650 Ti - Résolution 1920x1080 - Mise à l'échelle 125%
Avatar de l’utilisateur
Kwai chang caine
Messages : 6962
Inscription : sam. 23/sept./2006 18:32
Localisation : Isere

Re: Calculatrice en ligne de commande

Message par Kwai chang caine »

je n'aime pas Python
On devrait faire un club :mrgreen:
En plus un langage script qui veut jouer dans la cour des vrais en faisant croire qu'il est "EXEptionnel" :lol:
ImageLe bonheur est une route...
Pas une destination

PureBasic Forum Officiel - Site PureBasic
Avatar de l’utilisateur
Micoute
Messages : 2522
Inscription : dim. 02/oct./2011 16:17
Localisation : 35520 La Mézière

Re: Calculatrice en ligne de commande

Message par Micoute »

Merci pour ce partage très utile et intéressant.
Microsoft Windows 10 Famille 64 bits : Carte mère : ASRock 970 Extreme3 R2.0 : Carte Graphique NVIDIA GeForce RTX 3080 : Processeur AMD FX 6300 6 cœurs 12 threads 3,50 GHz PB 5.73 PB 6.00 LTS (x64)
Un homme doit être poli, mais il doit aussi être libre !
Répondre