domingo, 16 de junio de 2019

Calculadora en VFP

Calculadora

Mucha veces queremos implementar una calculadora en nuestros sistemas pero factor tiempo no nos proponemos a realizar el programa para realizar ciertas operaciones matemáticas. 

Hoy les vamos a presentar la forma de realizar una calculadora sencilla pero que reúne lo básico como para ejecutar operaciones como lo son la suma(+), la resta(-),la división(/),la multiplicación.

Además cuentas con la operación para sacar la raíz cuadrada de X número el cual desees consultar,y también cuanta la función aritmética para sacar porcentajes.

Además cuentas con el signo menos(-) para realizar cálculos negativos.



En caso de realizar una operación errónea cuentas con las teclas AC y C que te permitirán  limpiar los resultados. 

Este programa lo puedes adaptar en tu software que te estés desarrollando y llamarlo mediante una función el cual le hayas asignado ya bien sea F1,F2,F3, etc.

Realmente realizar este tipo de programas no resulta muy complicado de elaborar y hoy te vamos a enseñar como elaborar un sin muchas complicaciones.

Al presionar sobre cada una de las respectivas teclas las cuales hayas tecleado el programa se dirigirá al Keypress a realizar la operación respectiva.

Ejemplo : keyboard "7" clear

En la parte inicial de tu formulario en el Init publicaremos las variables que utilizaremos en el programa.

PUBLIC xcaltot,xcalpun,xcalope,xcalfun,xcala,xcalb,xcaligu,xcalerr,xcalsig

A continuación en el evento Keypress del formulario coloca los siguiente :


LPARAMETERS nKeyCode, nShiftAltCtrl


if nKeyCode=27
  thisform.release
endif

if nKeyCode=13
  thisform.container1.command4.setfocus
endif

if inlist(upper(chr(nKeyCode)),"Z","C","A","G") or inlist(chr(nKeyCode),"0","1","2","3","4","5","6","7","8","9",".") or nKeyCode=127 or nKeyCode=13 or inlist(chr(nKeyCode),"+","-","/","*","=","%")
   do case
      case nKeyCode=127
           xcaltip=1
      case chr(nKeyCode)>="0" and chr(nKeyCode)<="9" or chr(nKeyCode)="."
           xcaltip=2    
      case inlist(chr(nKeyCode),"+","-","/","*","%")
           xcaltip=3    
      case nKeyCode=13 or chr(nKeyCode)="="
           xcaltip=4    
      case upper(chr(nKeyCode))="Z"
           xcaltip=5    
      case upper(chr(nKeyCode))="C"
           xcaltip=6    
      case upper(chr(nKeyCode))="A"
           xcaltip=7    
      case upper(chr(nKeyCode))="G"
           xcaltip=8    
   endcase

   if xcaltip=7 
      xcaltot="0."
      xcalpun=0
      xcalope=""
      xcalfun=0
      xcala=0
      xcalb=0
      xcaligu=0
      xcalerr=0
      xcalsig=0
      thisform.refresh
   endif

   if xcaltip=6 
      xcaltot="0."
      xcalpun=0
      xcalerr=0
      thisform.refresh
   endif

   if xcaltip=5 
      if val(xcaltot)>=0
         xcaltot=allt(str(sqrt(val(xcaltot)),23,11))
      endif   
      thisform.formato
   endif

   if xcaltip=8 
      xcaltot=allt(str((-1)*(val(xcaltot)),23,11))
      thisform.formato
   endif
   
   if xcaltip=4 
      xcaligu=1 
      if xcalfun=1
         do case
            case xcalope="+"
                 xcalb=xcala+val(xcaltot)
            case xcalope="-"
                 xcalb=xcala-val(xcaltot)
            case xcalope="/"
                 if val(xcaltot)<>0 
                    xcalb=xcala/val(xcaltot)
                 else
                    xcalerr=1
                 endif   
            case xcalope="*"
                 xcalb=xcala*val(xcaltot)
            case xcalope="%"
                 xcalb=xcala*(val(xcaltot)/100)
         endcase        
         xcalerr=1
         xcalfun=0
         xcalope=""
         xcaltot=allt(str(xcalb,23,11))
         thisform.formato
      endif
   endif
   
   if xcaltip=3 
    if xcalsig=3
      xcalope=chr(nKeyCode)
    else 
      xcalsig=3
      if xcaligu=1
         xcaligu=0
      endif   
      if xcalfun=0
         xcalfun=1
         xcalope=chr(nKeyCode)
         xcala=val(xcaltot)
      else   
         do case
            case xcalope="+"
                 xcalb=xcala+val(xcaltot)
            case xcalope="-"
                 xcalb=xcala-val(xcaltot)
            case xcalope="/"
                 xcalb=xcala/val(xcaltot)
            case xcalope="*"
                 xcalb=xcala*val(xcaltot)
            case xcalope="%"
                 xcalb=xcala*(val(xcaltot)/100)
         endcase        
         xcalope=chr(nKeyCode)
         xcaltot=allt(str(xcalb,23,11))
         thisform.formato
      endif   
      xcaltot="0."
      xcalpun=0
    endif  
   endif

   if xcaltip=2   
      xcalsig=2
      if xcaligu=1
         xcaltot="0."
         xcalpun=0
         xcalope=""
         xcalfun=0
         xcala=0
         xcalb=0
         xcaligu=0
         xcalerr=0
      endif
      xcalch=chr(nKeyCode)
      if len(xcaltot)<13
         if xcalpun=0 and xcalch="."
            xcalpun=1
         endif   
         if xcalch>="0" and xcalch<="9"
            if xcaltot="0." and xcalpun=0
               xcaltot=""
            endif   
            if xcalpun=0
               xcaltot=substr(xcaltot,1,len(xcaltot)-1)+xcalch+"."     
               thisform.refresh
            else
               xcaltot=xcaltot+xcalch
               thisform.refresh
            endif   
         endif   
      endif
   endif   

   if xcaltip=1   
      if len(xcaltot)>1
      
         if substr(xcaltot,len(xcaltot),1)<>"."
            xcalpun=1
            xcaltot=substr(xcaltot,1,len(xcaltot)-1)
            thisform.refresh
         else   
            xcalpun=0
            xcaltot=substr(xcaltot,1,len(xcaltot)-2)+"."
            thisform.refresh
         endif
         if xcaltot="." or xcaltot="-."
            xcaltot="0."
            thisform.refresh
         endif
      endif     
   endif   
      
endif

Pega en el Evento formato las siguientes instrucciones :

xcaltot=substr(xcaltot,1,12)
xcalpun=0
for x=1 to len(xcaltot)
  if substr(xcaltot,x,1)="."
    xcalpun=1
  endif
endfor
if xcalpun=1
  do while xcalpun=1 and substr(xcaltot,len(xcaltot),1)<>"." and substr(xcaltot,len(xcaltot),1)="0"
    xcaltot=substr(xcaltot,1,len(xcaltot)-1) 
  enddo
else
  xcaltot=xcaltot+"."
endif
xcala=val(xcaltot)
thisform.refresh

Evento Activate

xcaltot="0."
xcalpun=0
xcalope=""
xcalfun=0
xcala=0
xcalb=0
xcaligu=0
xcalerr=0
xcalsig=0
thisform.refresh

Espero te resulte de gran utilidad.






0 comentarios:

Publicar un comentario