| 网站首页 | 业界新闻 | 小组 | 威客 | 人才 | 下载频道 | 博客 | 代码贴 | 在线编程 | 编程论坛
欢迎加入我们,一同切磋技术
用户名:   
 
密 码:  
共有 2450 人关注过本帖
标题:如何用重载函数求最大值
只看楼主 加入收藏
roje123
Rank: 1
等 级:新手上路
帖 子:9
专家分:0
注 册:2010-6-22
结帖率:100%
收藏
 问题点数:0 回复次数:5 
如何用重载函数求最大值
设计一个重载函数,求n个数的最大值(n=2, 3),并编程调用验证

如果能用C++做的最后也做出来
搜索更多相关主题的帖子: 最大值 函数 重载 
2010-06-23 01:35
xiaovs007
Rank: 2
来 自:青岛
等 级:论坛游民
帖 子:48
专家分:88
注 册:2010-5-16
收藏
得分:0 
#include "iostream"
using namespace std;
template <typename T>//用函数模板写的,呵呵,感觉重载代码量有点多,本人能力有限,你看看对你有用吗。
T max(T a,T b)
{
    if(a<b) a=b;
    return a;
}
template <typename T>
T max(T a,T b,T c)
{
    if(a<b) a=b;
    if(a<c) a=c;
    return a;
}
int main()
{
    int i_1,i_2,i_3;
    float f_1,f_2,f_3;
    double d_1,d_2,d_3;
    int i_a,i_b;
    float f_a,f_b;
    double d_a,d_b;
    cout<<"Please input two int:"<<endl;
    cin>>i_a>>i_b;
    cout<<"the max of two int is:"<<max(i_a,i_b)<<endl;
    cout<<"Please input two float:"<<endl;
    cin>>f_a>>f_b;
    cout<<"the max of two float is:"<<max(f_a,f_b)<<endl;
    cout<<"Please input two double:"<<endl;
    cin>>d_a>>d_b;
    cout<<"the max of two double is:"<<max(d_a,d_b)<<endl;
     cout<<"Please input three int:"<<endl;
    cin>>i_1>>i_2>>i_3;
    cout<<"the max of three int is:"<<max(i_1,i_2,i_3)<<endl;
    cout<<"Please input three float:"<<endl;
    cin>>f_1>>f_2>>f_3;
    cout<<"the max of three float is:"<<max(f_1,f_2,f_3)<<endl;
    cout<<"Please input three double:"<<endl;
    cin>>d_1>>d_2>>d_3;
    cout<<"the max of three double is:"<<max(d_1,d_2,d_3)<<endl;
   
    return 0;
}
2010-06-23 18:39
sunkaidong
Rank: 4
来 自:南京师范大学
等 级:贵宾
威 望:12
帖 子:4496
专家分:141
注 册:2006-12-28
收藏
得分:0 
求作业?至少也要谢谢二楼吧?呵呵

学习需要安静。。海盗要重新来过。。
2010-06-23 19:08
BlueGuy
Rank: 16Rank: 16Rank: 16Rank: 16
等 级:版主
威 望:29
帖 子:4476
专家分:4055
注 册:2009-4-18
收藏
得分:0 
回复 3楼 sunkaidong
最近心情不错吧,/

我就是真命天子,顺我者生,逆我者死!
2010-06-23 20:57
南国利剑
Rank: 12Rank: 12Rank: 12
等 级:贵宾
威 望:29
帖 子:1165
专家分:3536
注 册:2010-4-12
收藏
得分:0 
晚辈,向楼上两位问好!

南国利剑
2010-06-23 22:48
roje123
Rank: 1
等 级:新手上路
帖 子:9
专家分:0
注 册:2010-6-22
收藏
得分:0 
谢谢啦,各位热心人氏
2010-06-23 23:51
快速回复:如何用重载函数求最大值
数据加载中...
 
   



关于我们 | 广告合作 | 编程中国 | 清除Cookies | TOP | 手机版

编程中国 版权所有,并保留所有权利。
Powered by Discuz, Processed in 0.031525 second(s), 8 queries.
Copyright©2004-2024, BCCN.NET, All Rights Reserved