Turbo C 问题
您好,我正在使用 Turbo C...我只是有一些疑问,我在一本书中找到了 TC 的代码,但我对给出的说明不满意。这是代码:
main()
{
int count = -1; /* why it was initialized as -1? */
char ch;
printf("Type in a phrase:\n");
ch = 'a'; /* why it was initialized as 'a'? */
while (ch != '\r') /* perform while ch is not equal to return */
{
ch = getche();
count++; /* increment the count */
}
printf("\nCharacter count is %d", count); /* prints the value of count */
}
提前致谢!
Hello I am using Turbo C... I just have some query, I found a code of TC in a book but I'm not satisfied with the given clarification. Here's the code:
main()
{
int count = -1; /* why it was initialized as -1? */
char ch;
printf("Type in a phrase:\n");
ch = 'a'; /* why it was initialized as 'a'? */
while (ch != '\r') /* perform while ch is not equal to return */
{
ch = getche();
count++; /* increment the count */
}
printf("\nCharacter count is %d", count); /* prints the value of count */
}
Thanks in advance!
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
假设您的用户输入“abc”并按 Enter 键,因此输入缓冲区包含“a”、“b”、“c”、“/r”(最后一个字符代表返回)。缓冲区中有 4 个字符,但您的用户实际上只输入了 3 个字符(其中一个是 return),因此您需要从计数中减去 1。或者,从 -1 而不是 0 开始计数。
您可以这样想 - 这会循环多少次?
关于第二点,ch 初始化为什么并不重要,只要它不是 '\r' 即可。这意味着您将至少进入循环一次并读入字符。
Suppose your user types in "abc" and presses enter, so the input buffer contains 'a','b','c','/r' (this last character represents return). There are 4 characters in the buffer, but your user only really typed in 3 (one was return), so you need to subtract one from the count. Or, alternatively, start the count at -1 rather than 0.
You could think of it this way - how many times does this go through the loop?
On your second point, it doesn't really matter what ch is initialized to, as long as it's not '\r'. This means that you'll go into the loop at least once, and read in the characters.
在我看来,好像它正在计算一行中的字符,不包括最后的“回车”字符。这就是为什么它从 -1 开始 - 这样 '\r' 字符就不会成为计数的一部分。
这样
while
循环中的条件最初将为 true。可以选择任何内容,只要它不是 '\r',否则条件将立即为假,并且不会读取任何字符。Looks to me as if it's counting characters in a line, excluding the final 'carriage return' character. That's why it starts at -1 - so that the '\r' character won't be part of the count.
Just so the condition in the
while
loop will be initially true. Anything could have been chosen, just so long as it wasn't '\r', as then the condition would have been false immediately and no characters would be read.在第一个问题中,无论如何都可以启动计数值。不强制只从 -1 开始。
他们这样做只是为了自己的方便。我们只想从 1 或 0 开始计算数字。相同的公式将应用于字母表。句子和单词将是字母的组合。为了按照特定的顺序开始,我们应该这样计数,不会出现任何混乱。
如果我们输入任何短语或其他任何内容,我们需要一个终止变量,例如 \0,因为它们不存在于字母表中。为了计算频率,我们需要增加计数。
In the first question, count value can be started in anyway. It is not mandatory to start only with -1.
They just did that for their own convenience. We just want to count the numbers from 1 or 0. The same formula will be applied to the alphabets. Sentence and word will be the combination of the alphabets. In order to start in a particular order, we should count like that without any confusion.
If we type any phrase or anything else we need a termination variable like \0, as they were not present in alphabets. To calculate the frquency, we need to increment the count.