C语言程序设计——结构体的运用 求复数之积。利用结构变量求解如下两组复数之积。
时间:2022-09-30 17:30:00
求复数的积累。利用结构变量解决以下两组复数的积累。
za={3,4}, zb={5,6}
za={10,20}, zb={30,40}
**输出格式要求:"(%d %di)*(%d %di)=" "(%d %di)\n"
程序运行示例如下:
(3 4i)*(5 6i)=(-9 38i)
(10 20i)*(30 40i)=(-500 1000i)
#include "stdio.h" struct complx { int real; int im; } ; main() { static struct complx za={3,4}; static struct complx zb={5,6}; struct complx x,y,z; struct complx cmult(); void cpr(); z=cmult(za,zb); cpr(za,zb,z); x.real = 10; x.im = 20; y.real = 30; y.im = 40; z=cmult(x,y); cpr(x,y,z); } struct complx cmult(struct complx za, struct complx zb) { struct complx w; w.real = za.real*zb.real - za.im*zb.im; w.im = za.real*zb.im za.im*zb.real; return w; } void cpr(struct complx za, struct complx zb, struct complx z) { printf("(%d %di)*(%d %di)=",za.real,za.im,zb.real,zb.im); printf("(%d %di)\n",z.real,z.im); }