셀맨1 2016. 12. 27. 21:49
반응형

2차원 벡터를 구조체로 정의하여 보라. 벡터 사이에는 덧셈 연산들이 정의될 수 있다. 벡터의 덧셈을 함수로 구현하고 테스트해보자.


1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
#include <stdio.h>
struct vector{
    double x;
    double y;
};
struct vector vector_add(struct vector c1, struct vector c2);
int main(void)
{
    struct vector a,b,res;
    a.x=0;a.y=0;b.x=0;b.y=0;
    printf("벡터 a의 x성분과 y성분을 차례로 입력하세요.\n");
    scanf("%lf %lf"&a.x, &a.y);
    printf("벡터 b의 x성분과 y성분을 차례로 입력하세요.\n");
    scanf("%lf %lf"&b.x, &b.y);
    res=vector_add(a,b);
    printf("결과는 (%lf,%lf)  입니다.\n",res.x, res.y);
    return 0;
}
struct vector vector_add(struct vector c1, struct vector c2)
{
    struct vector sum;
    sum.x=c1.x+c2.x;
    sum.y=c1.y+c2.y;
    return sum;
}
cs



앞의 복소수 합과 똑같은 문제이다.

반응형