Calculator Library

Version 5.0.0

Programmer Example

There are several prerequisites for using the JUCALC procedure and the related family of calculator routines.
  1. Any program that calls JUCALC directly or indirectly (via JUCALX(), STRGAR2(), INUM0(), RNUM0(), SNUM0()) must supply two routines:
  2. Nearly all the routines in libCLI.a use JUN to display messages. You may wish to override this routine, as is done in this example.
The following program shows a simple but complete line-mode calculator program.

The administrator of libCLI.a should have the scripts html2F and ccall in his search path. To build and execute this program enter the directly where this document resides and enter:

   html2F Example.html >X.f # extract source from document
   ccall X.f  # compile and load example program
   X        # run example program
   a=10
   a/2
   3**4
   sin(3.1416/4)
   PI=2*asin(1)
   diameter=20.3+8/4
   circumference=PI*diameter
   funcs
   dump
   # use end-of-file (typically control-D) to exit program
   ctrl-D

program calcx ! ! line mode calculator that calls jucalc ! use m_calculator, only: jucalc,iclen_calc implicit real*8 (a-h, o-z) character*(iclen_calc) event, line character*(iclen_calc) outlin 100 continue read(*,'(a)',end=999)line call jucalc(line,outlin,event,rvalue,ierr) ! several different meaning to the error flag returned by the calculator if(ierr.eq.0)then ! a numeric value was returned without error write(6,'(a,a,a)')outlin,' = ',line else if(ierr.eq.2)then ! a string value was returned without error write(6,'(a)')event else if(ierr.eq.1)then ! a request for a message has been returned (from DUMP or FUNC) write(6,'(a,a)')'message===>',event else if(ierr.eq.-1)then ! an error has occurred write(6,'(a,a)')'error===>',event else ! this should not occur write(6,'(a)')'warning===> unexpected ierr value from jucalc' endif go to 100 999 continue end program calcx !#cccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccc subroutine juown1(func,iflen,args,iargstp,n,x,y,fval,ctmp,ier) ! extend functions available to the calculator routine ! ! if the function ownmode(1) is called this subroutine ! will be accessed to do user-written functions. ! ! func(iend-1)=procedure name. func should not be changed. ! iflen=length of procedure name. ! args=array of 100 elements containing procedure arguments. ! iargstp=type of argument(1=value,2=position of string value) ! n=integer number of parameters ! x=array of 55555 x values ! y=array of 55555 y values ! fval=value to replace function call ! ctmp=string to return when returning a string value ! ier=returned error flag value. ! set to -1 if an error occurs. ! set to 0 if a number is returned ! set to 2 if a string is returned ! use m_calculator, only: x,y,values,valuer,iclen_calc ! values: the values of string variables ! valuer: the lengths of the string variable values character*(*) func character*(*) ctmp character*(iclen_calc) temp1 integer iflen ,n, ier, iargstp(100) real*8 args(100) real*8 fval character*(iclen_calc) lowers external lowers fval=0 !----------------------------------------------------------------------- write(*,*)'*juown1* unknown function ', func(1:iflen) write(*,*)'function name length is..',iflen write(*,*)'number of arguments .....',n do 10 i10=1,n if(iargstp(i10).eq.0)then write(*,*)i10,' VALUE=',args(i10) elseif(iargstp(i10).eq.2)then iwhich=args(i10) ilen=valuer(iwhich) write(*,*)i10,' STRING='//values(iwhich)(:ilen) else write(*,*)'unknown parameter type is ',iargstp(i10) endif 10 continue return end subroutine juown1 !ccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccc real function c(fval,n) ! a built-in calculator function called c must be satisfied. ! write whatever you want here as a function real*8 fval c=0.0 end function c !ccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccc subroutine jun(idum,string) ! the calculator displays internal error messages using a routine ! called jun. This creates a dummy that simply prints the messages. character*(*) string write(*,*)string end subroutine jun !ccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccc
See the LIBRARY homepage for related information.