링크드리스트 이용해서 다항식 덧셈하는거 질문이요...
ComeOn
질문제목:
링크드리스트 이용해서 다항식 덧셈하는거 질문이요...
질문요약:
C 완전 초보입니다;
링크드리스트를 이용해서 다항식 덧셈하는 프로그램을 짜는 중인데
프로그램을 돌리면 지수, 계수 출력부분에서 자꾸 에러가 나네요... (컴파일 에러는 없구용)
도대체 뭘 어떻게 고쳐야 할지 감이 전혀 안오구요 ㅠㅠ
질문내용:
프로그램은 일단각각의 다항식 a, b 에 대해서 coeffcient와 exponetial을 입력 받으면
그때마다 입력받은 coef와 expon을 보여줍니다(계속 입력할건지 물어보구요)
a와 b의 입력이 다 끝나면 두 다항식을 더해서 c에 저장후
a,b,c를 출력해주는 건데요
출력 ex)
List A : ================================
coef : 2 3
expon : 4 2
List B : ================================
coef : 5 4
expon : 2 1
List C : ================================
coef : 2 8 4 expon : 4 2 1이런식으로 출력이 되어야 하는데 자꾸 List A 에서 첫번째 지수와 계수를 입력하고 엔터치면
Coef만 출력되고오류생기면서 프로그램이 꺼지네요; 어딜 어떻게 고쳐야 할까요?;;ㅠㅠ
소스 올립니다.(중간에 자잘한 건 생략했어요)
#include stdio.h
#include stdlib.h
#include string.h
#define MALLOC(p,s) \
if (!((p) = malloc(s))) {\
fprintf(stderr, Insufficient Memory); \
exit(1); \
}
typedef struct polynode *polyPointer;
typedef struct polynode
{
int coef;
int expon;
polyPointer link;
};
polyPointer a;
polyPointer b;
int ans;
void linkedpolynomialadd();
polyPointer lpadd(polyPointer a, polyPointer b);
void lattach(int coef, int expon, polyPointer *ptr);
void printList(polyPointer ptr);void linkedpolynomialadd() // 메인함수라고 생각하시면 될듯
{
polyPointer rear, c;
int coef, expon;
ans = 1;
MALLOC(rear, sizeof(*rear));
a = rear;
while (ans !=0)
{
// 1. get input (of coef and expon ) for poly A using scanf
printf(Input the coef and expon for poly A : );
scanf(%d %d, &coef, &expon);
// 2. store coetore coef and expon into the node created by MALLOC
rear-coef=coef;
rear-expon=expon;
// 3. attach the node to the end of poly A
lattach(coef, expon, &a);
printf(List A : ================================\n);
printList(a);//이부분에서 에러가 나네요 ㅠ
printf(Will you continue ? - (Y - 1/ N - 0) );
scanf(%d, &ans);
}
ans=1;
MALLOC(rear, sizeof(*rear));
b = rear;
while (ans !=0)
{
// 1. get input (coef and expon ) for poly B using scanf
printf(Input the coef and expon for poly B : );
scanf(%d% d, &coef, &expon);
// 2. store coef and expon into the node created by MALLOC
rear-coef=coef;
rear-expon=expon;
// 3. attach the node to the end of poly B
lattach(coef, expon, &b);
printf(List B : ================================\n);
printList(b);
printf(Will you continue ? - (Y - 1/ N - 0) );
scanf(%d, &ans);
}c = lpadd(a, b);
printf(List A : ================================\n);
printList(a);
printf(List B : ================================\n);
printList(b);
printf(List C : ================================\n);
printList(c);
}
polyPointer lpadd(polyPointer a, polyPointer b)
{
polyPointer c, rear, temp;
int sum;
MALLOC(rear, sizeof(*rear));
c= rear;
while (a && b)
switch(compare(a-expon, b-expon)) {
case -1 : lattach(b-coef, b-expon, &rear);
b = b-link;
break;
case 0 : sum = a-coef + b-coef;
if (sum) lattach(sum, a-expon, &rear);
a=a-link; b= b-link;
break;
case 1 : lattach(a-coef, a-expon, &rear);
a=a-link;
break;
}
for (; a; a=a-link)
lattach(a-coef, a-expon, &rear);
for (; b; b=b-link)
lattach(b-coef, b-expon, &rear);
rear-link = NULL;
temp = c;
c=c-link;
free(temp);
return c;
}
void lattach(int coef, int expon, polyPointer *ptr)
{
polyPointer temp;
MALLOC(temp, sizeof(*temp));
temp-coef = coef;
temp-expon = expon;
(*ptr)-link = temp;
*ptr = temp;
}
/////////////////////ㅡ _- 요부분...orz
void printList(polyPointer ptr)
{
polyPointer temp;
temp=ptr;
// print the coef and expon of all nodes in ptr list
printf(Coef : );
while(ptr!=NULL)//coef
{
printf(%d , ptr-coef);
ptr=ptr-link;
}
printf(\nExpon : );//expon
while(temp!=NULL)
{
printf(%d , temp-expon);
temp=temp-link;
}
}
-
두동 2025-01-26
MALLOC(rear, sizeof(*rear));
를 하기 전에
printf(\%d\
번호 | 제 목 | 글쓴이 | 날짜 |
---|---|---|---|
2697129 | [질문] 아래 저축액을 계산하는 프로그램 ㅠ 추가 질문 (7) | 찬늘 | 2025-06-05 |
2697072 | 쌩초보 질문! (1) | 얀 | 2025-06-04 |
2697050 | 도움부탁드립니다.. | 화이트 | 2025-06-04 |
2697023 | gotoxy함수, 어떻게 사용하죠? (3) | 적송 | 2025-06-04 |
2696994 | c언어를 막 시작한 초보인데 질문이 있어용 ㅠㅠ (2) | 귀1여운렩 | 2025-06-03 |
2696944 | 윈도우 콘솔프로그램 질문드립니다 (2) | 꽃님이 | 2025-06-03 |
2696882 | c언어 입력받기 질문 입니다 (2) | 흰추위 | 2025-06-02 |
2696853 | 문자열은 정적메모리에 할당된다고 하는데 정적메모리가 뭡니까?? (6) | 다온 | 2025-06-02 |
2696799 | c c++언어입문전에 파이톤이나 자바스크립트배워두면.. (2) | 앨런 | 2025-06-02 |
2696747 | size of 연산자에 대해 질문이 있습니다. (3) | 유진공주 | 2025-06-01 |
2696721 | Char 타입의 2D array 기초에 대해 질문드립니다. | 벚꽃 | 2025-06-01 |
2696691 | 자릿수 이동방법좀 가르쳐주세요 (1) | 흰추위 | 2025-06-01 |
2696664 | 학생정보 구조체 질문 | 큰아 | 2025-05-31 |
2696619 | c언어에서는 논리형을 어떻게 표현하나요? | 은율 | 2025-05-31 |
2696590 | 구조체 리스트 입니다..... (4) | 피네 | 2025-05-31 |
2696565 | 스택 미로찾기 소스코드 오류 질문드립니다 | 크나 | 2025-05-30 |
2696512 | 포인터를 배열처럼 이용하려는데 잘 모르겠습니다. | 재찬 | 2025-05-30 |
2696465 | 이해가 가지 않는것이 있어 질문합니다. (3) | 깜찏한그1녀 | 2025-05-29 |
2696411 | 질문... 힌트좀 주세요~ | 초코향 | 2025-05-29 |
2696379 | c언어 반복문 중괄호 질문입니다. (6) | 일본드립 | 2025-05-29 |