运算符重载函数应用---从小到大排序三个数

/*
题目:输入三个整数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;
}

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值