#include
#include
#include
#define STOP '|'
int main(void)
{
char c;
char prev;
long n_chars = 0L;
int n_lines = 0;
int n_words = 0;
int p_lines = 0;
bool inword = false;
printf("enter next to be analyzed (| to terminate):\n");
prev = '\n';
while ((c = getchar()) != STOP)
{
n_chars++;
if (c == '\n')
n_lines++;
if (!isspace(c) && !inword)
{
inword = true;
n_words++;
}
if (isspace(c) && inword)
inword = false;
prev = c;
}
if (prev != '\n')
p_lines = 1;
printf("characters=%ld,words=%d,lines=%d,",
n_chars, n_words, n_lines);
printf("partial lines=%d\n", p_lines);
getchar();
getchar();
return 0;
}
第二個if那裡,!inword為假,那為什麼要把 bool inword聲明為false?我覺得把inword聲明為true更方便呀,intword聲明為false,然後!inword也是false,這樣或許會讓人產生理解錯誤?
inword表示是否在單詞裡面,最一開始為false,所以收到第一個不是空格的字符時,單詞數加1,並且把inword標記改為true,這樣下一個不是空格的字符到來單詞球就不會加1,空格到來的話,表示一個單詞輸入完畢,把inword改為false表示不在單詞裡面, 總之這段程序應該是統計輸入的單詞數的吧