2

以下代码将第二个数字输出为最大值。如果您需要任何其他信息,请告诉我。

#include <iostream>                                                     
#include <cstdlib>
using namespace std;
double *ComputeMaximum(const double *Max, const double *Min);

double *ComputeMaximum(const double *Max, const double *Min)
{
    return ((double *)((&Max > &Min) ? Max : Min));
}

int main(void)
{
    double *max;
    double Initial, Secondary;


    cout << "Enter the number followed by space then another number: ";
    cin >> Initial;
    cout << "\nIn-" << Initial;
    cin >> Secondary;
    cout << "\nSe-" << Secondary;
    //cout >> "Of " >> Inital >> "and " >> Secondary;
    //cout >> "the maximum is " >>
    max = ComputeMaximum((double*)&Initial,(double*)&Secondary);
    cout << "\nmax" << *max;
    return 0;
}

下一个代码输出第一个数字作为最大值

#include <iostream>                                                     
#include <cstdlib>
using namespace std;
double *ComputeMaximum(const double *Max, const double *Min);

double *ComputeMaximum(const double *Max, const double *Min)
{
    return ((double *)((Max > Min) ? Max : Min));  // Here is the difference(& missing)
}

int main(void)
{
    double *max;
    double Initial, Secondary;


    cout << "Enter the number followed by space then another number: ";
    cin >> Initial;
    cout << "\nIn-" << Initial;
    cin >> Secondary;
    cout << "\nSe-" << Secondary;
    //cout >> "Of " >> Inital >> "and " >> Secondary;
    //cout >> "the maximum is " >>
    max = ComputeMaximum((double*)&Initial,(double*)&Secondary);
    cout << "\nmax" << *max;
    return 0;
}

做错了什么?我只需要最大值,而不是第二个或第一个输入。我得到了答案。谢谢你们。

这里是:

#include <iostream>                                                     
#include <cstdlib>
using namespace std;
double *ComputeMaximum(const double *Max, const double *Min);

double *ComputeMaximum(const double *Max, const double *Min)
{
    return (double*)((*Max > *Min) ? Max : Min);
}

int main(void)
{
    double *max;
    double Initial, Secondary;


    cout << "Enter the number followed by space then another number: ";
    cin >> Initial;
    cout << "\nIn-" << Initial;
    cin >> Secondary;
    cout << "\nSe-" << Secondary;
    //cout >> "Of " >> Inital >> "and " >> Secondary;
    //cout >> "the maximum is " >>
    max = ComputeMaximum(&Initial, &Secondary);
    cout << "\nmax" << *max;
    return 0;
}
4

4 回答 4

6

您正在比较地址。正确的方法是:

double *ComputeMaximum(const double *Max, const double *Min)
{
    return *Max > *Min ? Max : Min;
}

你的版本:

(Max > Min)

比较指针本身,并且

(&Max > &Min)

比较指针的地址,这又是错误的。

此外,您不需要指针,并注意您拥有std::max可以使用的指针。

于 2012-10-29T10:08:26.380 回答
6
double *ComputeMaximum(const double *Max, const double *Min)
{
    return *Max > *Min ? Max : Min;
}

请注意,我使用*Maxand*Min而不是&Maxand &Min,即取消引用,而不是获取地址!double*另请注意,对于已经是所需类型的表达式,您有很多不必要的强制转换。一个例子是您的 ComputeMaximum 函数体。另一个例子

max = ComputeMaximum((double*)&Initial,(double*)&Secondary);

因为InitialandSecondary属于 type double, &Initialand &Secondaryare of typedouble*所以绝对不需要丑陋的不必要的演员表。只需使用

max = ComputeMaximum(&Initial,&Secondary);

其他地方也一样。

我强烈建议您阅读一本关于 C++ 的好书

于 2012-10-29T10:08:28.817 回答
3

你为什么首先使用指针?它甚至不需要。

下面是一个更好的实现:

double ComputeMaximum(double a, double b)
{
    return a > b ? a : b;
}

或者如果你想做这样的事情:

ComputeMaximum(x,y) = 100; //modify the one which is maximum

那么参考就是你需要的:

double & ComputeMaximum(double & a, double & b)
{
    return a > b ? a : b;
}

顺便说一句,您可能希望从标准库中查看std::max(和)。std::min

于 2012-10-29T10:09:24.000 回答
0

它们都不返回实际最大值。

您正在传递指向双打的指针。所以变量Min,Max的值是一个指向 double 的地址。

&Max > &Min...这将比较函数本地的变量Max,Min的地址。

Max > Min...这将比较 Min 和 Max 指向的地址(只是地址编号)。

*Max > *Min...这将取消引用指针,并比较它们指向的对象。这就是你想做的...

return ((double *)((*Max > *Min) ? Max : Min));
于 2012-10-29T10:12:07.287 回答