-3
int main()
 {
 int c,nl,nt,Blanks;
 nl = 0;
 nt = 0;
 Blanks = 0;
 while ((c= getchar()) != '5'){
    if (c ='\n')
        nl++;
    if(c='\t')
        nt++;
    if(c = 's')
        Blanks ++;
 }

printf("No of lines is %d \n No of tabs %d \n and no of blanks %d \n",nl,nt,Blanks);

return 0;

出力:

 No of lines is 12 
 No of tabs 12 
 and no of blanks 12 

 RUN SUCCESSFUL (total time: 5s)

出力は入力された文字の数であり、それらをまったく区別しません。また、EOF を使用して、ファイルの終わりに達したときにループを停止させたところ、ループは停止せず、プログラムは実行を続けました。

4

2 に答える 2

1

あなたifのは間違っています。=それぞれの で1 秒を逃しましたif。今度は に値を代入しているためc、結果は になりtrueます。また、else ifケースは専用ですのでご使用いただけます。

int main()
{
  int c,nl,nt,Blanks;
  nl = 0;
  nt = 0;
  Blanks = 0;
  while ((c= getchar()) != '5'){
    if (c =='\n')
        nl++;
    else if(c=='\t')
        nt++;
    else if(c == 's')
        Blanks ++;
  }

  printf("No of lines is %d \n No of tabs %d \n and no of blanks %d \n",nl,nt,Blanks);

  return 0;
}
于 2014-07-19T21:42:12.383 に答える