'프로그래밍 언어공부/열혈강의 C프로그래밍 (2003년작)'에 해당되는 글 34건

  1. 2015.07.22 열혈강의 C 연습문제 23-1

연습문제 1


[uzi@uzi pg]$ cat ex23_1.c 

#include <stdio.h>


struct simple{

        int data1;

        int data2;

};


void swap(struct simple *a, struct simple *b);

void show(struct simple k);


int main()

{

        struct simple i={1,2};

        struct simple j={3,4};


        show(i);

        show(j);

        swap(&i,&j);

        show(i);

        show(j);

        return 0;

}


void swap(struct simple *a, struct simple *b)

{

        struct simple *temp;

        *temp=*a;

        *a=*b;

        *b=*temp;

}


void show(struct simple k)

{

        printf("%d %d \n",k.data1,k.data2);

}



[uzi@uzi pg]$ ./ex23_1 

1 2 

3 4 

3 4 

1 2 

[uzi@uzi pg]$ 



연습문제 2


[uzi@uzi pg]$ cat ex21_1_2.c 

#include <stdio.h>


struct point{

        int x;

        int y;

};


struct point PointAdd(struct point i, struct point j);

struct point getdata(void);


int main()

{

        struct point a=getdata();

        struct point b=getdata();

        struct point c=PointAdd(a,b);


        printf("data1: %d , data2: %d\n",c.x,c.y);


        return 0;

}


struct point PointAdd(struct point i, struct point j)

{

        struct point c;

        c.x=i.x+j.x;

        c.y=i.y+j.y;


        return c;

}

struct point getdata(void)

{

        struct point d;

        scanf("%d %d",&d.x,&d.y);

        return d;

}



[uzi@uzi pg]$ ./ex21_1_2     

4 6

5 4

data1: 9 , data2: 10

[uzi@uzi pg]$ 

Posted by 느림의 미학 .
,