반응형

1번 문제에서 equal()과 quadrant()를 다음과 같이 구조체의 포인터를 받도록 변경하여서 작성하고 테스트하라. 기존의 코드는 어떻게 변경하여야 하는가?

int equal(struct point *p1, struct point *p2);


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
26
27
28
29
#include <stdio.h>
typedef struct point{
    int x;
    int y;
} POINT;
int equal(POINT *p1, POINT *p2);
int main(void)
{
    POINT *a, *b;
    POINT first,second;
    a=&first; b=&second;
    int i;
    printf("두 점이 같으면 1 다르면 0을 반환합니다.\n");
    printf("a점의 좌표를 입력하세요.\n");
    scanf("%d %d"&first.x, &first.y);
    printf("b점의 좌표를 입력하세요.\n");
    scanf("%d %d"&second.x, &second.y);
    i=equal(a,b);
    printf("결과값은 %d입니다.\n", i);
    return 0;
}
int equal(POINT *p1, POINT *p2)
{
    int result=0;
    if((p1->x)==(p2->x)&&(p1->y)==(p2->y)) // p1->x 는 (*p1).x 와 같다.
    {result=1;}
    return result;
}
 
cs

결과 사진은 1번과 같다.

int quadrant(struct point *p);


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
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
#include <stdio.h>
typedef struct point{
    int x;
    int y;
} POINT;
int quadrant(POINT *p);
int main(void)
{
    POINT a;
    POINT *k=&a;
    int res;
    while(1){
    printf("점의 좌표를 입력하세요.(종료는 6666 6666)\n");
    scanf("%d %d"&a.x, &a.y);
    res=quadrant(k);
    if(res>=1&&res<=4)
        printf("점은 %d사분면 위에 있습니다.\n", res);
    else if(res==5)
        printf("점은 원점이나 축위에 있습니다.\n");
    else if(res==6)
        break;}
    return 0;
}
int quadrant(POINT *p)
{
    int result=0;
    if(p->x==6666&&p->y==6666)    
        result=6;
    else if(p->x>0&&p->y>0//p->x=(*p).x 와 같다.
        result=1;
    else if(p->x<0&&p->y>0)
        result=2;
    else if(p->x<0&&p->y<0)
        result=3;
    else if(p->x>0&&p->y<0)
        result=4;
    else if(p->x==0||p->y==0)
        result=5;
    return result;
}
 
cs


결과 사진은 1번과 같다.

반응형

'컴퓨터 & 프로그래밍 & 전자공학 > C언어' 카테고리의 다른 글

복소수 합  (0) 2016.12.27
사각형 판별과 둘레와 넓이  (0) 2016.12.27
좌표 일치 확인  (0) 2016.12.27
찾아 바꾸기  (0) 2016.08.22
전광판 만들기  (0) 2016.08.22

+ Recent posts