====Getnumber (VMS BASIC)====
Unitconversion is a test script I make with VMS BASIC. Don't expect it to be perfect.
It has two modules: UNITCONVERSION.BAS is the main module, and GETNUMBER.BAS tests the user provided input.
! **************************************
! *GETNUMBER - ASKS FOR A NUMBER *
! * *
! * Checks for validity and bounds of *
! * number. *
! **************************************
FUNCTION REAL GETNUMBER(REAL MN,REAL MX)
DECLARE REAL R, &
INTEGER REPEAT
REPEAT=1
WHILE REPEAT
! An error will be issued if
! R is not a number
WHEN ERROR USE input_handler
INPUT R
END WHEN
! Check boundaries
IF (RMX) THEN
PRINT "TOO BIG"
ELSE
REPEAT=0
END IF
END IF
NEXT
! No error, Return R
GETNUMBER=R
! Error handler
! Most probable errors are
! 50 Wrong data
! and
! 20? (not sure) End of file.
HANDLER input_handler
SELECT ERR
CASE=50
RETRY
PRINT "WRONG DATA TYPE."
CASE 20
PRINT "END OF FILE"
CONTINUE
END SELECT
END HANDLER
END FUNCTION
!****************************************************
!* THIS IS A TEST PROGRAM I MADE FOR VMS BASIC *
!****************************************************
PROGRAM UNITCONVERSION
EXTERNAL REAL FUNCTION GETNUMBER(REAL, REAL)
DECLARE INTEGER I,F,M ! For-Next, while variables
DECLARE INTEGER PMAG, ! Magnitude pointer &
PUNIT, ! Unit pointer &
REQMAG, ! Magnitude requested &
PRESP, ! Valid Response matrix point &
REQFROM, ! FROM unit &
REQTO ! TO unit
DECLARE INTEGER INTERACTIVE ! User ask to repeat
DECLARE REAL VALUFROM, ! FROM valuese matrix &
VALUTO ! TO value
!***********************************
!* magnitude data
!***********************************
! magnitude code, magnitude name
DATA 1,"TEMPERATURE"
DATA 2,"LONGITUDE"
DATA 3,"MASS"
DATA -1,"EXIT"
!***********************************
!* Unit data
!***********************************
! unit magnitude, factor, delta, symbol, name
DATA 1,100,273.15,"K","kelvin"
DATA 1,100,0,"C","celsius"
DATA 1,180,32,"F","fahrenheit"
DATA 2,0.001,"mm","milimeter"
DATA 2,0.01,"cm","centimeter"
DATA 2,0.0254,"in","inch"
DATA 2,0.1,"dm","decimeter"
DATA 2,0.3048,"ft","foot"
DATA 2,0.9144,"yd","yard"
DATA 2,1,"m","meter"
DATA 2,10,"dam","decameter"
DATA 2,100,"hm","decameter"
DATA 2,100,"hm","hectometer"
DATA 2,1000,"km","kilometer"
DATA 2,1609.3,"mile","mile"
DATA 2,1853,"n.mile","nautical mile"
DATA -1,-1,"EXIT","EXIT"
DIM MAG$ (5)
DIM MAGCODE%(5)
DIM UNITMAG%(20)
DIM REAL UNITFACTOR(20)
DIM REAL UNITDELTA(20)
DIM UNITSYMBOL$(20)
DIM UNITNAME$(20)
PMAG=0
PUNIT=0
M=0
WHILE M<>-1
READ M
MAGCODE%(PMAG)=M
READ MAG$(PMAG)
PMAG=PMAG+1
NEXT
! Reset PMAG to last valid unit
PMAG=PMAG-2
PUNIT=0
M=0
WHILE M<>-1
READ M
UNITMAG%(PUNIT)=M
READ UNITFACTOR(PUNIT)
! Temperature units have deltas
IF UNITMAG%(PUNIT)=1 THEN
READ UNITDELTA(PUNIT)
END IF
READ UNITSYMBOL$(PUNIT)
READ UNITNAME$(PUNIT)
PUNIT=PUNIT+1
NEXT
PUNIT=PUNIT-2 ! Reset PUNIT to last valid unit
! Interactive is reserved for compilation as function.
! It should return -1 to ask repeat, or 0 to ask exit.
INTERACTIVE=0
PRINT "UNIT CONVERSION"
PRINT "WHAT DO YOU WANT TO CONVERT"
! Print a list of magnitudes
PRINT I;"- ";MAG$(I) FOR I=0 TO PMAG
PRINT "-1 TO EXIT"
REQMAG=INT(GETNUMBER(-1,0+PMAG))
IF REQMAG<0 THEN
EXIT PROGRAM
END IF
REQMAG=MAGCODE%(REQMAG) !Now ReqMag is a magnitude code.
! Create a matrix of magnitude units
DIM RESP%(PUNIT)
PRESP=0
! Fill the matrix
FOR F=0 TO PUNIT
IF UNITMAG%(F)=REQMAG THEN
RESP%(PRESP)=F
PRESP=PRESP+1
END IF
NEXT F
PRESP=PRESP-1 ! Reset PRESP to last valid unit.
! Show unit list
PRINT "SELECT UNIT TO CONVERT FROM:"
PRINT F;"- ";UNITNAME$(RESP%(F)) FOR F=0 TO PRESP
PRINT "-1 TO EXIT"
REQFROM=INT(GETNUMBER(-1,0+PRESP))
IF REQFROM<0 THEN
EXIT PROGRAM
END IF
REQFROM=RESP%(REQFROM) !Now ReqFrom is a unit code.
! Show unit list
PRINT "SELECT UNIT TO CONVERT TO:"
PRINT F;"- ";UNITNAME$(RESP%(F)) FOR F=0 TO PRESP
PRINT "-1 TO EXIT"
REQTO=INT(GETNUMBER(-1,0+PRESP))
IF REQTO<0 THEN
EXIT PROGRAM
END IF
REQTO=RESP%(REQTO) !Now ReqTo is a unit code.
PRINT "TRANSLATE ";UNITSYMBOL$(REQFROM);" INTO ";UNITSYMBOL$(REQTO)
PRINT "VALUE TO TRANSLATE: ";
VALUFROM=GETNUMBER(-9e9,9e9)
! Translating Temperature? Deltas should be applied, then.
IF REQMAG=1 THEN
VALUTO=((VALUFROM-UNITDELTA(REQFROM))*UNITFACTOR(REQTO))/UNITFACTOR(REQFROM)+UNITDELTA(REQTO)
ELSE
!These units seem to be reverse defined
VALUTO=(VALUFROM*UNITFACTOR(REQFROM))/UNITFACTOR(REQTO)
END IF
PRINT VALUFROM;UNITSYMBOL$(REQFROM);" = ";VALUTO;UNITSYMBOL$(REQTO)
INTERACTIVE=1
END PROGRAM