27、模板类练习:实现两个整数、浮点数和字符的比较

问题描述 :

请定义一个类模板,利用它分别实现两个整数、浮点数和字符串的比较。在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;
}

 

  • 1
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值