17周课后自主-项目二-引用作形参

来源:互联网 发布:js获取id的css样式 编辑:程序博客网 时间:2024/06/06 19:40

#include<iostream>using namespace std;//函数原型void pSort(int *,int *,int *);void rSort(int &,int &,int &);void swap_(int &,int &);int main(){    int a,b,c;    cin>>a>>b>>c;    pSort(&a,&b,&c);    cout<<a<<" "<<b<<" "<<c<<endl;    cin>>a>>b>>c;    rSort(a,b,c);    cout<<a<<" "<<b<<" "<<c<<endl;}//指针实现void pSort(int* x,int* y,int* z){    int* t;    if(*x > *y)    {        swap_(*x,*y);    }    if(*y >*z);    {        swap_(*y,*z);    }    if(*x > *y)    {        swap_(*x,*y);    }}//引用实现void rSort(int &x,int &y,int &z){    int t;    if(x > y)    {        swap_(x,y);    }    if(y > z)    {        swap_(y,z);    }    if(x > y)    {        swap_(x,y);    }}//交换函数,用引用实现void swap_(int &a,int &b){    int t;    t = a;    a = b;    b = t;}

运行结果



0 0
原创粉丝点击