用vs编写c++重载函数maxl可以分别求两个整数,三个整数,两个三精度,两个双精度的最大值。

谁能帮忙把主程序写一下,我实在是不会了...编写4个重载函数
2024-12-03 10:59:57
推荐回答(3个)
回答(1):

#include

int max(int x1, int x2){ return (x1>x2)?x1:x2;

int max(int x1,int x2,int x3){ int y = max(x1,x2); return (y>x3)? y:x3;}

double max(double f1, double f2){ return (f1>f2)?f1:f2;}

double max(double x1,double x2,double x3){ double y = max(x1,x2); return (y>x3)? y:x3;}

int main()

{ int x1=1, x2=3, x3=2;

printf("max(%d,%d)= %d\n", x1, x2, max(x1, x2));

printf("max(%d,%d, %d)= %d\n", x1, x2, x3,max(x1, x2,x3));

double d1=93.1, d2=99.1, d3=70.0;

printf("max(%.1lf,%.1lf)= %.1lf\n",d1, d2, max(d1, d2));

printf("max(%.1lf,%.1lf, %.1lf)= %.1lf\n", d1, d2,d3,max(d1, d2,d3));

return 0;

}

二、

c++编写

#include

using namespace std;

int Max1(int a,int b){

if(a>b)

return a;

else 

return b;

}

double Max1(double x,double y){

if(x>y)

return x;

else 

return y;

}

int Max1(int a,int b,int c){

return Max1(a,Max1(b,c));

}

double Max1(double x,double y,double z){

return Max1(x,Max1(y,z));

}

int main(){

int a,b,c;

cout<<"please enter two integer:";

cin>>a>>b;

cout<<"there is the answer:"<

cout<<"please enter three integer:";

cin>>a>>b>>c;

cout<<"there is the answer:"<

double x,y,z;

cout<<"please enter two numder:";

cin>>x>>y;

cout<<"there is the answer:"<

cout<<"please enter three number:";

cin>>x>>y>>z;

cout<<"there is the answer:"<

return 0;

}

扩展资料:

C++运算符重载的相关规定如下:

(1)不能改变运算符的优先级;

(2)不能改变运算符的结合型;

(3)默认参数不能和重载的运算符一起使用;

(4)不能改变运算符的操作数的个数;

(5)不能创建新的运算符,只有已有运算符可以被重载;

(6)运算符作用于C++内部提供的数据类型时,原来含义保持不变。

参考资料来源:百度百科-重载函数

回答(2):

#include

int max(int x1, int x2){ return (x1>x2)?x1:x2;
int max(int x1,int x2,int x3){ int y = max(x1,x2); return (y>x3)? y:x3;}
double max(double f1, double f2){ return (f1>f2)?f1:f2;}
double max(double x1,double x2,double x3){ double y = max(x1,x2); return (y>x3)? y:x3;}
int main()
{ int x1=1, x2=3, x3=2;
printf("max(%d,%d)= %d\n", x1, x2, max(x1, x2));
printf("max(%d,%d, %d)= %d\n", x1, x2, x3,max(x1, x2,x3));

double d1=93.1, d2=99.1, d3=70.0;
printf("max(%.1lf,%.1lf)= %.1lf\n",d1, d2, max(d1, d2));
printf("max(%.1lf,%.1lf, %.1lf)= %.1lf\n", d1, d2,d3,max(d1, d2,d3));

return 0;
}

回答(3):