c++程序怎么用函数重载,实现两个整数和三个浮点数的排序,并按照从小到大的顺序将排序结果输出?

如题所述

第1个回答  2018-02-02
#include <iostream>
using namespace std;
void sort(int &a,int &b)
{
int temp;
if (a>b)
{
temp = a;
a = b;
b = temp;
}
}
void sort(float &a,float &b,float &c)
{
int temp[3];
if (b>a)
{
temp[0] = a;
temp[1] = b;
if (c>b)
temp[2] = c;
else
{
if (c<a)
{
temp[0] = c;
temp[1] = a;
temp[2] = b;
}
else
{
temp[1] = c;
temp[2] = b;
}

}
}
else
{
temp[0] = b;
temp[1] = a;
if (a>c)
{
if (b>c)
{
temp[0] = c;
temp[1] = b;
temp[2] = a;
}
else
{
temp[1] = c;
temp[2] = a;
}
}
else
temp[2] = c;
}
a = temp[0];
b = temp[1];
c = temp[2];
}
int main()
{
int a,b;
float c,d,e;
cout<<"请输入两个整数:"<<endl;
cin>>a>>b;
sort(a,b);
cout<<"排序之后:"<<a<<"\t"<<b<<"\t"<<endl;
cout<<"请输入三个浮点数:"<<endl;
cin>>c>>d>>e;
sort(c,d,e);
cout<<"排序之后:"<<c<<"\t"<<d<<"\t"<<e<<"\t"<<endl;
return 0;
}本回答被网友采纳
相似回答