天天看點

YTU 2618: B 求類中資料成員的最大值-類模闆

2618: B 求類中資料成員的最大值-類模闆

時間限制: 1 Sec  

記憶體限制: 128 MB

送出: 430  

解決: 300

題目描述

聲明一個類模闆,類模闆中有三個相同類型的資料成員,有一函數來擷取這三個資料成員的最大值。

類模闆聲明如下:

template<class numtype>
 class Max
 {
 public:
 Max(numtype a,numtype b,numtype c);
 numtype getMax();
 private:
 numtype x,y,z;
 };
 請在下面的程式段基礎上完成整個設計:
 #include <iostream>
 #include <iomanip>
 using namespace std; template<class numtype>
 class Max
 { public:
 Max(numtype a,numtype b,numtype c);
 numtype getMax();
 private:
 numtype x,y,z;
 };
//将程式需要的其他成份寫在下面,隻送出begin到end部分的代碼
 //******************** begin ******************** //********************* end ********************
 int main()
 {
 int i1,i2,i3;
 cin>>i1>>i2>>i3;
 Max<int> max1(i1,i2,i3);
 cout<<max1.getMax()<<endl;
 float f1,f2,f3;
 cin>>f1>>f2>>f3;
 Max<float> max2(f1,f2,f3);
 cout<<setiosflags(ios::fixed);
 cout<<setprecision(2);
 cout<<max2.getMax()<<endl;
 char c1,c2,c3;
 cin>>c1>>c2>>c3;
 Max<char> max3(c1,c2,c3);
 cout<<max3.getMax()<<endl;
 return 0;
 }      

輸入

分别輸入3個整數,3個浮點數,3個字元

輸出

 3個整數的最大值

3個浮點數中的最大值

3個字元中的最大值

樣例輸入

9 5 6
1.1 3.4 0.9
a b c      

樣例輸出

9
3.40
c      

提示

在類模闆外定義各成員函數。

隻送出begin到end部分的代碼。

迷失在幽谷中的鳥兒,獨自飛翔在這偌大的天地間,卻不知自己該飛往何方……

#include <iostream>
#include <iomanip>
using namespace std;
template<class numtype>
class Max
{
public:
    Max(numtype a,numtype b,numtype c);
    numtype getMax();
private:
    numtype x,y,z;
};
template<class numtype>
Max<numtype>::Max(numtype a,numtype b,numtype c)
{
    x=a;
    y=b;
    z=c;
}
template<class numtype>
numtype Max<numtype>::getMax()
{
    return (x>y&&x>z)?x:(y>x&&y>z)?y:z;
}
int main()
{
    int i1,i2,i3;
    cin>>i1>>i2>>i3;
    Max<int> max1(i1,i2,i3);
    cout<<max1.getMax()<<endl;
    float f1,f2,f3;
    cin>>f1>>f2>>f3;
    Max<float> max2(f1,f2,f3);
    cout<<setiosflags(ios::fixed);
    cout<<setprecision(2);
    cout<<max2.getMax()<<endl;
    char c1,c2,c3;
    cin>>c1>>c2>>c3;
    Max<char> max3(c1,c2,c3);
    cout<<max3.getMax()<<endl;
    return 0;
}