본문 바로가기
C 언어/0x09-other

[c lang] 복소수 사칙연산

by SpeeDr00t 2023. 4. 6.
반응형
/*
how to compile
C99 표준
hacker@rust:~/clang$ gcc -o c1 c1.c
hacker@rust:~/clang$ ./c1
c1: 2.0 + 3.0i
c2: 4.0 + -2.0i
Sum: 6.0 + 1.0i
Difference: -2.0 + 5.0i
Product: 14.0 + 8.0i
Division: 0.1 + 0.8i
*/
#include <stdio.h>
#include <complex.h>
int main() {
// 복소수 생성
double complex c1 = 2.0 + 3.0 * I;
double complex c2 = 4.0 - 2.0 * I;
// 복소수 사칙연산
double complex sum = c1 + c2;
double complex difference = c1 - c2;
double complex product = c1 * c2;
double complex division = c1 / c2;
// 복소수 출력
printf("c1: %.1f + %.1fi\n", creal(c1), cimag(c1));
printf("c2: %.1f + %.1fi\n", creal(c2), cimag(c2));
printf("Sum: %.1f + %.1fi\n", creal(sum), cimag(sum));
printf("Difference: %.1f + %.1fi\n", creal(difference), cimag(difference));
printf("Product: %.1f + %.1fi\n", creal(product), cimag(product));
printf("Division: %.1f + %.1fi\n", creal(division), cimag(division));
return 0;
}
view raw c1.c hosted with ❤ by GitHub
반응형