0

结果为 0.0。我希望它作为 0 以外的数字出现,除非输入的数字当然是 0。我尝试了几件事。这是当前代码。

#include <stdio.h>                                     /* Necessary header */
#include <stdlib.h>


int main()
{
    double Initial;
    double Post;

    printf("Enter a number with a decimal: ");
    scanf("%lf", &Initial);
    printf("Enter another number using the same format: ");
    scanf("%lf", &Post);

    ComputeMinimum(Initial, Post);
    ComputeMaximum(Initial, Post);

    printf("Of %1.1lf and %1.1lf ", Initial, Post);
    printf("the minimum is %1.1lf ", ComputeMinimum(Initial, Post));
    printf("and the maximum is %1.1lf.", ComputeMaximum(Initial, Post));

    return 0;
}

    double ComputeMaximum(double B, double A)
{
    return (A > B) ? A : B;
}

double ComputeMinimum(double a, double b)
{
    return (a < b) ? a : b;
}

我已经尝试过以下方法。另外,我应该使函数的返回类型加倍,但不确定如何。

int main()
{
    double Initial;
    double Post;

    printf("Enter a number with a decimal: ");
    scanf("%lf", &Initial);
    printf("Enter another number using the same format: ");
    scanf("%lf", &Post);

    double minimum = ComputeMinimum(Initial, Post);
    double maximum = ComputeMaximum(Initial, Post);

    printf("Of %1.1lf and %1.1lf ", Initial, Post);
    printf("the minimum is %1.1lf ", minimum);
    printf("and the maximum is %1.1lf.", maximum);

    return 0;
}
4

2 回答 2

1

我不知道,它对我来说非常好用。

您应该在 main 之前添加函数声明,因此它得到:

#include <stdio.h>                                     /* Necessary header */
#include <stdlib.h>

double ComputeMinimum(double a, double b);
double ComputeMaximum(double a, double b);

int main()
{
....

至于您的下一个问题,这些函数的返回类型是 IS double,因此您无需更改任何内容。问题是没有函数原型编译器不知道它会是什么double,所以它假设int

请启用编译器警告,它们真的很有帮助,您应该始终仔细阅读它们。

于 2012-10-21T14:45:04.783 回答
0

For me also it worked i.e. the above program in which you are catching the output in maximum and minimum as double. I think above mention of declaring the prototype is also correect or you can directly put the definition of the both the function above the main program, then in that case you don't even need prototype since it will work as prototype for the compiler as well

于 2012-10-21T15:11:03.947 回答