C 프로그래밍 언어에서 두 개의 복소수를 더하려면 사용자는 두 개의 복소수를 구조체 멤버로 사용하고 사용자 정의 함수를 만들어 두 숫자에 대해 더하기 연산을 수행해야 합니다.
알고리즘
두 개의 복소수를 더하는 방법은 아래의 알고리즘을 참조하십시오.
Step 1: Declare struct complex with data members. Step 2: Declare name for structure and variables. Step 3: Enter real and imaginary part for first complex number at run time. Step 4: Enter real and imaginary part for second complex number at runtime Step 5: Compute addition of number1 and number2 by calling function. Go to step 7. Step 6: Print the result. Step 7: Compute addition
- Declare temp variable
- temp.real = num1.real + num2.real;
- temp.imag = num1.imag + num2.imag;
- return (temp);
예시
다음은 구조를 함수에 전달하여 두 개의 복소수를 더하는 C 프로그램입니다 -
#include <stdio.h>
typedef struct complex{
float real;
float imag;
} complex;
complex addition(complex num1, complex num2);
int main(){
complex num1, num2, value;
printf("entering real and imag parts of first complex no:\n ");
scanf("%f %f", &num1.real, &num1.imag);
printf("entering real and imag parts of second complex no:\n ");
scanf("%f %f", &num2.real, &num2.imag);
value= addition(num1, num2);
printf("result = %.1f + %.1fi", value.real, value.imag);
return 0;
}
complex addition(complex num1, complex num2){
complex temp;
temp.real = num1.real + num2.real;
temp.imag = num1.imag + num2.imag;
return (temp);
} 출력
위의 프로그램을 실행하면 다음과 같은 결과가 나온다 -
entering real and imag parts of first complex no: entering real and imag parts of second complex no: result = 0.0 + 0.0i