复数乘法中的结构体赋值实现代码

时间:2021-10-01 23:56:37

废话不多说,直接上代码

复制代码 代码如下:


#include <iostream>
using namespace std;

 

typedef struct
{
 double real;
 double imag;
} complex;


//复数乘法
complex X_complex(complex a, complex b)
{
 complex temp;

 temp.real = a.real * b.real - a.imag * b.imag;
 temp.imag = b.imag * a.real + a.imag * b.real;

 return temp;
}

int main(int argc, char *argv[])
{
 complex a,b,c;

 a.real = 2;
 a.imag = 3;
 b.real = 4;
 b.imag = 5;

 c = X_complex(a,b);//结构体可以作为返回值,然后赋值给另一个相同结构体的变量
 cout<<c.real<<","<<c.imag<<endl;

 return 0;
}