/*
题目:输入三个整数x,y,z,请把这三个数由小到大输出。
1.程序分析:选用运算符号<和>重载函数分别算出最大值和最小值
然后再判断中间值
by adengou 2011.8.13
win7 dev c++5.0 and vs 2010 通过
*/
#include <iostream>
using namespace std;
class Cmax
{
private :
double x;
public:
Cmax(double a=0.0){x=a;}
//double val(){return x;}
/*求小值,运算符重载函数*/
friend Cmax operator<(Cmax c1,Cmax c2)
{
return(c1.x<c2.x?c1:c2);
}
/*求大值,运算符重载函数*/
friend Cmax operator>(Cmax c1,Cmax c2 )
{
return(c1.x>c2.x?c1:c2);
}
void min_max(Cmax c1,Cmax c2,Cmax c3);
};
void Cmax::min_max(Cmax c1,Cmax c2,Cmax c3)
{
int i;
double arrayNum[]={c1.x,c2.x,c3.x};
double *pt;pt=arrayNum;
Cmax min,max,mid;
min=c1<c2<c3;
max=c3>c2>c1;
for(i=0;i<3;i++){if(*(pt+i)!=min.x &&*(pt+i)!=max.x){mid.x=*(pt+i);}}
cout<<c1.x<<"、"<<c2.x<<"、"<<c3.x<<"三个数从小到大排序是:"<<min.x<<" "<<mid.x<<" "<<max.x<<endl;
};
int main()
{ double v,m,n;
cout<<"please input thtee numbers:\n"; cin>>v>>m>>n;
Cmax A(v),B(m),C(n),D;
D.min_max(A,B,C);//从小到大排序函数
system("pause");
return 0;
}
本文介绍了一个使用C++实现的程序,该程序通过重载运算符来确定三个输入整数的大小顺序,并将它们按从小到大的顺序输出。文章提供了一个完整的代码示例,展示了如何定义类、重载比较运算符以及实现排序逻辑。
1万+

被折叠的 条评论
为什么被折叠?



