[cpp]
#include <iostream>
using namespace std;
void jiaohuan(int x, int y);
int main(void)
{ www.2cto.com
int a,b;
cin>>a>>b;
if (a<b)
jiaohuan(a, b);
cout<<"a,b="<<a<<","<<b;
return 0;
}
void jiaohuan(int x, int y)
{
int t;//新定義一個數,用來保存數
t=x; //將x保存在t內
x=y;
y=t;
}
//該程序能夠實現互換
<pre class="cpp" name="code">#include <iostream>
using namespace std;
void jiaohuan(int *x, int *y);//形式參數為指針
int main(void)
{
int a,b;
cin>>a>>b;
if (a<b)
jiaohuan(&a, &b);
cout<<"a,b="<<a<<","<<b;
return 0;
}
void jiaohuan(int *x, int *y)
{
int t;//定義一個新的數
t=*x;//將指針x保存在t內
*x=*y; //y的指針復制給x
*y=t;//指針x賦值給指針y
}
//該程序能夠實現兩數互換</pre><pre class="cpp" name="code"><pre class="cpp" name="code">#include <iostream>
using namespace std;
void jiaohuan(int &x, int &y);//形式參數為引用x的值
int main(void)
{
int a,b;
cin>>a>>b;
if (a<b)
jiaohuan(a, b); //引用a,b的值
cout<<"a,b="<<a<<","<<b;
return 0;
}
void jiaohuan(int &x, int &y)
{
int t;
t=x; //x的值保存在t內
x=y; //y的值復制給x
y=t;//由t保存的x的值賦值給y
}
//該程序能夠實現兩數互換</pre><br>
<pre></pre>
<pre></pre>
</pre>