complex0.c 994 B

123456789101112131415161718192021222324252627282930313233343536373839404142434445
  1. // complex.c -- complex numbers
  2. #include <stdio.h>
  3. #include <complex.h>
  4. void show_cmlx(complex double cv);
  5. int main(void)
  6. {
  7. complex double v1 = 4.0 + 3.0*I;
  8. double re, im;
  9. complex double v2;
  10. complex double sum, prod, conjug;
  11. printf("Enter the real part of a complex number: ");
  12. scanf("%lf", &re);
  13. printf("Enter the imaginary part of a complex number: ");
  14. scanf("%lf", &im);
  15. // CMPLX() a C11 feature
  16. // v2 = CMPLX(re, im);
  17. v2 = re + im * I;
  18. printf("v1: ");
  19. show_cmlx(v1);
  20. putchar('\n');
  21. printf("v2: ");
  22. show_cmlx(v2);
  23. putchar('\n');
  24. sum = v1 + v2;
  25. prod = v1 * v2;
  26. conjug = conj(v1);
  27. printf("sum: ");
  28. show_cmlx(sum);
  29. putchar('\n');
  30. printf("product: ");
  31. show_cmlx(prod);
  32. putchar('\n');
  33. printf("complex congjugate of v1: ");
  34. show_cmlx(conjug);
  35. putchar('\n');
  36. return 0;
  37. }
  38. void show_cmlx(complex double cv)
  39. {
  40. printf("(%.2f, %.2fi)", creal(cv), cimag(cv));
  41. return;
  42. }