Task

Write a function/procedure/subroutine that is called with an integer value and returns the middle three digits of the integer if possible or a clear indication of an error if this is not possible.

Note: The order of the middle digits should be preserved.

Your function should be tested with the following values; the first line should return valid answers, those of the second line should return clear indications of an error:

123, 12345, 1234567, 987654321, 10001, -10001, -123, -100, 100, -12345
1, 2, -1, -10, 2002, -2002, 0

Show your output on this page.




 
PROGRAM MIDDLE
 
!$DOUBLE
 
FUNCTION LUNG(N)
   LUNG=LEN(STR$(INT(ABS(N))))+1
END FUNCTION
 
FUNCTION NCNT(N)
    NCNT=VAL(MID$(STR$(INT(ABS(N))),(LUNG(N)-1)/2,3))
END FUNCTION
 
FUNCTION EVEN(N)
    EVEN=INT(N/2)=N/2
END FUNCTION
 
PROCEDURE NUMBER_EXAM(N->R$)
    R$=""   LG%=LUNG(N)-2
    IF EVEN(LG%) THEN R$="?EVEN,"  END IF
    IF LG%<3 THEN
             R$=R$+"ONLY"+STR$(LG%)+" DIGIT"
             IF LG%=1 THEN
                 R$=R$+"S"
             END IF
    END IF
    IF RIGHT$(R$,1)="," THEN R$=LEFT$(R$,LEN(R$)-1)   EXIT PROCEDURE END IF
    IF LEFT$(R$,1)="?" THEN EXIT PROCEDURE END IF
    IF R$<>"" THEN R$="?"+R$   EXIT PROCEDURE END IF
    R$=STR$(NCNT(N))
    IF LEFT$(R$,1)=" " THEN R$=MID$(R$,2) END IF
    IF LEN(R$)=1 THEN R$="00"+R$  END IF
    IF LEN(R$)=2 THEN R$="0"+R$   END IF
END PROCEDURE
 
BEGIN
    DATA(123,12345,1234567,987654321,10001,-10001,-123,-100,100,-12345)
    DATA(1,2,-1,-10,2002,-2002,0)
    FOR I%=1 TO 17 DO
        READ(N)
        PRINT(N;"  ",)
        NUMBER_EXAM(N->R$)
        PRINT(R$)
    END FOR
END PROGRAM
 