【问题标题】:Program to find biggest of four numbers using nested-if使用嵌套 if 查找四个数字中最大的程序
【发布时间】:2019-10-30 21:46:05
【问题描述】:

先生/女士,
我不知道我做错了什么。从逻辑上讲,一切对我来说都是正确的。牙套似乎也放置在正确的位置。那为什么我会收到以下错误消息?

32 8 [错误] '(' 标记之前的预期构造函数、析构函数或类型转换
33 2 [错误] 'return' 之前的预期不合格 ID
34 1 [错误] '}' 标记之前的预期声明

我对这个特定程序做错了什么?而且,我希望使用相同的语句进行更正,而不是任何其他语句。

请帮忙!

程序:

#include <stdio.h>
main()
{
    int a, b, c, d, big;
    printf("\n Enter value to a, b, c, d: ");
    scanf("%d %d %d %d", &a, &b, &c, &d);
    if (a >b)
    {
        if(a>c)
        {
            if(a>d)
            big = a;
            else
            big=d;
        }
        else
           {
           if(c>d)
            big = c;
            else
            big = d;
        }
        else
            if(b>c)
            {
                if(b>d)
                big = b;
                else
                big = d;
            }
    }
    printf("\n The biggest number is %d", big);
    return 0;
}

【问题讨论】:

  • 构造函数和析构函数不是 C 语言的一部分。
  • 你不能在另一个 else 语句之后有第二个 else 语句。在第 23 行附近,您需要一个 else if 语句。
  • 您正在使用 C++ 编译器。因此,您必须使用符合 ISO 标准的 main 定义:int main()。除了else 问题(这只是一个错位的},如果您正确缩进代码可以避免),您还缺少一些逻辑。请注意“固定”程序编译但不起作用:godbolt.org/z/RoWlRj
  • 当然,因为这是C++,你可以使用int big = std::max(std::max(a, b), std::max(c, d))
  • 这是 C,不是 C++。但是,是的,编译器同时兼容 C 和 C++。 @paddy

标签: c nested-if


【解决方案1】:

您可以使用ternary operator 来解决您的问题。因此,代码简化为

int main() {
    int a, b, c, d;
    printf("\n Enter value to a, b, c, d: ");
    scanf("%d %d %d %d", &a, &b, &c, &d);

    const int big1 = (a > b) ? a : b;
    const int big2 = (c > d) ? c : d;
    int big = (big1 > big2) ? big1 : big2;

    printf("\n The biggest number is %d", big);
}

【讨论】:

    【解决方案2】:

    我不知道我做错了什么。从逻辑上讲,一切对我来说都是正确的。

    不过有一些语法错误。像

    这样的构造
    // ...
    if ( a > c ) {
        // ...
    }
    else {               // <----
        // ...
    }
    else if ( b > c ) {
        // ...
    }
    

    在 C 中无效。

    我不清楚 OP 计划如何构建嵌套的 ifs 以解决任务,但在继续之前,我建议编写一些简单的测试来验证算法的正确性:

    #include <stdio.h>
    
    // Prototype of the function which returns the biggest out of the four arguments
    int max_of_four(int a, int b, int c, int d);
    
    // Helper function used to verify the correctness of the results
    int test_(int a, int b, int c, int d, int expected)
    {
        int result = max_of_four(a, b, c, d);
        if ( result != expected )
        {
            printf("FAILED - Expected: %d (given %d, %d, %d and %d), instead of %d.\n", 
                   expected, a, b, c, d, result);
            return 1;
        }
        return 0;
    }
    
    // Test runner
    int main(void)
    {
        int failed = 0;
    
        // The function should be able to find the biggest regardless of its "position"
        failed += test_(1, 2, 3, 4,  4);
        failed += test_(4, 3, 2, 1,  4);
        failed += test_(2, 1, 4, 3,  4);
        failed += test_(3, 4, 1, 2,  4);
    
        // The function should manage negative values
        failed += test_(1, -2, -3, -4,    1);
        failed += test_(-4, -3, -2, -1,  -1);
        failed += test_(0, -3, -1, -2,    0);
    
        // The function should manage duplicate values
        failed += test_(1, -2, 1, 2,      2);
        failed += test_(-4, -3, -3, -5,  -3);
        failed += test_(1, 1, 1, 1,       1);
    
        if ( failed == 0 )
            puts("So far so good.");
    
        return 0;
    }
    
    // A simple implentation.
    int max_of_four(int a, int b, int c, int d)
    {
        int big = a;
        if ( b > a )
            big = b;
        if ( c > big )
            big = c;
        if ( d > big )
            big = d;
        return big;    
    }
    

    您可以测试它here 并尝试使用嵌套的if 语句重写函数,如果您愿意。也许像

    int max_of_four(int a, int b, int c, int d)
    {
        if ( b > a )
        {
            if ( d > c )
            {
                if ( b > d )
                    return b;
                else
                    return d;
            }
            else // d <= c
            {
                if ( b > c )
                    return b;
                else
                    return c;
            }            
        }
        else // b <= a
        {
            // You got the idea...
        }
    }
    

    【讨论】:

      【解决方案3】:

      我使用了三元运算符而不是 if-else 语句,它的工作方式与 if-else 相同,您可以查看 ternary operator 的工作方式。

      int max_of_four(int a,int b,int c,int d){
           return (a < b ? ((b > c)? ((b > d)? b:d) :((c > d)? c: d))  : ((a < c)? ((c > d)? c: d) : ((a > d) ? a : d)));
      }
      
      int main() {
          int a, b, c, d;
          scanf("%d %d %d %d", &a, &b, &c, &d);
          int ans = max_of_four(a, b, c, d);
          printf("%d", ans);
          
          return 0;
      }
      

      【讨论】:

      • @AdrianMole 谢谢你的信息,下次我会努力的
      【解决方案4】:

      a &lt;= b 或它放置在错误的位置(在a &lt; b 块内)时,您没有考虑大小写。

      为了更清楚,您可以在类似于this(3 个数字)的决策树中想象问题。

      #include <stdio.h>
      
      main() {
        int a, b, c, d, big;
        printf("\n Enter value to a, b, c, d: ");
        scanf("%d %d %d %d", &a, &b, &c, &d);
      
        if (a > b) {
          if (a > c) {
            if (a > d)
              big = a;
            else
              big = d;
          } else {
            if (c > d)
              big = c;
            else
              big = d;
          }
      
        } else {
          if (b > c) {
            if (b > d)
              big = b;
            else
              big = d;
          }
          else {
            if (c > d)
              big = c;
            else
              big = d;
          }
        }
      
        printf("\n The biggest number is %d", big);
      
        return 0;
      }
      

      【讨论】:

      • 您能解释一下您为解决问题所做的工作吗? OP 可能没有注意到,因此他无法从中学到任何东西。不是我的反对票。
      猜你喜欢
      • 2022-11-12
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2021-02-06
      相关资源
      最近更新 更多