C语言编程 - K&安培; R例如1.5.2 - 修改后的程序不能正常工作按计划[解决] [英] C programming - K&R example 1.5.2 - modified program not functioning as intended [solved]

查看:205
本文介绍了C语言编程 - K&安培; R例如1.5.2 - 修改后的程序不能正常工作按计划[解决]的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我的问题很简单:为什么在第10行我的code和11不能正常工作?我的code的预期目的是为了准确地做与原来K&安培; R code意,但不计算NC时(的getchar()=='\\ n'),你会请赐教

略作修改K&安培; R code:

  / ** K&安培; R  -  1.5.2字符计数** /
#包括LT&;&stdio.h中GT;在输入/ * count个字符;第一个版本* /
主要(){
  长NC;  NC = 0;
  而(的getchar()!= EOF){
    如果(的getchar()!='\\ n'){
      ++ NC;
    }
  }
  的printf(%LD \\ N,NC);
}

我用64位Windows 7,codeBlocks10.05,GNU GCC编译。

我目前的进展和认识:

这是一个运行示例,我输入的字两个并回车,相当于4个输入,之后我preSS CTRL + Z要在进入 ^ Z 或EOF字符。该程序然后打印 1 。我期待它打印 3 。我想唯一合乎逻辑的解释是,它究竟做什么,我适得其反(它的只有的计算换行符?)。事实证明,如果我两个和preSS输入输入单词,可以说4次,它打印 4 。这似乎是计数 NC 每换行字符输入,但若真我$ ​​P $ PSS进入单独(在这种情况下的4倍),然后EOF,它总是打印 0 。在进一步的实验,被一些看不见的手4也许是对这一计划的一个神奇的数字。如果我启动它,并准确击中回车键(一个数字被4整除)次,然后EOF它打印 0 。但是,如果我打输入一些其他的次数的EOF什么都不做,我必须输入 ^ Z 两排,一前一后,要正确结束while循环,和它打印 1 。这是令人难以置信的我的心!


解决方案

麻烦的是,你需要保存从值的getchar() - 在 INT - 因为你正在阅读每次两个字符您增加计数。其中之一是在EOF试验;二是在新行测试。

  INT℃;而((C =的getchar())!= EOF)
{
    如果(C!='\\ n')
        ++ NC;
}


您需要存储的getchar() INT ,而不是<$ C的结果的原因$ C>字符的是,它可以返回每一个可能的字符价值,也是一个不同的值,EOF。如果不使用 INT (你直接存储到一个字符),两件事情之一会发生:


  1. 如果字符是一个符号类型,一个合法的字符(通常Y型变音,Y,拉丁小写字母带分音符,U + 00FFŸ - 至少在$ C从拉丁语1或ISO 8859-1)衍生$ csets将PTED等同于EOF间$ p $,你的程序将终止pmaturely $ p $。

  2. 如果字符是一个无符号的类型,没有字符都不会等同于EOF,所以程序将永远不会停止的循环。

无论这些情况是可取的。存储)的在返回值的getchar(一个 INT prevents这两个问题;它是'唯一'(或者,至少,最简单的)正确的方式来做到这一点。

My question is simply "Why does my code on line 10 and 11 not function properly?" The intended purpose of my code is to do exactly as the original K&R code intended, but to NOT count nc whenever (getchar() == '\n') will you please enlighten me?

slightly modified K&R code:

/** K&R - 1.5.2 Character Counting **/
#include <stdio.h>

/* count characters in input; 1st version */
main(){
  long nc;

  nc = 0;
  while (getchar() != EOF){
    if (getchar() != '\n'){
      ++nc;
    }
  }
  printf("%ld\n", nc);
}

I use 64-bit Windows 7, CodeBlocks10.05, GNU GCC Compiler.

my current progress and understanding:

on a sample run, i type in the word two and hit enter, which equals 4 inputs, after which i press ctrl+Z to enter in a ^Z or EOF character. The program then prints 1. I was expecting it to print 3. I suppose the only logical explanation is that it is doing exactly the opposite of what I intended (it only counts newline characters?). As it turns out, if I type in the word two and press enter, lets say 4 times, it prints 4. It seems to be counting nc for every newline character entered, but yet if I press enter alone (in this case 4 times) and then EOF, it always prints 0. Upon further experimentation, by some hand unseen 4 is perhaps a magical number for this program. If I start it up and hit enter key exactly (a number divisible by 4) times and then EOF it prints 0. However if i hit enter some other number of times the EOF does nothing, and I must enter in ^Z two rows, one after the other, to end the while loop correctly, and it prints 1. This is boggling my mind!

解决方案

The trouble is that you need to save the value from getchar() – in an int – because you are reading two characters for each time you increment the count. One of those is in the EOF test; the second is in the newline test.

int c;

while ((c = getchar()) != EOF)
{
    if (c != '\n')
        ++nc;
}


The reason you need to store the result of getchar() in an int and not a char is that it can return every possible char value and also a distinct value, EOF. If you don't use int (you store direct into a char), one of two things will happen:

  1. If char is a signed type, a legitimate character (often y-umlaut, ÿ, LATIN SMALL LETTER Y WITH DIAERESIS, U+00FF — at least in codesets derived from Latin 1 or ISO 8859-1) will be interpreted as equivalent to EOF, and your program will terminate prematurely.
  2. If char is an unsigned type, no character will ever be equivalent to EOF, so the program will never stop the loop.

Neither of these circumstances is desirable. Storing the return value of getchar() in an int prevents both problems; it is the 'only' (or, at least, the simplest) correct way to do it.

这篇关于C语言编程 - K&安培; R例如1.5.2 - 修改后的程序不能正常工作按计划[解决]的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆