繁体   English   中英

不能对“双”数进行乘法运算

[英]can't do multiplication operation on 'double' numbers

我正在尝试为复杂类型数字编写一些函数,但无法完成这项工作。

#include<stdio.h>
#include<stdlib.h>
#include<math.h>

typedef struct{
double a; /* Real*/
double b; /* Imaginary*/
}Complex;
Complex Nmbr1,Nmbr2;

int main()
{
Complex NmbrMulti;
printf("Type the value of the real part of the first number\n");
scanf("%d",&Nmbr1.a);
printf("\nType the value of the Imaginary part of the first number\n");
scanf("%d",&Nmbr1.b);
printf("\nType the value of the real part of the second number\n");
scanf("%d",&Nmbr2.a);
printf("\nType the value of the Imaginary part of the second number\n");
scanf("%d",&Nmbr2.b);

   NmbrMulti.a = Nmbr1.a  * Nmbr2.a  - Nmbr1.b * Nmbr2.b ;
   NmbrMulti.b = Nmbr1.a * Nmbr2.b + Nmbr2.a * Nmbr1.b ;

  printf("\nThe Multiplication : %d+i", NmbrMulti.a);
  printf("%d\n", NmbrMulti.b);

return 0;
}

但我得到的结果是0+i0 ,似乎问题出在运算上,还有另一种方法可以对我不知道的双数进行乘法运算吗?

scanf看到%d ,相应的参数必须是指向int的指针。 如果参数是指向double的指针,则代码应为%lf

参考
http://www.cplusplus.com/reference/cstdio/scanf/

在C中将double格式化为lf而不是d 后者暗示和int

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM