求教大佬 如何实现C语言结构体内容交换?
课程设计
1
#include<stdio.h>
#include<stdlib.h>
#include<math.h>
struct complex
{
double r,i;
complex() {r=0,i=0;}
complex(double r1,double i1): r(r1),i(i1){}
};
void swap(complax*a ,complax*b)
{
}
int main()
{
complex a1(1,2) , a2(4,6);
swap(a1,a2)
return 0
}
想通过swap交换a1 a2 的值
帮忙实现一下swap函数
-
void swap(compex *a, complex*b){ double tr, ti; tr = a->r; ti = a->i; a->r = b->r; a->i = b->i; b->r = tr; b->i = ti; }
-
void swap(complex &a, complex &b) { complex tmp; tmp = a; a = b; b = tmp; }
tmp是备份变量,利用它交换两个结构体其实和交换两个变量也是一样的,只不过不能在数值上操作。
-
void swap(complex &a, complex &b) { complex temp; temp = a; a = b; b = temp; }
-
void swap(complex &a, complex &b) { complex temp = a; a = b; b = temp; }
发表回复