學習之路-練習題參考答案 1-11

#include<stdio.h>
#define IN 1
#define OUT 0
int main()
{
 int c;
 int nCharCount = 0,nWordsCount = 0, nLinesCount = 0;
 int state = OUT;
 while(1)
 {
  while((c =getchar()) != EOF)
    {
      nCharCount++;
      if(c == '\n')
      {
          nLinesCount++;
      }
           else if(c == ' ' || c == '\t' || c == ' ')
     {
         state = OUT;
     }
     else if(state == OUT)
    {
            state = IN;
   nWordsCount++;
    }
     
 }

    printf("字符數:%d 單詞數:%d 行數:%d\n",nCharCount, nWordsCount, nLinesCount);
    nCharCount = 0;nWordsCount = 0;nLinesCount = 0; state = OUT;
 }
    return 0;
}

用一個whlie(1)死循環不斷輸入。

程序中若存在錯誤最可能的輸入是些特殊的輸入:

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章