#include<stdio.h> // printf()
#include<stdlib.h> // exit()
#include<malloc.h> // malloc()
#include<string.h> // memset()
#define IS_FULL(p) (!(p))
typedef struct _PTR ptr;
struct _PTR
{
float coef;
int exp;
ptr *link;
};
void attach(float, int, ptr *);
int compare(int, int);
ptr* padd(ptr *, ptr *);
void init(ptr **, ptr **);
void clearT(ptr *);
void pp(char *, ptr *);
void attach(float coefficient, int exponent, ptr **txt)
{
ptr *temp;
temp = (ptr *) malloc(sizeof(ptr));
memset(temp,0,sizeof(ptr));
if (IS_FULL(temp))
{
printf("We cannot attach new term since the memory is full\n");
return;
}
temp-> coef= coefficient;
temp-> exp= exponent;
// 복잡하게 temp에 넣고 그것을 rear에다 대입한다.
(*txt)->link= temp; // error
*txt= temp;
}
int compare(int exp1, int exp2)
{
if (exp1 == exp2) return 0;
if (exp1 < exp2) return -1;
else return 1;
}
ptr* padd(ptr *p, ptr *q)
{
/* 함수내에서 rear변수는 정확하게 정해진 타입이 아님. */
ptr *front, *rear, *temp;
float sum; // data type is float by recommend.
rear = (ptr *) malloc(sizeof(ptr)); // rear is return value
memset(rear,0,sizeof(ptr)); // return setting
if(IS_FULL(rear))
{
fprintf(stderr,"We cannot add two polynomials since the memory is full\n");
exit(1);
}
front = rear;
while ((p) && (q))
/* 다항식 P(x) 항의 차수와 Q(x)항의 차수를 비교 */
switch(compare(p->exp, q->exp))
{
case -1 :
attach( q->coef, q->exp, &rear);
q= q->link;
break;
/* 다항식 P(x) 항의 차수와 Q(x)항의 차수보다 적을 때
* Q(x)의 항을 하나 추가하고, 포인터 q가 다항식 Q(x)의
* 다음 항을 가리키도록 한다.
*/
case 0 :
sum = p->coef + q->coef;
if (sum)
attach(sum, p->exp, &rear);
p=p->link;
q=q->link;
break;
case 1:
attach(p->coef, p->exp, &rear);
p=p->link;
}
for(; p; p=p->link)
attach(p->coef, p->exp, &rear);
for(; q; q=q->link)
attach(q->coef, q->exp, &rear);
rear->link =NULL;
temp = front;
front = front ->link;
free(temp);
return front;
}
void init(ptr **p, ptr **q)
{
ptr *w, *x, *y;
*p = (ptr *) malloc(sizeof(ptr));
*q = (ptr *) malloc(sizeof(ptr));
(*p)->coef = -3;
(*p)->exp = 5;
w=(ptr *) malloc(sizeof(ptr));
(*p)->link = w;
w->coef=2;
w->exp =2;
x =(ptr *) malloc(sizeof(ptr));
w->link = x;
x->coef=9;
x->exp =1;
y =(ptr *) malloc(sizeof(ptr));
x->link = y;
y->coef=-1;
y->exp =0;
y->link = NULL;
(*q)->coef = -2;
(*q)->exp = 5;
w=(ptr *) malloc(sizeof(ptr));
(*q)->link = w;
w->coef=3;
w->exp =3;
x =(ptr *) malloc(sizeof(ptr));
w->link = x;
x->coef=-4;
x->exp =2;
y =(ptr *) malloc(sizeof(ptr));
x->link = y;
y->coef=6;
y->exp =0;
y->link = NULL;
}
void pp(char *msg, ptr * r)
{
/* role : polynominal printing */
printf("%s(x)= ",msg);
/* <digit><^><digit> {<+><digit>} expression */
printf("%dx^%d ",(int)r->coef,r->exp);
r=r->link;
while(r)
{
printf("+ %dx^%d",(int)r->coef,r->exp);
r=r->link;
}
printf("\n");
}
void clearT(ptr *p)
{
ptr *tmp;
while(p)
{
tmp=p->link;
free(p);
p=tmp;
}
}
main()
{
ptr *p, *q, *r;
/* int *p라고 선언되었다면
* *p는 *p가 가리키는곳의 값
* p는 가리키는곳의 주소(값)
* &p는 현재 p의 주소.
*/
init(&p, &q);
printf("Data Init .. ok\n");
printf("Result Value Memory Allocation .. ok\n");
r=padd(p,q);
printf("Result Value Memory Insert .. ok\n");
pp("P",p);
pp("Q",q);
pp("R",r);
/* dynamic allocation clear routine */
printf("program finish .. ");
clearT(p);
clearT(q);
clearT(r);
printf("ok\n");
}
책에 나온거라서 많이 안고치고 일부러 중요한사항만 고치려고 했습니다.
이렇게 해도 경고사항이 많이 뜨네요..
여튼 답변 감사드립니다. 즐거운시간되십시요.
^^*
|