整数交换

//整数交换
#include
using namespace std;
void change1(int &a,int &b);
void change2(int *p1,int *p2);
void change3();int a,b;
int main()
{
 
 int *p1,*p2;p1=&a;p2=&b;
 cout<<"请输入两个整数:";
 cin>>a>>b;
 cout<<"利用引用将两数进行交换"<<endl;
 change1(a,b);
 cout<<a<<" "<<b<<endl;
 cout<<"利用指针将两数进行交换"<<endl;
 change2(&a,&b);
 cout<<a<<" "<<b<<endl;
 cout<<"直接交换"<<endl;
 change3();
 cout<<a<<" "<<b<<endl;
 return 0;
}
void change1(int &a,int &b)
{
 int temp;
 temp=a;
 a=b;
 b=temp;
}
void change2(int *p1,int *p2)
{
   int temp;
   temp=*p1;
   *p1=*p2;
   *p2=temp;
}
void change3()
{
 int temp;
 temp=a;
 a=b;
 b=temp;
}

posted on 2012-12-07 23:05  木本  阅读(151)  评论(0编辑  收藏  举报

导航