수다닷컴

  • 해외여행
    • 괌
    • 태국
    • 유럽
    • 일본
    • 필리핀
    • 미국
    • 중국
    • 기타여행
    • 싱가폴
  • 건강
    • 다이어트
    • 당뇨
    • 헬스
    • 건강음식
    • 건강기타
  • 컴퓨터
    • 프로그램 개발일반
    • C언어
    • 비주얼베이직
  • 결혼생활
    • 출산/육아
    • 결혼준비
    • 엄마이야기방
  • 일상생활
    • 면접
    • 취업
    • 진로선택
  • 교육
    • 교육일반
    • 아이교육
    • 토익
    • 해외연수
    • 영어
  • 취미생활
    • 음악
    • 자전거
    • 수영
    • 바이크
    • 축구
  • 기타
    • 강아지
    • 제주도여행
    • 국내여행
    • 기타일상
    • 애플
    • 휴대폰관련
  • 프로그램 개발일반
  • C언어
  • 비주얼베이직

링크드리스트 이용해서 다항식 덧셈하는거 질문이요...

잠팅이

2023.04.01

질문제목:
링크드리스트 이용해서 다항식 덧셈하는거 질문이요...

질문요약:
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;
}
}

신청하기





COMMENT

댓글을 입력해주세요. 비속어와 욕설은 삼가해주세요.

  • 울트라

    MALLOC(rear, sizeof(*rear));
    를 하기 전에
    printf(\%d\

번호 제 목 글쓴이 날짜
2699816 오류 질문입니다.. (1) 해비치 2025-06-29
2699763 질문입니다 ! 꼭 좀 도와주세요ㅠㅠ (2) 미라 2025-06-28
2699555 c언어 다항식을 입력을 했는데 왜 출력이 안될까요? 피스케스 2025-06-27
2699528 C언어 포인터연산 질문입니다. (3) 안녕나야 2025-06-26
2699476 끌어올림;;달력 짜봤는데요 이 소스 줄일 수 있나요? - 스샷첨부 (2) 클라우드 2025-06-26
2699444 [좀 급함] system("explorer [주소] ") 문에 변수를 사용할 수 있나요? 알 2025-06-26
2699415 파일//read//와 배열 아란 2025-06-25
2699386 구조체 안에 일부분만 char 배열에 복사하려면 어떻게 해야하나요? (1) 미즈 2025-06-25
2699361 연결리스트 정렬하는 부분에 대해서 질문 드립니다 아이처럼 2025-06-25
2699304 [기초]아직 안주무시는분 계신가요..?포인터배열? 좀 도와주세요. 놀리기 2025-06-24
2699272 printf() 함수이용해서 프로그램 만들기 질문요! (5) 다가 2025-06-24
2699221 PUSH와 POP코드를 더 간단하게 어떻게 해야할까요? 파라미 2025-06-24
2699192 설치오류가 자꾸 나요 한번봐주세여~ (1) 소녀틳향기 2025-06-23
2699161 for loop안에 있는 if문 (9) Orange 2025-06-23
2699105 링크더리스트 이전 링크값 출력함수. 꼬꼬마 2025-06-23
2699078 정수를 한자리씩 배열에 담는 법은 어떻게 하나요.. (4) 귀염포텐 2025-06-22
2699024 C언어 공부하려는데 도와주세요!!! (2) 달님 2025-06-22
2698994 날짜 계산하는 C 코드 짜고 있는데 꽉 막혀서 질문드립니다.. (6) 별 2025-06-22
2698967 파일삭제 윈도우 폴더까지 접근하게하는 함수가 뭔가요 (2) 샤인 2025-06-21
2698938 c언어 메모리질문 (3) 나래 2025-06-21
<<  이전  1 2 3 4 5 6 7 8 9 10  다음  >>

수다닷컴 | 여러분과 함께하는 수다토크 커뮤니티 수다닷컴에 오신것을 환영합니다.
사업자등록번호 : 117-07-92748 상호 : 진달래여행사 대표자 : 명현재 서울시 강서구 방화동 890번지 푸르지오 107동 306호
copyright 2011 게시글 삭제 및 기타 문의 : clairacademy@naver.com