C语言 使用nested-if查找四个数中最大的一个

4nkexdtk  于 9个月前  发布在  其他
关注(0)|答案(6)|浏览(53)

先生/女士:
我不知道我做错了什么。从逻辑上讲,我觉得一切都是正确的。大括号似乎也被放置在正确的位置。那为什么我会收到以下错误信息呢?
32 8 [错误]在“(”标记之前需要构造函数、析构函数或类型转换
33 2 [错误]在'return'之前需要unqualified-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;
}
zbsbpyhn

zbsbpyhn1#

您可以使用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);
}
4smxwvx5

4smxwvx52#

我不知道我做错了什么。从逻辑上看,一切都是正确的。
不过,还是有一些语法错误。一个类似于

// ...
if ( a > c ) {
    // ...
}
else {               // <----
    // ...
}
else if ( b > c ) {
    // ...
}

在C中无效。
我不清楚OP计划如何构造嵌套的if s来解决这个任务,但在继续之前,我建议编写一些简单的测试来验证算法的正确性:

#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...
    }
}
k2arahey

k2arahey3#

我使用了三进制操作符而不是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;
}
5jdjgkvh

5jdjgkvh4#

必须添加一个if语句。你忘了,这就是为什么它给出一个错误

else
    if(c>d)
        cout << c << "is greater number" << endl;
    else
        cout << d << "is greater number" << endl;
tvokkenx

tvokkenx5#

您的代码中存在多个问题:

  • 你使用了一个过时的语法main(),没有返回类型:你的编译器拒绝它,因为它被配置为编译C++代码。
  • 程序没有正确缩进:这将防止视觉分析,这将表明你错过了一些大括号和一些情况:
#include <stdio.h>

int 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;
        }
    // missing }
    else
    // missing {
        if (b > c) {
            if (b > d)
                big = b;
            else
                big = d;
        } // missing else branch
    }
    printf("The biggest number is %d\n", big);
    return 0;
}
  • 你应该检查和处理scanf()转换失败.

以下是完整版本:

#include <stdio.h>

int main(void) {
    int a, b, c, d, big;
    printf("\n Enter value to a, b, c, d: ");
    if (scanf("%d %d %d %d", &a, &b, &c, &d) != 4)
        return 1;
    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("The biggest number is %d\n", big);
    return 0;
}

嵌套测试可以使用三元运算符重写:

#include <stdio.h>

int main(void) {
    int a, b, c, d;
    printf("\n Enter value to a, b, c, d: ");
    if (scanf("%d %d %d %d", &a, &b, &c, &d) != 4)
        return 1;
    int big = (a > b) ? (a > c) ? (a > d) ? a : d
                            : (c > d) ? c : d
                  : (b > c) ? (b > d) ? b : d
                            : (c > d) ? c : d;

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

您可以使用中间变量来简化此操作:

int max_ab = (a > b) ? a : b;
    int max_cd = (c > d) ? c : d;
    int big = (max_ab > max_cd) ? max_ab : max_cd;

最后,使用一种易于扩展到更多变量的模式通用方法:

int big = a > b ? a : b;
    big = big > c ? big : c;
    big = big > d ? big : d;
3wabscal

3wabscal6#

您没有考虑a <= b或它放置在错误位置(在a < 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;
}

相关问题