수다닷컴

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

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

ComeOn

2025.01.26

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

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

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

  • 두동 2025-01-26

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

번호 제 목 글쓴이 날짜
2700400 원넓이를 계산이요 ㅜㅜ 천칭자리 2025-07-04
2700368 if에 관해서 질문이요... Orange 2025-07-04
2700339 이거 결과값이 왜이런건지.. (4) 그댸와나 2025-07-04
2700313 파일 읽어서 저장하는데 빈파일일 경우 문재가 발생하네요.. (2) 크나 2025-07-03
2700287 구조체 동적할당 연습을 하는데 오류가 뜹니다...(해결) (3) 아련나래 2025-07-03
2700264 문자와 숫자 동시에 입력??? 글고운 2025-07-03
2700236 txt파일로만 쓰고 읽게 하려면 어떻게 해야 하나요..?? (8) 미국녀 2025-07-03
2700211 전위 연산자 (2) 어른처럼 2025-07-02
2700183 C에서 파일이름을 받고, 그 파일의 사이즈를 출력해줘야하는데 내용이 출력이 안되네요 ;ㅅ; 피스케스 2025-07-02
2700150 꼭좀 도와주세요ㅠㅠㅠ 호습다 2025-07-02
2700095 연산문제...질문... 오빤테앵겨 2025-07-01
2700070 while문 , 3의배수 출력하는 프로그램좀 짜주세욤. 횃불 2025-07-01
2700041 초보인데요 ㅎ 배열안에 배열을 집어넣을수 있나요?? 헛장사 2025-07-01
2700012 배열// (1) 전갈자리 2025-07-01
2699895 무한루프에 빠집니다.!! 해결좀부탁드려요 (10) 선아 2025-06-30
2699842 질문을 너무 많이 하네여.....죄송.... (2) 해님꽃 2025-06-29
2699816 오류 질문입니다.. (1) 해비치 2025-06-29
2699763 질문입니다 ! 꼭 좀 도와주세요ㅠㅠ (2) 미라 2025-06-28
2699555 c언어 다항식을 입력을 했는데 왜 출력이 안될까요? 피스케스 2025-06-27
2699528 C언어 포인터연산 질문입니다. (3) 안녕나야 2025-06-26
<<  이전  1 2 3 4 5 6 7 8 9 10  다음  >>

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