링크리스트로 구현한 다항식 질문좀 드립니다..
맛조이
질문 제목 : 링크리스트로 다항식 만든것중에 에러가 발생했습니다.질문 요약 :소스는 이상이없지만 아래 padd부분에서 문제가 발생하는 것 같습니다 도와주세요 ㅠ질문 내용 :
#include stdio.h
#include stdlib.h
#define COMPARE(x,y) ( ( (x)(y) ) ? -1 : ( (x)==(y) ) ? 0 : 1)
#define IS_FULL(ptr) (!(ptr))
typedef struct term *ptr;
struct term
{
float coef;
int exp;
ptr link;
};
ptr p, q, s;
void attach(float coefficient, int exponent, ptr *x);
ptr padd(ptr p, ptr q)
{
ptr front, rear,temp;
float sum;
rear = (ptr) malloc(sizeof(term));
if(IS_FULL(rear))
{
fprintf(stderr, We cannot add two polynomials since the memory is full\n);
exit(1);
}
front = rear;
while(p && q)
switch(COMPARE(p-exp, q-exp))
{
case -1:
attach( q-coef, q-exp, &rear);
q = q-link;
break;
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 attach(float coefficient, int exponent, ptr *x)
{
ptr temp;
temp = (ptr) malloc(sizeof(term));
if(IS_FULL(temp))
{
printf(We cannot new term since the memory is full\n);
return;
}
temp-coef = coefficient;
temp-exp = exponent;
(*x)-link = temp;
*x = temp;
}
void main()
{
int counta=0;
int countb=0;
float coeff;
int expon;
ptr arear,brear,afront,bfront;
arear=(ptr)malloc(sizeof(term));
brear=(ptr)malloc(sizeof(term));
afront=arear;
bfront=brear;
printf(\n모든 항은 오름차순이고, 제일 앞의 항이 최고차 항이다.\n);
printf(입력을 마치실려면 -1을 입력하시오:\n);
for ( ; ; )
{
counta++;
printf(\nA의 항의 %d번째 계수를 입력하시오:,counta);
scanf(%d, &coeff);
if(coeff0)
{
printf(\nA의 항의 %d번째 지수를 입력하시오:,counta);
scanf(%d, &expon);
}
else
{
break;
}
attach(coeff,expon,&arear);
}
for ( ; ; )
{
countb++;
printf(\nB의 항의 %d번째 계수를 입력하시오:,countb);
scanf(%d, &coeff);
if(coeff0)
{
printf(\nB의 항의 %d번째 지수를 입력하시오:,countb);
scanf(%d, &expon);
}
else
{
break;
}
attach(coeff,expon,&brear);
}
printf(다항식 A와 B의 합은\n);
s=padd(afront,bfront);
for( ; ; )
{
printf(%dx^%d,s-coef,s-exp);
s=s-link;
if(s-link==NULL)
break;
printf(+);
}
}
-
수국
어느줄에서 어떤 에러가 발생했는지, 입력은 무엇을 주었는지 등을 같이 적어 주시면 문제를 알아보는데 도움이 될 것 같네요.