YTU-OJ-Problem L: 熟悉题型——自由设计(比较大小-类模板)

Problem L: 熟悉题型——自由设计(比较大小-类模板)

Time Limit: 1 Sec   Memory Limit: 128 MB
Submit: 94   Solved: 79
[ Submit][ Status][ Web Board]

Description

声明一个类模板,利用它分别实现两个整数、浮点数和字符的比较,求出大数和小数。说明:在类模板外定义各成员函数。

Input

输入两个整数、两个浮点数和两个字符

Output

从大到小输出两个整数、两个浮点数和两个字符

Sample Input

3 7
45.78 93.6
a A

Sample Output

7 3
93.60 45.78
a A

HINT

前置代码及类型定义已给定如下,提交时不需要包含,会自动添加到程序前部



/* C++代码 */



#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;



};











主函数已给定如下,提交时不需要包含,会自动添加到程序尾部



/* C++代码 */



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;



}


#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;
}
/* C++代码 */
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;
}


你可能感兴趣的:(C++,设计)