Turbo C 问题

发布于 2024-11-07 12:41:14 字数 591 浏览 5 评论 0原文

您好,我正在使用 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

吲‖鸣 2024-11-14 12:41:14

假设您的用户输入“abc”并按 Enter 键,因此输入缓冲区包含“a”、“b”、“c”、“/r”(最后一个字符代表返回)。缓冲区中有 4 个字符,但您的用户实际上只输入了 3 个字符(其中一个是 return),因此您需要从计数中减去 1。或者,从 -1 而不是 0 开始计数。

您可以这样想 - 这会循环多少次?

  • 计数从-1 开始。
  • 第一次:从字符串中读取“a”。再次循环,因为它不是“/r”。现在计数为 0。
  • 第二次:从字符串中读取“b”。再次循环,因为它不是“/r”。现在计数为 1。
  • 第三次:从字符串中读取“c”。再次循环,因为它不是“/r”。现在计数为 2。
  • 第四次:从字符串中读取“/r”,然后停止。 count 现在是 3。

关于第二点,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?

  • Count starts at -1.
  • First time: read 'a' from string. Go round again as it's not '/r'. count is now 0.
  • Second time: read 'b' from string. Go round again as it's not '/r'. count is now 1.
  • Third time: read 'c' from string. Go round again as it's not '/r'. count is now 2.
  • Fourth time: read '/r' from string, and stop. count is now 3.

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.

命硬 2024-11-14 12:41:14
 int count = -1;                \* why it was initialized as -1? *\

在我看来,好像它正在计算一行中的字符,不包括最后的“回车”字符。这就是为什么它从 -1 开始 - 这样 '\r' 字符就不会成为计数的一部分。

 ch = 'a';                      \* why it was initialized as 'a'? *\

这样 while 循环中的条件最初将为 true。可以选择任何内容,只要它不是 '\r',否则条件将立即为假,并且不会读取任何字符。

 int count = -1;                \* why it was initialized as -1? *\

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.

 ch = 'a';                      \* why it was initialized as 'a'? *\

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.

我不咬妳我踢妳 2024-11-14 12:41:14

在第一个问题中,无论如何都可以启动计数值。不强制只从 -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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文