Template 笔记

简单应用

#include <iostream>
#include<string>
using namespace std;
int n;
int a, b, c;
double a1, b1, c1;
char a2, b2, c2;
string a3, b3, c3;
template<class T>//每次使用类模板都要带这句
T TwoSmallest(T a, T b, T c, T& s1, T& s2)
{
	if (a > b)
	{
		s1 = b;
		s2 = a;
	}
	else
	{
		s1 = a;
		s2 = b;
	}
	if (c < s2&&c!=s1)
	{
		s2 = c;
		if (c < s1)
		{
			s2 = s1;
			s1 = c;
		}
		
	}
	return s1;
}
void st()
{
	cout << "*******************"<<endl;
	cout << "   1. int"<<endl;
	cout << "   2. double"<<endl;
	cout << "   3. char" << endl;
	cout << "   4. string" << endl;
	cout << "   0. exit" << endl;
	cout << "*******************" << endl;
	cout << "Inout choice: ";
}
template<class S>
void show(S s1,S s2)
{
	cout << endl << "The smallest number : " << s1;
	cout << endl << "The second smallest number : " << s2<<endl;
}
int main()
{
	
	st();
	cin >> n;
	while (n)
	{

		if (n == 1)
		{
			cout  << "Int1: ";
			cin >> a;
			cout << "Int2: ";
			cin >> b;
			cout << "Int3: ";
			cin >> c;
			int s1, s2;
			TwoSmallest(a, b, c, s1, s2);
		    show(s1,s2);
		}
		if (n == 2)
		{
			cout  << "Double1: ";
			cin >> a1;
			cout  << "Double2: ";
			cin >> b1;
			cout  << "Double3: ";
			cin >> c1;
			double s12, s22;
			TwoSmallest(a1, b1, c1, s12, s22);
			show(s12, s22);
		}
		if (n == 3)
		{
			cout  << "Char1: ";
			cin >> a2;
			cout  << "Char2: ";
			cin >> b2;
			cout  << "Char3: ";
			cin >> c2;
			char s13, s23;
			TwoSmallest(a2, b2, c2, s13, s23);
			show(s13, s23);
		}
		if (n == 4)
		{
			cout  << "String1: ";
			cin >> a3;
			cout  << "String2: ";
			cin >> b3;
			cout  << "String3: ";
			cin >> c3;
			string s14, s24;
			TwoSmallest(a3, b3, c3, s14, s24);
			show(s14, s24);
		}
		if (n == 0) break;
		st();
		cin >> n;
	}
	return 0;
}

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

ItsNorth

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值