[英]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
。
在C中將double
格式化為lf
而不是d
。 后者暗示和int
。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.