(C++实例)编写重载函数Max1分别求取两个整数,三个整数,两个双精度数,三个双精度数的最大值。

该博客展示了如何使用C++编写重载函数来求取不同数量整数和双精度数的最大值。通过四个不同的Max1函数,分别处理两个整数、三个整数、两个双精度数和三个双精度数的情况。在main函数中,对每个函数进行了测试并从用户输入获取数值,然后输出最大值。
摘要由CSDN通过智能技术生成

问题引入

        编写重载函数Max1分别求取两个整数,三个整数,两个双精度数,三个双精度数的最大值。

代码实现

#include<iostream>
#include<math.h>
using namespace std;

//求两个整数最大值 
int Max1(int a,int b){
	if(a>b){
		return a;
	}else{
		return b;
	}
}
//求三个整数最大值
int Max1(int a,int b,int c){
	int temp=a;
	if(temp<b){
		temp=b;
	}
	if(temp<c){
		temp=c;
	}
	return temp;
} 
//求两个双精度整数最大值
double Max1(double a,double b){
	if(a>b){
		return a;
	}else{
		return b;
	}
}
//求三个双精度整数最大值
double Max1(double a,double b,double c){
	double temp=a;
	if(temp<b){
		temp=b;
	}
	if(temp<c){
		temp=c;
	}
	return temp;
}

int main(){
	int u,v,w; double a,b,c;
	//2.1求两个整数最大值 
	cout<<"求两个整数最大值,请分别输入数u、数v:"<<endl;
	cout<<"请输入数 u:";
	cin>>u;
	cout<<"请输入数 v:";
	cin>>v;
	cout<<u<<"和"<<v<<"中最大值为:"<<Max1(u,v)<<endl<<endl;
	//2.2求三个整数最大值
	cout<<"求三个整数最大值,请分别输入数u、数v、数w:"<<endl;
	cout<<"请输入数 u:";
	cin>>u;
	cout<<"请输入数 v:";
	cin>>v;
	cout<<"请输入数 w:";
	cin>>w;
	cout<<u<<"和"<<v<<"和"<<w<<"中最大值为:"<<Max1(u,v,w)<<endl<<endl;
	//2.3求两个双精度整数最大值
	cout<<"求两个双精度整数最大值,请分别输入数a、数b:"<<endl;
	cout<<"请输入数 a:";
	cin>>a;
	cout<<"请输入数 b:";
	cin>>b;
	cout<<a<<"和"<<b<<"中最大值为:"<<Max1(a,b)<<endl<<endl;
	//2.4求三个双精度整数最大值
	cout<<"求三个双精度整数最大值,请分别输入数a、数b、数c:"<<endl;
	cout<<"请输入数 a:";
	cin>>a;
	cout<<"请输入数 b:";
	cin>>b;
	cout<<"请输入数 c:";
	cin>>c;
	cout<<a<<"和"<<b<<"和"<<c<<"中最大值为:"<<Max1(a,b,c)<<endl<<endl; 
}

运行结果

 

  • 26
    点赞
  • 107
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 2
    评论
评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

等日出看彩虹

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值