2017-04-03 18 views
-9

我写下面的代码。但是直到最终的printf才会运行。此外,如果我设置的验证未能通过,则打印结果无法解释。问题与C程序(也许解决使用数组)

#include <stdio.h> 
int main(void) 
{ 
int k, j, z, i, n; 
int l[30]; 
//setting initial values 0 (0=off 1=on) 
for (n=0; n<30; n++) 
{ 
    l[n]=0; 
} 
// employee number 
printf("give employee number\n"); 
scanf("%d", &k); 
//validation of k 
if (k<0 || k>30) 
{ 
    printf("wrong input"); 
} 
    else 
    //lamp status change 
    for (i=1; i=k; i=i+1) 
    { 

     for (z=i; z=30; z=2*z) 
     { 
      if (l[z]=0) 
      l[z]=1; 
      else 
      l[z]=0; 
     } 

    } 

    for (j=0; j<30; j++); 
    { 
     printf("lamp[%d] is: %d\n", j+1, l[j]); 
    } 

return(0); 
} 
+1

欢迎来到Stack Overflow!请说明迄今为止的研究/调试工作。请先阅读[问]页面。 –

+2

如果我们不知道您需要什么帮助,我们无能为力。你有算法吗?如果没有,请询​​问你所困扰的是什么。如果是这样,请提供并向我们展示如何实施它。 –

+0

在我们能做任何事情来帮助你之前,你必须出示工作证据。 –

回答

1

我建议你去工作,多一点你的C基础...

1日建议,所报告的halfer你应该把你的缩进代码护理,它可以让你(和我们)更容易阅读。

  • 1错误,也helfer指出,你可能忘了申报代码 块与{},为您的研究提示,知道什么时候把它 here
  • 第二个错误,你应该看看forloop syntaxfor (j=0; j<30; j++);基本上什么都不会做,因为最后是;
  • 你混淆做作和条件测试,if (l[z]=0)for (i=1; i=k; i=i+1)for (z=i; z=30; z=2*z)没有条件测试,但做作(只是=而不是==<=等),所以他们总是真的...

而且你不” t解释你想要你的代码做什么......看起来你想打开一些灯,但错误for的双语句循环令人困惑。我不知道,如果你想打开2^N灯泡或只是由用户选择的一个...这里是我的代码的修正:

int main(void) { 
    int k, j, z, i, n; 
    int l[30]; 

    //setting initial values 0 (0=off 1=on) 
    for (n=0; n<30; n++) { 
      l[n] = 0; 
    } 

    // employee number 
    printf("give employee number\n"); 
    scanf("%d", &k); 

    //validation of k 
    if (k<0 || k>30) { 
     printf("wrong input"); 
    } else { //new block 

     //lamp status change 
     /* 
     i = k is an affectation not a test, maybe i == k ? 
     but still false, for do while the condition is true 
     so use <= and why use i = i+1 here and not i++ like for n++ L6 ? 
     Ok for this loop, but with the other you gonna reswitch again and 
     again. If you want only switch the one selected, consider to use 
     an if instead of the 2nd for loop. 
     */ 
     for (i=1; i <= k; i=i+1) { 

      /* 
      Same test/affectation misunderstanding. 
      Personnaly except 15, I don't know a lot of intergers 
      mutiplied by 2 that give 30. Exemple : if I set 1 in 
      my keyboard, k = 1, then i = 1, z = 1, z = 2,z = 4, 
      z = 8,z = 16, z = 32, z = 64 etc to overflow. 
      So z = 30 (ouch z == 30) is never true. 
      If you tried to switch only the lamp selected by the user 
      I don't see the point of the second for loop. 
      But if you wanted to switch 1 light each 2^N bulbs you 
      should set z <= 30. 
      */ 
      for (z=i; z<=30; z=2*z) { 
       if (l[z] == 0) //same this is an affectation, == instead ? 
        l[z] = 1; //no need of block because only 1 instruction 
       else 
        l[z] = 0; //idem 1 instruction 
       } 

      } 

      for (j=0; j<30; j++) { // no ; here, see the for loop syntax 
       printf("lamp[%d] is: %d\n", j+1, l[j]); 
      } 
    } //end of the else block 

    return(0); 
} 

所有你对我的意见英语或/和C是受欢迎的。