📄 p5_8.cpp
字号:
/**********************************************
* p5_8.cpp *
* 实现两个数据交换的通用程序 *
***********************************************/
#include <iostream>
using namespace std;
void swap_i(int *num1,int *num2) //整型数交换
{
int t;
t=*num1;
*num1=*num2;
*num2=t;
}
void swap(void *num1,void *num2,int size) // 所有类型数据交换
{
char *first=(char *)num1,*second=(char *)num2;
for(int k=0;k<size;k++)
{
char temp;
temp=first[k];
first[k]=second[k];
second[k]=temp;
}
}
void main()
{
int a=3,b=6;
double x=2.3,y=4.5;
char c1[8]="John",c2[8]="Antony";
cout<<"before swap:a="<<a<<" b="<<b<<endl;
swap_i(&a,&b);
cout<<"after swap: a="<<a<<" b="<<b<<endl;
cout<<"before swap:x="<<x<<" y="<<y<<endl;
swap(&x,&y,sizeof(x));
cout<<"after swap: x="<<x<<" y="<<y<<endl;
cout<<"before swap:c1="<<c1<<" c2="<<c2<<endl;
swap(&c1,&c2,sizeof(c1));
cout<<"after swap: c1="<<c1<<" c2="<<c2<<endl;
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -