不能在' double'上执行乘法运算数字

时间:2014-04-02 21:38:33

标签: c double multiplication

我正在尝试为复杂类型编写一些函数,但我无法使其工作。

#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,似乎问题在于操作,还有另一种方法可以对我不知道的双数进行乘法运算吗?

2 个答案:

答案 0 :(得分:5)

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

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

答案 1 :(得分:2)

C中double的格式为lf而不是d。后者意味着int