ํ‹ฐ์Šคํ† ๋ฆฌ ๋ทฐ

๋ฌธ์ œ:  ๋‘ ๊ฐœ์˜ ๋ณต์†Œ์ˆ˜๋ฅผ ์ž…๋ ฅ ๋ฐ›๊ณ , ๋‘ ๋ณต์†Œ์ˆ˜๋ฅผ ๋”ํ•œ ๊ฒฐ๊ณผ๊ฐ’์„ ์ถœ๋ ฅํ•˜๋Š” ํ”„๋กœ๊ทธ๋žจ์„ ์ž‘์„ฑํ•˜์‹œ์˜ค.

์กฐ๊ฑด: ๊ตฌ์กฐ์ฒด ์ •์˜, add ํ•จ์ˆ˜ ์ •์˜, main ํ•จ์ˆ˜์—์„œ ์ž…๋ ฅ ๋ฐ ์ถœ๋ ฅ

 

์ฝ”๋“œ:

 

#include <stdio.h>
#include <string.h>
#include <stdlib.h>

struct complex { //๋ณต์†Œ์ˆ˜ ๊ตฌ์กฐ์ฒด ์ •์˜
	double sil, huh; //๋ณต์†Œ์ˆ˜์˜ ์‹ค์ˆ˜๋ถ€์™€ ํ—ˆ์ˆ˜๋ถ€
};

struct complex add(struct complex a, struct complex b); //ํ•จ์ˆ˜ ์›ํ˜• ์„ ์–ธ, ๊ตฌ์กฐ์ฒด ํ•จ์ˆ˜๋กœ ์„ค์ •ํ•˜๊ธฐ ์ฃผ์˜**

int main() {
	struct complex a, b, c;

	scanf("%lf %lf", &a.sil, &a.huh);
	scanf("%lf %lf", &b.sil, &b.huh);

	c =  add(a, b); //add ํ•จ์ˆ˜๋ฅผ int๋กœ ์„ ์–ธํ•˜๋ฉด ์ด๋ถ€๋ถ„์—์„œ ์˜ค๋ฅ˜๊ฐ€ ๋‚จ
	
	printf("%.1f + %.1fi", c.sil, c.huh);

		return 0;
}

struct complex add(struct complex a, struct complex b) {
	struct complex c;
	c.sil = a.sil + b.sil;
	c.huh = a.huh + b.huh;

	return c;
}
๋ฐ˜์‘ํ˜•