问题描述 :
请定义一个类模板,利用它分别实现两个整数、浮点数和字符串的比较。在main函数中应用并测试之。
输入说明 :
每组输出分3行:
第一行:标志行('i'代表输入两个整数进行比较;'f'代表输入两个浮点数进行比较;'s'代表输入两个字符串进行比较;'e'代表输入结束。)
第二行和第三行分别对应进行比较的两个数据
输入行之间无多余的空行,每行的开头和结尾无多余的空格
输入可连续进行。
输出说明 :
每组输入数据对应一行输出数据,为2个比较数据之中的较大者。
组与组的输出行之间无多余的空行,每行的开头和结尾无多余的空格。
输入范例 :
i
212
323
f
1.1
0.51
s
China
china
i
3243
253
e
输出范例 :
323
1.1
china
3243
解题代码:
#include<iostream>
#include<cstdio>
#include<cstdlib>
#include<cmath>
#include<cstring>
#include<string>
#include<map>
#include<algorithm>
#include<set>
#include<vector>
#include<queue>
using namespace std;
class check {
private:
int a1, a2;
string s1, s2;
double f1, f2;
int p;
public:
check();
void getin(int a, int b);
void getin(double a, double b);
void getin(string a, string b);
int huge1();
double huge2();
string huge3();
};
check::check()
{
;
}
void check::getin(int a, int b)
{
a1 = a;
a2 = b;
p = 1;
}
void check::getin(double a, double b)
{
f1 = a;
f2 = b;
p = 2;
}
void check::getin(string a, string b)
{
s1 = a;
s2 = b;
p = 3;
}
int check::huge1()
{
return max(a1, a2);
}
double check::huge2()
{
return max(f1, f2);
}
string check::huge3()
{
return max(s1, s2);
}
int main()
{
char swit;
check w;
while (cin >> swit)
{
if (swit == 'e')
break;
if (swit == 'i')
{
int x, y;
cin >> x >> y;
w.getin(x, y);
cout << w.huge1()<<endl;
continue;
}
if (swit == 'f')
{
double x, y;
cin >> x >> y;
w.getin(x, y);
cout<<w.huge2() << endl;
continue;
}
if (swit == 's')
{
string x, y;
cin >> x >> y;
w.getin(x, y);
cout << w.huge3() << endl;
continue;
}
}
return 0;
}