實驗2.3 使用重載函數模板重新實現上小題中的函數Max1

題目

使用重載函數模板重新可分別求取兩個整數,三個整數,兩個雙精度數,三個雙精度數的最大值

AC的C++代碼如下:

#include<iostream>
#include<algorithm>
using namespace std;

template <class T>
T Max1(T a,T b){
	return max(a,b);
}

template <class T>
T Max1(T a,T b,T c){
	return max(max(a,b),c);
}

int main()
{
	int x,y,z;
	//兩個整數 
	cout<<"Please enter two integer:\n";
	cin>>x>>y;
	cout<<Max1(x,y)<<endl;
	//三個整數 
	cout<<"Please enter three integer:\n";
	cin>>x>>y>>z;
	cout<<Max1(x,y,z)<<endl;
	
	double a,b,c;
	//兩個雙精度數 
	cout<<"Please enter two double precision numbers:\n";
	cin>>a>>b;
	cout<<Max1(a,b)<<endl;
	//三個雙精度數 
	cout<<"Please enter three double precision numbers:\n";
	cin>>a>>b>>c;
	cout<<Max1(a,b,c)<<endl;
	
	return 0;
}
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章