C++编程:定义一个函数,比较两个数的大小,形参分别使用指针和引用

2025-05-14 08:35:23
推荐回答(3个)
回答1:

#include
#include
using namespace std;
float compare(float *a,float &b) //a为指针,b为引用
{

if (*a>b) return *a;
else return b;

}
int main()
{
float x,y;
cin>>x>>y;
cout<<"The max="< return 0;
}

回答2:

#include

using namespace std;

float compare(float *a,float *b)
{

if (*a>*b) return *a;
else return *b;

}

float compare(float &a,float &b)
{
return (a>b?a:b);
}

int main()
{
float x,y;
cout<<"请输入两个参数:"< cin>>x>>y;
cout<<"当形参为指针时:"< cout<<"The max="< cout<<"当形参为引用时:"< cout<<"The max="< return 0;
}

回答3:

#include
#include
dedecms.com

using namespace std;
float compare(float *a,float &b) //a为指针,b为引用
{
if (*a>b) return *a;
else return b;
}
int main()
{
float x,y;
cin>>x>>y;
cout<<"The max="<return 0;
}