简单应用
#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;
}