【问题标题】:Nested switch leads to an infinite loop嵌套开关导致无限循环
【发布时间】:2015-04-10 11:34:40
【问题描述】:

在下面的程序中,我需要将初始字符和最终字符更改为它们各自的字符,如下所述,但这给了我一个无限循环。我应该怎么做才能修复它?

int main(void)
{
 char state ='t';
 char word[20]="aaabbccaaaaccbbb";

 int initiallength = strlen(word)-1; strcat(word,"a");
 while(strlen(word)-1 >initiallength)
 {
   switch(state)
   {
     case 't':
       switch(word[strlen(word)-1])
       {
         case 'a':
           word[strlen(word)-1]='b'; break;
         case 'b':
           word[strlen(word)-1]='c'; break;
         case 'c':
           word[strlen(word)-1]='d'; break;
         case 'd':
           word[strlen(word)-1]='\0'; break;
       }
       switch(word[0])
       {
         case 'a':
           word[0]='b'; break;
         case 'b':
           word[0]='c'; break;
         case 'c':
           word[0]='d'; break;
         case 'd': 
           word[0]='\0'; break;
       }
   }
 }
}

【问题讨论】:

  • 如果您需要任何帮助,请使用代码块格式化此代码。无法按现状阅读。
  • 在调试器中运行您的程序并逐行逐行查看它的实际作用。
  • 排除任何其他问题,您永远不会更新initiallengthword 的长度,这意味着您需要考虑while 条件strlen(word)-1 >initiallength 的作用。
  • @nos ,word 中的 NUL 终止符在某些位置添加。所以strlen(word) 会改变
  • 也不更新state 所以switch(state) 也是没用的

标签: c switch-statement infinite-loop


【解决方案1】:

如果我理解正确,您想要做的是交换给定字符串中的第一个和最后一个字符。如果是这种情况,首先您的代码太复杂了,其次您获得无限循环的原因是因为条件strlen(word)-1 >initiallength 始终为真。

【讨论】:

  • 你能解释一下 strlen(word)-1 >initiallength 总是正确的吗?
  • 在初始化initiallength时监听它等于字符串的大小,然后在原始字符串中添加一个字符,使其大小大于initiallength。为了使您的循环结束,您必须在每次迭代中使字符串更小,直到其大小小于初始长度,否则 strlen(words)-1 将始终大于初始长度。如果你向我解释你想做什么,我可能会帮助你,我真的不明白你想在这里完成什么。
【解决方案2】:

判断单词是否为空

int main(void){                                                                
    char state ='t';                                                           
    char word[20]="aaabbccaaaaccbbb";                                          

    int initiallength = strlen(word)-1;                                        

    strcat(word,"a");                                                          

    while(strlen(word)-1 >initiallength && strlen(word) >= 0){                 
        printf("%d %d\n", strlen(word)-1, initiallength);                      
        printf("%d len %s\n", strlen(word), word);                             
        switch(state){                                                         
          case 't':                                                            
            switch(word[strlen(word)-1]){                                      
              case 'a':                                                        
              case 'b':                                                        
              case 'c':                                                        
                word[strlen(word)-1]++;                                        
                break;                                                         
              case 'd':                                                        
                word[strlen(word)-1] = '\0';                                   
                break;                                                         
            }                                                                  

            switch(word[0]){                                                   
              case 'a':                                                        
              case 'b':                                                        
              case 'c':                                                        
                word[0]++;                                                     
                break;                                                         
              case 'd':                                                        
                word[0] = '\0';                                                
                break;                                                         
            }                                                                  
        }                                                                      
    }                                                                          
}        

【讨论】:

  • 我认为这也会在无限循环中运行。
  • 那么,问题出在 -1 和 16(strlen(word)-1 >initiallength) 之间的比较上?
  • @Himanshu 是的,它正在无限循环中运行,问题是 -1 和 16 的比较。我不明白 prog 将 >= 替换为 > 以停止无限循环,但 word 将设置为空字符串
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2020-08-27
  • 2021-05-07
  • 2012-08-24
  • 2020-11-16
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多