This is an exercise from THE ELEMENTS OF PROGRAMMING STYLE by Kernighan and Plauger. The program supposedly counts characters, words and sentences. A slach signals end of data. The exercise is to replace the arithmetic ifs with logical iffs.
I don't think the program works, arithmetic ifs or not. I don't have Fortran compiler but a line-by-line mechanical translation into Pascal seems to go into an infinite loop. Will it work in Fortran?
INTEGER C,KT,BUFR(72),BLANK,COMMA,SCOL, DASH,SLSH,PEROD,NW,NC,NS
REAL AWS,ASW
DATA BLANK,COMMA,SCOL,DASH,SLSH,PEROD/' ',',',';','-','/','.'/,
* NW,NC,NS,KT,C/0,0,0,73,' '/
101 FORMAT(1H1,35X,'INPUT TEXT')
102 FORMAT(72A1)
103 FORMAT(4X,72A1)
104 FORMAT(///,26X,'NUMBER OF SENTENCES=',I8,/,19X,'AVERAGE NUMBER OF
*WORDS/SENTENCE=',F8.2,/20X,'AVERAGE NUMBER OF SYMBOLS/WORD=',F8.2)
WRITE(6,101)
10 READ(5,102) BUFR
WRITE(6,103) BUFR
KT=KT-72
IF(C-PEROD) 20,35,20
20 C=BUFR(KT)
25 IF(C-PEROD) 40,30,40
30 NS=NS+1
NW=NW+1
KT=KT+3
IF(KT-72) 35,35,10
35 C=BUFR(KT)
IF(C-SLSH) 25,75,25
40 IF(C-BLANK) 50,45,50
45 NW=NW+1
GOTO 70
50 IF(C-COMMA) 55,70,55
55 IF(C-SCOL) 60,70,60
60 IF(C-DASH) 65,70,65
65 NC=NC+1
70 KT=KT+1
IF(KT-72) 20,20,10
75 AWS=FLOAT(NW)/NS
ASW=FLOAT(NC)/NW
WRITE(6,104) NS,AWS,ASW
CALL EXIT
END
I don't think the program works, arithmetic ifs or not. I don't have Fortran compiler but a line-by-line mechanical translation into Pascal seems to go into an infinite loop. Will it work in Fortran?
INTEGER C,KT,BUFR(72),BLANK,COMMA,SCOL, DASH,SLSH,PEROD,NW,NC,NS
REAL AWS,ASW
DATA BLANK,COMMA,SCOL,DASH,SLSH,PEROD/' ',',',';','-','/','.'/,
* NW,NC,NS,KT,C/0,0,0,73,' '/
101 FORMAT(1H1,35X,'INPUT TEXT')
102 FORMAT(72A1)
103 FORMAT(4X,72A1)
104 FORMAT(///,26X,'NUMBER OF SENTENCES=',I8,/,19X,'AVERAGE NUMBER OF
*WORDS/SENTENCE=',F8.2,/20X,'AVERAGE NUMBER OF SYMBOLS/WORD=',F8.2)
WRITE(6,101)
10 READ(5,102) BUFR
WRITE(6,103) BUFR
KT=KT-72
IF(C-PEROD) 20,35,20
20 C=BUFR(KT)
25 IF(C-PEROD) 40,30,40
30 NS=NS+1
NW=NW+1
KT=KT+3
IF(KT-72) 35,35,10
35 C=BUFR(KT)
IF(C-SLSH) 25,75,25
40 IF(C-BLANK) 50,45,50
45 NW=NW+1
GOTO 70
50 IF(C-COMMA) 55,70,55
55 IF(C-SCOL) 60,70,60
60 IF(C-DASH) 65,70,65
65 NC=NC+1
70 KT=KT+1
IF(KT-72) 20,20,10
75 AWS=FLOAT(NW)/NS
ASW=FLOAT(NC)/NW
WRITE(6,104) NS,AWS,ASW
CALL EXIT
END