搜索
您的当前位置:首页正文

求三个数当中最大的数

来源:二三娱乐

程序代码

#include <iostream>
using namespace std;
int max(int a,int b,int c){
    if(b>a) a=b;
    if(c>a) a=c;
    return a;
}
float max(float a,float b,float c){
    if(b>a) a=b;
    if(c>a) a=c;
    return a;
}
long max(long a,long b,long c){
    if(b>a) a=b;
    if(c>a) a=c;
    return a;
}

int main(){
    int a,b,c;
    float d,e,f;
    long g,h,i;
    cin >>a>>b>>c;
    cin >>d>>e>>f;
    cin >>g>>h>>i;

    int m;
    m=max(a,b,c);
    cout <<"max_i="<<m<<endl;
    float n;
    n=max(d,e,f);
    cout <<"max_f="<<n<<endl;
    long int p;
    p=max(g,h,i);
    cout <<"max_l"<<p<<endl;
}

执行结果

执行结果

代码分析

1.在上述的例子当中我们看到max函数名字被使用了很多次,在c++当中,只要函数声明的类型不同,就可以重复使用函数的名字定义函数,在一定的作用域当中一个函数名称定义多个函数,这叫做函数的重载。

2.main函数三次调用了max函数,系统会根据实参的类型自动去寻找与之对应的函数。

Top