영어로 읽는 코딩

03 [C] 단어 수 세는 코드

#include <stdio.h>
#define IN    1 /* inside a word */
#define OUT 0 /* outside a word */
/* Count lines, words, and characters in input */
int main()
{
	int c, nl, nw, nc, state;
	state = OUT;
	nl = nw = nc = 0;
	while ((c = getchar()) != EOF) {
		++nc;
		if (c == '\n')
			++nl;
		if (c == ' ' || c == '\n' || c == '\t')
			state = OUT;
		else if (state == OUT) {
			state = IN;
			++nw;
		}
	}
	printf("%d %d %d\n", nl, nw, nc);
}
/*The C Programming Language (second edition, Prentice Hall) 
by Brian W. Kernighan and Dennis M. RitchieS*/

 

 

* 코드 설명. Symbolic constant를 미리 define 해서 1 0대신 OUT IN사용하는 이유 - #Define

Every time the program encounters the first character of a word, it counts one more word. The variable state records whether the program is currently in a word or not; initially it is “not in a word,” which is assigned the value OUT. We prefer the symbolic constants IN and OUT. to the literal values 1 and 0 because they make the program more readable. In a program as tiny as this, it makes little difference, but in larger programs, the increase in clarity is well worth the modest extra effort to write it this way from the beginning. You’ll also find that it’s easier to make extensive changes in programs where magic numbers appear only as symbolic constants.

 

* 코드 설명: 연산자의 연산 방향

The line

nl = nw = nc = 0;

sets all three variables to zero. This is not a special case, but a consequence of the fact that an assignment is an expression with a value and assignments associate from right to left. It’s as if we had written

nl = (nw = (nc = 0));

The operator || means OR, so the line

if (C == ’ ‘ || c == ’ \n ‘ || c == ’ \t ’ )

says "if c is a blank or c is a newline or c is a tab”. (Recall that the escape sequence \t is a visible representation of the tab character.) There is a corresponding operator && for AND; its precedence is just higher than ||. Expressions connected by && or || are evaluated left to right, and it is guaranteed that evaluation will stop as soon as the truth or falsehood is known. If c is a blank, there is no need to test whether it is a newline or tab, so these tests are not made. 

 

*코드 설명: if - else

The example also shows an else, which specifies an alternative action if the condition part of an if statement is false. The general form is

 

if (expression)

statement 1

else

statement 2

One and only one of the two statements associated with an if-else is performed. If the expression is true, statement 1 is executed; if not, statement 2 is executed. Each statement can be a single statement or several in braces. In the word count program, the one after the else is an if that controls two statements in braces.

 

*코드 설명: if - else

The example also shows an else, which specifies an alternative action if the condition part of an if statement is false. The general form is

 

if (expression)

statement 1

else

statement 2

One and only one of the two statements associated with an if-else is performed. If the expression is true, statement 1 is executed; if not, statement 2 is executed. Each statement can be a single statement or several in braces. In the word count program, the one after the else is an if that controls two statements in braces.

 [The C Programming Language (second edition, Prentice Hall) by Brian W. Kernighan and Dennis M. Ritchie]

댓글

댓글 본문
작성자
비밀번호
버전 관리
Yoo Moon Il
현재 버전
선택 버전
graphittie 자세히 보기