Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Mike Lewis on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Elements of Programming Style

Status
Not open for further replies.

Actor

Programmer
Nov 19, 2003
20
US
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 compiled it with g77 on MinGW+MSYS using command:
Code:
$ g77 word_sentence.f -o word_sentence

Hmmm, and it works - look how:
Code:
$ word_sentence
This is my first sentence.  And this is the second one.  /
1                                   INPUT TEXT
    This is my first sentence.  And this is the second one.  /              



                          NUMBER OF SENTENCES=       2
                   AVERAGE NUMBER OF WORDS/SENTENCE=    5.50
                    AVERAGE NUMBER OF SYMBOLS/WORD=    3.82

However, the echo
Code:
1                                   INPUT TEXT
should be displayed first but this is an common error which I get with g77 running on MinGW+MSYS everytime.

So, as you see I entered the 2 sentences terminated by sslash:
Code:
This is my first sentence.  And this is the second one.  /
and the program delivered this result:
Code:
                          NUMBER OF SENTENCES=       2
                   AVERAGE NUMBER OF WORDS/SENTENCE=    5.50
                    AVERAGE NUMBER OF SYMBOLS/WORD=    3.82

:)
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top