习题9-2 计算两个复数之积
来源https://pintia.cn/problem-sets/12/problems/348
要求实现一个计算复数之积的简单函数。
代码如下
#include <stdio.h>
struct complex{
int real;
int imag;
};
struct complex multiply(struct complex x, struct complex y);
int main()
{
struct complex product, x, y;
scanf("%d%d%d%d", &x.real, &x.imag, &y.real, &y.imag);
product = multiply(x, y);
printf("(%d+%di) * (%d+%di) = %d + %di\n",
x.real, x.imag, y.real, y.imag, product.real, product.imag);
return 0;
}
struct complex multiply(struct complex x, struct complex y)
{
struct complex product;//需要建立一个新的struct
int a = x.real*y.real;
int b = -x.imag*y.imag;
product.real = a+b;
int c = x.real*y.imag;
int d = x.imag*y.real;
product.imag = c+d;
return product;//返回这个新的struct
}
初学代码,如有建议,不吝赐教