需要说明! ! ! [英] Need explanation please! ! !

查看:97
本文介绍了需要说明! ! !的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在解释这段代码时遇到了问题,我真的想详细解释它是如何工作的。

  #include   <   stdio.h  < span class =code-keyword>>  
#include < span class =code-keyword>< stdlib.h >

int main()
{
int x = - 3 ,y = 0 ,k,j;

for (k = j = - 3 ; x = x +(k< ; j),++ j; y + = 2 );
printf( x =%dy =%dk =%dj =%d, X,Y,K,J);

getch();
}



顺便说一下,答案是x = -1 y = 4 k = -3 j = 0.



我尝试了什么:



我试图系统地解决它但我一直陷入增量问题的阶段(++ j)和我的答案最终不同于运行程序

解决方案

这是令人讨厌的代码!

拥有这样的结构总是一个坏主意:

  for (...); 
printf(...);

因为很难看到;在循环结束时 - 所以它看起来像你写的那样随意:

 < span class =code-keyword> for (...)
{
printf(...);
}

哪种结果会有很大差异。事实上,现代编译器会抱怨或至少警告你,因为它确实会导致很多混乱。

所以从重组你的代码开始:

  int  main()
{
int x = - < span class =code-digit> 3 ,y = 0 ,k,j;

for (k = j = - 3 ; ++ j; y + = 2
{
x = x +(k< j);
}
printf( x =%dy =%dk =%dj =%d ,X,Y,K,J);
return 0 ;
}

现在它更容易阅读,更明显的是发生了什么。

1)预设x为-3,y为0。

2)将k和j预设为-3。

3)进入循环。

3.1)比较k和j。

3.1.1)如果k小于j,则向x中加1(记住,在C中,任何逻辑运算的结果对于false为零,或者对于true为1,因此 k 返回0或1)

3.1.2)否则,将x加零。

3.2将2添加到y

3.3在j中添加一个,如果该值为非零,则循环回到(3)。

4)打印结果。



看看你写出来的时候阅读和理解有多容易吗?

虽然这仍然是令人讨厌的代码:我更愿意看到这个:

  int  main()
{
int x = - 3 ,y = 0 ,k,j;

for (k = j = - 3 ; ++ j; y + = 2
{
if (k< j)
{
x ++;
}
}
printf( x =%dy =%dk =% DJ =%d,X,Y,K,J);
return 0 ;
}

或者:

  int  main()
{
int x = - 3 ,y = 0 ,k,j;

for (k = j = - 3 ; ++ j; y + = 2
{
x + =(k< j)? 1 0 ;
}
printf( x =%dy =%dk =%dj =%d ,X,Y,K,J);
return 0 ;
}

您正在玩的值更明确。


要了解该代码,您需要了解 for for循环前缀增量运算符



这个for循环有点乱。通常只在头部检查和计数器逻辑和在体内。



  int  main()
{

for int y = 0 ; y< 5 ; y + = 2 // 通常你会检查柜台
{
j ++;
x = x +(k< j); // 奇怪应该是x + 1(真)???
如果(j> y) // 替代退出
{
break ;
}
}

printf( x =%dy = %DK =%DJ =%d,X,Y,K,J);

getch();
}

此演示代码可能会产生除代码之外的其他结果。


从这里开始:为什么x = ++ x + x ++给我错误的答案? [ ^ ]

I'm having problems explaining this code and I would really like a detailed explanation of how it works.

#include <stdio.h>
#include <stdlib.h>

int main()
{
    int x=-3, y=0, k,j;

    for( k=j=-3 ; x= x+(k<j),++j ; y+=2);
    printf("x=%d  y=%d k=%d  j=%d",x,y,k,j);

    getch();
}


By the way, the answers are x=-1 y=4 k=-3 j=0.

What I have tried:

I tried to solve it systematically but I keep getting stuck at the increment phase of the problem (++j) and my answers end up different from the run program

解决方案

That is nasty code!
It's always a bad idea to have a construct like this:

for (...);
printf(...);

because is't very difficult to see the ";" at the end of the for loop - so it looks to a casual eye like you wrote this:

for(...)
   {
   printf(...);
   }

Which would have very different results. In fact, modern compilers will complain or at least warn you about it because it does cause a lot of confusion.
So start by restructuring your code:

int main()
{
    int x = -3, y = 0, k, j;

    for( k = j = -3 ; ++j ; y += 2)
       {
       x = x + (k < j);
       }
    printf("x=%d  y=%d k=%d  j=%d",x,y,k,j);
    return 0;
}

Now it's easier to read and a lot more obvious what is going on.
1) Preset x to -3, y to 0.
2) Preset k and j to -3.
3) Enter the loop.
3.1) Compare k and j.
3.1.1) If k is less than j, add one to x (Remember, in C the result of any logical operation is zero for false, or one for true, so k < j returns 0 or 1)
3.1.2) Otherwise, add zero to x.
3.2 Add 2 to y
3.3 Add one to j, and if that value is nonzero, loop back round to (3).
4) Print the results.

See how much easier it is to read and understand when you write it out properly?
It's still nasty code though: I'd prefer to see either this:

int main()
{
    int x = -3, y = 0, k, j;

    for( k = j = -3 ; ++j ; y += 2)
       {
       if (k < j)
          {
          x++;
          }
       }
    printf("x=%d  y=%d k=%d  j=%d",x,y,k,j);
    return 0;
}

Or this:

int main()
{
    int x = -3, y = 0, k, j;

    for( k = j = -3 ; ++j ; y += 2)
       {
       x += (k < j) ? 1 : 0;
       }
    printf("x=%d  y=%d k=%d  j=%d",x,y,k,j);
    return 0;
}

Where the values you are playing with are more explicit.


To understand that code you need to learn about the for loop and the prefix increment operator.

This for loop is a bit messy. Normally is in the head only the check and counter logic and the in the body.

int main()
{

 for( int y = 0 ; y < 5; y+=2) //normally you check the counter
 {
   j++;
   x= x+(k<j);// strange should be x + 1 (true) ???
   if( j > y ) //alternative exit
   {
     break;
   }
 }
 
  printf("x=%d  y=%d k=%d  j=%d",x,y,k,j);

    getch();
}

This demonstration code may led to other results than your code.


Start here: Why does x = ++x + x++ give me the wrong answer?[^]


这篇关于需要说明! ! !的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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