c++分别对三个整数,三个实数,三个字符从小到大排序,用重载函数实现

如题所述

利用C++重载函数、引用和冒泡排序分别实现三个整数、三个实数、三个字符的升序排序,参考代码如下:

#include<iostream>
using namespace std;
void sort(int &a,int &b,int &c)
{
int t;
if(a>b)t=a,a=b,b=t;
if(b>c)t=b,b=c,c=t;
if(a>b)t=a,a=b,b=t;
}
void sort(float &a,float &b,float &c)
{
float t;
if(a>b)t=a,a=b,b=t;
if(b>c)t=b,b=c,c=t;
if(a>b)t=a,a=b,b=t;
}
void sort(char &a,char &b,char &c)
{
char t;
if(a>b)t=a,a=b,b=t;
if(b>c)t=b,b=c,c=t;
if(a>b)t=a,a=b,b=t;
}
int main(){
int a,b,c;
cin>>a>>b>>c;
sort(a,b,c);
cout<<a<<" "<<b<<" "<<c<<endl;
float x,y,z;
cin>>x>>y>>z;
sort(x,y,z);
cout<<x<<" "<<y<<" "<<z<<endl;
cin.get();
char l,m,n;
cin>>l>>m>>n;
sort(l,m,n);
cout<<l<<" "<<m<<" "<<n<<endl;
return 0;
}

温馨提示:答案为网友推荐,仅供参考
第1个回答  2019-04-01
#include int max(int x1, int x2){ return (x1>x2)?x1:x2; int max(int x1,int x2,int x3){ int y = max(x1,x2); return (y>x3)? y:x3;} double max(double f1, double f2){ return (f1>f2)?f1:f2;} double max(double x1,double x2,double x3){ double y = max(x1,x2); return (y>x3)? y:x3;} int main() { int x1=1, x2=3, x3=2; printf("max(%d,%d)= %d\n", x1, x2, max(x1, x2)); printf("max(%d,%d, %d)= %d\n", x1, x2, x3,max(x1, x2,x3)); double d1=93.1, d2=99.1, d3=70.0; printf("max(%.1lf,%.1lf)= %.1lf\n",d1, d2, max(d1, d2)); printf("max(%.1lf,%.1lf, %.1lf)= %.1lf\n", d1, d2,d3,max(d1, d2,d3)); return 0; }追问

不要瞎答啊。。

相似回答
大家正在搜