a1+ ib1 및 a2 + ib2 형식의 두 복소수가 주어지면 이 두 복소수를 더하는 것이 과제입니다.
복소수는 "a+ib"의 형태로 표현될 수 있는 수입니다. 여기서 "a"와 "b"는 실수이고 i는 𝑥 2 =-1 식의 해인 허수입니다. 실수는 방정식을 만족하므로 허수라고 합니다.
입력
a1 = 3, b1 = 8 a2 = 5, b2 = 2
출력
Complex number 1: 3 + i8 Complex number 2: 5 + i2 Sum of the complex numbers: 8 + i10
설명
(3+i8) + (5+i2) = (3+5) + i(8+2) = 8 + i10
입력
a1 = 5, b1 = 3 a2 = 2, b2 = 2
출력
Complex number 1: 5 + i3 Complex number 2: 2 + i2 Sum of the complex numbers: 7 + i5
설명
(5+i3) + (2+i2) = (5+2) + i(3+2) = 7 + i5
문제를 해결하기 위해 다음과 같은 접근 방식을 사용합니다.
-
실수와 허수를 저장할 구조체를 선언합니다.
-
입력을 받아 모든 복소수의 실수와 허수를 더하세요.
알고리즘
Start
Decalre a struct complexnum with following elements
1. real
2. img
In function complexnum sumcomplex(complexnum a, complexnum b)
Step 1→ Declare a signature struct complexnum c
Step 2→ Set c.real as a.real + b.real
Step 3→ Set c.img as a.img + b.img
Step 4→ Return c
In function int main()
Step 1→ Declare and initialize complexnum a = {1, 2} and b = {4, 5}
Step 2→ Declare and set complexnum c as sumcomplex(a, b)
Step 3→ Print the first complex number
Step 4→ Print the second complex number
Step 5→ Print the sum of both in c.real, c.img
Stop 예시
#include <stdio.h>
//structure for storing the real and imaginery
//values of complex number
struct complexnum{
int real, img;
};
complexnum sumcomplex(complexnum a, complexnum b){
struct complexnum c;
//Adding up two complex numbers
c.real = a.real + b.real;
c.img = a.img + b.img;
return c;
}
int main(){
struct complexnum a = {1, 2};
struct complexnum b = {4, 5};
struct complexnum c = sumcomplex(a, b);
printf("Complex number 1: %d + i%d\n", a.real, a.img);
printf("Complex number 2: %d + i%d\n", b.real, b.img);
printf("Sum of the complex numbers: %d + i%d\n", c.real, c.img);
return 0;
} 출력
위의 코드를 실행하면 다음 출력이 생성됩니다 -
Complex number 1: 1 + i2 Complex number 2: 4 + i5 Sum of the complex numbers: 5 + i7