[YTU]_2437 (C++ 习题 比较大小-类模板)
生活随笔
收集整理的這篇文章主要介紹了
[YTU]_2437 (C++ 习题 比较大小-类模板)
小編覺得挺不錯的,現在分享給大家,幫大家做個參考.
題目描述
聲明一個類模板,利用它分別實現兩個整數、浮點數和字符的比較,求出大數和小數。說明:在類模板外定義各成員函數。
輸入
輸入兩個整數、兩個浮點數和兩個字符
輸出
從大到小輸出兩個整數、兩個浮點數和兩個字符
樣例輸入
3 7 45.78 93.6 a A樣例輸出
7 3 93.60 45.78 a A#include <iostream> #include <iomanip> using namespace std; template<class numtype> class Compare { public:Compare(numtype a,numtype b);numtype max();numtype min(); private:numtype x,y; }; template <class numtype> Compare<numtype>::Compare(numtype a,numtype b) {x=a;y=b; } template <class numtype> numtype Compare<numtype>::max() {return (x>y)?x:y; } template <class numtype> numtype Compare<numtype>::min() {return (x<y)?x:y; } int main() {int i1,i2;cin>>i1>>i2;Compare<int> cmp1(i1,i2);cout<<cmp1.max()<<" "<<cmp1.min()<<endl;float f1,f2;cin>>f1>>f2;Compare<float> cmp2(f1,f2);cout<<setiosflags(ios::fixed);cout<<setprecision(2);cout<<cmp2.max()<<" "<<cmp2.min()<<endl;char c1,c2;cin>>c1>>c2;Compare<char> cmp3(c1,c2);cout<<cmp3.max()<<" "<<cmp3.min()<<endl;return 0; }總結
以上是生活随笔為你收集整理的[YTU]_2437 (C++ 习题 比较大小-类模板)的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: [YTU]_2435 ( C++ 习题
- 下一篇: [YTU]_2618 ( B 求类中数据