원형연결리스트를 이용한 다항식 연산 구현
리카
질문 제목 : 원형연결리스트를 이용한 다항식 연산 구현원형연결리스트를 이용해서 다항식의 덧셈과 곱셈 연산을 구현해야 하는데요,
printpoly함수에서 문제가 생겼는지 안 돌아가요ㅠㅠ 덧셈이랑 곱셈 함수는 검사도 못해봤어요ㅜ
무한루프만 돌고, 무슨 문제인 걸까요??질문 내용 : #include stdio.h
#include stdlib.h
#include math.h
#include malloc.h
#define compare(x,y) (((x) (y)) ? -1: ((x) == (y))? 0:1)
#define false 0
#define true 1
typedef struct polynode *polypointer;
typedef struct polynode{
int coef;
int expon;
polypointer link;
}polynode;
polypointer a, b;
polypointer avail=null;
void attach(int coefficient, int exponent, polypointer *ptr);
polypointer getnode();
void retnode(polypointer);
void cerase(polypointer *);
polypointer readpoly();
void printpoly(polypointer);
polypointer cpadd(polypointer, polypointer);
polypointer cmult(polypointer, polypointer);
int main()
{
polypointer a, b, c;
int menu;
while(1){
printf([1]addition [2]multiplication [3]exit \n);
scanf(%d, &menu);
printf(first polynomial\n);
a=readpoly();
printf(second polynomial\n);
b=readpoly();
if(menu==1){
printf(result of addition\n);
printpoly(a);
printf(\n+);
printpoly(b);
printf(\n=);
printf(\n);
c=cpadd(a,b);
printpoly(c);
}
else if(menu==2){
c=cmult(a,b);
}
else
exit(1);
}
return 0;
}
polypointer getnode()
{
polypointer node;
node=(polypointer)malloc(sizeof(polynode));
node-coef=null; // /* 헤드 노드를 가진 원형
node-expon=null; // 연결 리스트 */
node-link=node;
return node;
}
void retnode(polypointer node)
{
node-link = avail;
avail = node;
}
polypointer cpadd(polypointer a, polypointer b)
{
polypointer starta,startb, c;
int sum;
starta=a; //a 와 b 의 시작을 기록
startb=b;
if( a != null )
a=a-link; // 헤드 노드 건너뜀
if( b != null )
b=b-link;
c=getnode();
//a 와 b 의 지수와 계수 비교후 d 에 입력
while(starta != a && startb != b){
switch (compare(a-expon, b-expon))
{
case -1: // b 의 계수가 더 큰 경우
attach(b-coef,b-expon,&c);
b=b-link; //b 는 다음 노드로 이동
break;
case 0 : // a와 b 의 계수가 같은 경우
sum = a-coef + b-coef;
if(sum)
attach(sum, a-expon,&c);
a=a-link; //a와 b 모두 이동
b=b-link;
break;
case 1: //a의 계수가 더 큰 경우
attach(a-coef,a-expon,&c);
a=a-link; //a 는 다음 노드로 이동
}
}
while(starta != a) { // a 식이 더 길 경우 a 만 더함
attach(a-coef,a-expon,&c);
a=a-link;&; // a 를 다음 노드로 이동
}
while(startb != b) { // b 식이 더 길 경우 b 만 더함.
attach(b-coef,b-expon,&c);
b=b-link; //b 를 계속 이동시킴
}
return c;
}
polypointer readpoly()
{
polypointer node;
int coefficient;
int exponent;
int n;
int i;
printf(input n : );
scanf(%d, &n);
node = getnode();
for(i=0; in; i++) //n 번 만큼 루프를 돌고
{ //지수,계수를 입력 받음 */
scanf(%d %d,&coefficient, &exponent);
attach(coefficient,exponent,&node);
}
return node;
}
void attach(int coefficient, int exponent, polypointer *ptr)
{
polypointer tmp;
tmp = getnode();
tmp-coef=coefficient;
tmp-expon=exponent;
(*ptr)-link=tmp;
*ptr=tmp;
}
void printpoly(polypointer ptr)
{
polypointer c=ptr-link;
for(;c-link=null;c=c-link)
printf(%dx^%d, c-coef, c-expon);
}
polypointer cmult(polypointer a, polypointer b)
{
polypointer starta, startb, c, lastc;
int tmpexpon;//임시 지수 저장
int tmpcoef;//임시 계수 저장
starta=a;
startb=b;
a=a-link;
b=b-link;
c=getnode();
c-expon=-1;
c-coef=-1;
lastc=c;
for(;a!=starta;a=a-link)
{
for(;b!=startb;b=b-link)
{
tmpcoef=a-coef*b-coef;
tmpexpon=a-expon+b-expon;
attach(tmpcoef,tmpexpon,&lastc);
}
}
return c;
}