写一个互换函数,在主程序中调用该函数,来实现实参值的交换
写一个交换函数,在主程序中调用该函数,来实现实参值的交换。
#include<iostream>
using namespace std;
int main()
{
void swap(int*x,int*y);
int a,b;
int *p_1,*p_2;
cout<<"请输入两个数:";
cin>>a>>b;
cout<<"交换前:";
cout<<a<<","<<b<<endl;
p_1=&a;
p_2=&b;
swap(p_1,p_2);
cout<<"交换后:";
cout<<a<<","<<b<<endl;
return 0;
}
void swap(int*x,int*y)
{
int temp;
temp=*x;
*x=*y;
*y=temp;
}
#include<iostream>
using namespace std;
int main()
{
void swap(int*x,int*y);
int a,b;
int *p_1,*p_2;
cout<<"请输入两个数:";
cin>>a>>b;
cout<<"交换前:";
cout<<a<<","<<b<<endl;
p_1=&a;
p_2=&b;
swap(p_1,p_2);
cout<<"交换后:";
cout<<a<<","<<b<<endl;
return 0;
}
void swap(int*x,int*y)
{
int temp;
temp=*x;
*x=*y;
*y=temp;
}