본문 바로가기
Paper

c programming practice

by hasd 2013. 9. 7.



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

struct point{
 float x;
 float y;
};

struct complex{
 double re;
 double im;
};

void add(int a, int b, int *p);
void swap(int *x, int *y);
float distance(struct point *p, struct point *q);
void print_point(struct point*p);
void assign(struct complex *p,double real,double imag);
void add_complex(struct complex *result,struct complex *p,struct complex *q);
void prt(struct complex *p);

int main(void){
 int sum;
 int a,b;
 a=10;b=20;
 struct point c={1.0,4.0};
 struct point d={5.0,1.0};
 struct complex c1,c2,c3;


 //add(10,20,&sum);
 //printf("sum=%d\n",sum);
 //
 //printf("before:a=%d, b=%d\n",a,b);
 //swap(&a,&b);
 //printf("after :a=%d, b=%d\n",a,b);

 //printf("점 c의 좌표:");
 //print_point(&c);
 //printf("\n점 d의 좌표:");
 //print_point(&d);
 //printf("\n두 점 사이의 거리: %f\n",distance(&c,&d));

 //assign(&c1,1.0,2.0); /* c1 : 1+2i*/
 //assign(&c2,3.0,4.0); /* c2 : 3+4i*/
 //add_complex(&c3,&c1,&c2); /*c3=c1+c2*/
 //prt(&c3);
  
 return 0;

}

void add(int a,int b, int*p){
 *p=a+b;
}

void swap(int *x,int *y)
{
 int tmp;

 tmp = *x;
 *x=*y;
 *y=tmp;
}

float distance(struct point *p,struct point *q){
 float dx,dy,distance;

 dx=p->x-q->x;
 dy=p->y-q->y;
 distance=sqrt(dx*dx+dy*dy);
 return distance;
}

void print_point(struct point *p){
 
 printf("(%g,%g)",p->x,p->y);
 
}

void assign(struct complex *p,double real,double imag){
 p->re=real;
 p->im=imag;
}

void add_complex(struct complex *result,struct complex *p,struct complex *q){
 result->re=p->re+q->re;
 result->im=p->im+q->im;
}

void prt(struct complex *p){
 printf("%g + %gi\n",p->re,p->im);
}




댓글