簡體   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