鱼C论坛

 找回密码
 立即注册
查看: 1379|回复: 1

[已解决]求助一下

[复制链接]
发表于 2023-11-11 14:27:32 | 显示全部楼层 |阅读模式

马上注册,结交更多好友,享用更多功能^_^

您需要 登录 才可以下载或查看,没有账号?立即注册

x
6-1 计算两个复数之积
分数 10
作者 张泳
单位 浙大城市学院
本题要求实现一个计算复数之积的简单函数。

函数接口定义:
struct complex multiply(struct complex x, struct complex y);
其中struct complex是复数结构体,其定义如下:

struct complex{
    int real;
    int imag;
};
裁判测试程序样例:
#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;
}

/* 你的代码将被嵌在这里 */
输入样例:
3 4 5 6
输出样例:
(3+4i) * (5+6i) = -9 + 38i

这个函数怎么写
最佳答案
2023-11-11 14:27:45
  1. #include <stdio.h>

  2. struct complex{
  3.     int real;
  4.     int imag;
  5. };

  6. struct complex multiply(struct complex x, struct complex y);

  7. int main()
  8. {
  9.     struct complex product, x, y;

  10.     scanf("%d%d%d%d", &x.real, &x.imag, &y.real, &y.imag);
  11.     product = multiply(x, y);
  12.     printf("(%d+%di) * (%d+%di) = %d + %di\n",
  13.            x.real, x.imag, y.real, y.imag, product.real, product.imag);
  14.    
  15.     return 0;
  16. }

  17. struct complex multiply(struct complex x, struct complex y) {
  18.     struct complex result;
  19.     result.real = x.real * y.real - x.imag * y.imag;
  20.     result.imag = x.real * y.imag + x.imag * y.real;
  21.     return result;
  22. }
复制代码
小甲鱼最新课程 -> https://ilovefishc.com
回复

使用道具 举报

发表于 2023-11-11 14:27:45 | 显示全部楼层    本楼为最佳答案   
  1. #include <stdio.h>

  2. struct complex{
  3.     int real;
  4.     int imag;
  5. };

  6. struct complex multiply(struct complex x, struct complex y);

  7. int main()
  8. {
  9.     struct complex product, x, y;

  10.     scanf("%d%d%d%d", &x.real, &x.imag, &y.real, &y.imag);
  11.     product = multiply(x, y);
  12.     printf("(%d+%di) * (%d+%di) = %d + %di\n",
  13.            x.real, x.imag, y.real, y.imag, product.real, product.imag);
  14.    
  15.     return 0;
  16. }

  17. struct complex multiply(struct complex x, struct complex y) {
  18.     struct complex result;
  19.     result.real = x.real * y.real - x.imag * y.imag;
  20.     result.imag = x.real * y.imag + x.imag * y.real;
  21.     return result;
  22. }
复制代码
小甲鱼最新课程 -> https://ilovefishc.com
回复 支持 反对

使用道具 举报

您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

小黑屋|手机版|Archiver|鱼C工作室 ( 粤ICP备18085999号-1 | 粤公网安备 44051102000585号)

GMT+8, 2025-4-22 09:32

Powered by Discuz! X3.4

© 2001-2023 Discuz! Team.

快速回复 返回顶部 返回列表