简体   繁体   中英

can't do multiplication operation on 'double' numbers

I am trying to write some functions for Complex type numbers, but I couldn't make this work.

#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;
}

but I get the result 0+i0 , it seems that the problem is with the operations, is there another way to do multiplication for double numbers that I'm not aware of ?

When scanf sees %d , the corresponding argument must be a pointer to int . If the argument is a pointer to double , the code should be %lf .

Reference
http://www.cplusplus.com/reference/cstdio/scanf/

Formatting for double in C is lf rather than d . The latter implies and int .

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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