【问题标题】:Why not returning true even if my condition is verified即使我的条件得到验证,为什么不返回 true
【发布时间】:2020-11-24 09:47:31
【问题描述】:

我是编程新手,我正在研究这个程序。

要求是:编写一个 C 程序来检查给定的整数数组(长度至少为 2),如果有两个值 15、15 相邻,则返回 true。

虽然我的条件得到验证,但除非我排除“else”,否则该函数不会返回 true。但是如果我排除其他,它不会在 false 时返回 0,而是返回任何其他随机数。我不明白为什么以及如何解决它。

#include <stdio.h>
#include <stdbool.h>

int check_fifteen(int a[], int size);

int main(){
   int a[]={1, 6, 15, 15, 3, 5, 15};
   int size= sizeof(a) / sizeof(a[0]);

   printf( "%d", check_fifteen(a, size));

}
int check_fifteen(int a[], int size){
   int i=0;

   if(size < 2){
       return 0;
   }
   for(i=0; i<size-1; i++){
       if( ( a[i] == 15 ) && ( a[i+1] == 15  ) ){
           return true;
       }
       else
           return false;
   }
}

【问题讨论】:

    标签: c loops for-loop boolean-expression function-definition


    【解决方案1】:

    即使一对不满足if condition,您也会返回false。这不应该发生,您应该检查所有对,如果没有满足则返回 false。

    if( ( a[i] == 15 ) && ( a[i+1] == 15  ) ){
               return true;
           }
           else
               return false;
    }
    

    for(i=0; i<size-1; i++){
           if( ( a[i] == 15 ) && ( a[i+1] == 15  ) ){
               return true; // if any pair satisfies you return true there itself
           }
    }
    
    // you will reach here only if every pair doesn't satisfy the requirement. 
    return false;
    

    【讨论】:

      【解决方案2】:

      一旦遇到数组中两个相互相等且等于15 或不相等的连续元素,循环就会中断。

         for(i=0; i<size-1; i++){
             if( ( a[i] == 15 ) && ( a[i+1] == 15  ) ){
                 return true;
             }
             else
                 return false;
         }  
      

      至少将语句return false 放在循环之外。

         for(i=0; i<size-1; i++){
             if( ( a[i] == 15 ) && ( a[i+1] == 15  ) ){
                 return true;
             }
         }
         return false;
      

      最好按照下面的方式声明和定义函数

      int check_fifteen( const int a[], size_t size )
      {
          const int Target = 15;
          int success = 0;
      
          for ( size_t i = 1; !success && i < size; i++ )
          {
              if ( a[i] == Target ) success = a[i-1] == Target;
          }
      
          return success;
      }
      

      和变量size 声明类似

      size_t size = sizeof(a) / sizeof(a[0]);
      

      此外,编写一个仅检查两个相邻元素是否等于 15 的函数也没有太大意义。您可以编写一个更通用的函数。

      你来了。

      #include <stdio.h>
      
      int check_equal_adjacent( const int a[], size_t size, int value )
      {
          int success = 0;
      
          for ( size_t i = 1; !success && i < size; i++ )
          {
              if ( a[i] == value ) success = a[i-1] == value;
          }
      
          return success;
      }
      
      int main(void) 
      {
          int a[] = { 1, 6, 15, 15, 3, 5, 15 };
          size_t size = sizeof( a ) / sizeof( *a );
      
          printf( "%s\n", check_equal_adjacent( a, size, 15 ) ? "true" : "false" );
         
          return 0;
      }
      

      程序输出是

      true
      

      【讨论】:

        【解决方案3】:

        问题是函数check_fifteen在第一次迭代时已经返回false,因为不满足if条件(指向数组的前两个元素不包含值15 ),在else 语句中,您会立即返回false

        for (i = 0; i < size-1; i++){
           if ( ( a[i] == 15 ) && ( a[i+1] == 15  ) ){
               return true;
           }
           else
               return false;    // You tell: If the 'if' condition is not matched, return `false`.
        }
        

        return false; 放在循环之后,这样else 语句不会在if ( a[i] == 15 &amp;&amp; a[i+1] == 15 ) 不为真时使函数在第一次迭代时返回。

        for (i = 0; i < size-1; i++){
           if ( a[i] == 15  && a[i+1] == 15 ) {
               return true;
           }
        }
        
        return false;
        

        【讨论】:

          【解决方案4】:

          如果您遵循每个函数应该只有一个返回点的规则(指南、风格偏好),则可以轻松避免此类错误。只需维护一个在您看到验证条件时设置的标志。比如:

          int
          check_fifteen(const int *a, int size)
          {
                  int status = 0;
                  const int *b = a + size;
                  while( a + 1 < b && !status ) {
                          status = a[0] == 15 && a[1] == 15;
                          a += 1;
                  }
                  return status;
          }
          

          请注意,您可以通过简单的“优化”来减少工作量(请记住,过早的优化是万恶之源):

          int
          check_fifteen(const int *a, int size)
          {
                  int status = 0;
                  const int *b = a + size;
                  while( a + 1 < b && !status ) {
                          if( a[1] == 15 ) {
                                  status = *a++ == 15;
                          } else {
                                  a += 2;
                          }
                  }
                  return status;
          }
          

          【讨论】:

            猜你喜欢
            • 2021-11-30
            • 2020-10-31
            • 2014-08-06
            • 2018-11-21
            • 2015-08-11
            • 1970-01-01
            • 1970-01-01
            • 1970-01-01
            • 1970-01-01
            相关资源
            最近更新 更多