다항식의 덧셈 관련 질문입니다
미즈
질문 제목 : 링크드리스트 이용해 다항식의 뺄셈코드를 작성해오라는 과제인데 지금 덧셈도 제대로 안돌아가네요..이 코드 컴파일해보면 에러도 안나고 실행까지 됩니다...
그런데 실행해서 숫자를 입력만 하면 바로 오류가 발생하네요..
아마 input함수에서 문제가 발생하지 않을까 싶은데... 컴파일 에러도 안뜨니.. 못찾겠네요 ㅜㅜ
좀만 도와주세요 ㅜㅜㅜㅜㅜ
질문 내용 :
#include stdio.h
#include stdlib.h
#include string.h
typedef struct poly_node *poly_ptr;
typedef struct poly_node {
int coef;
int exp;
poly_ptr link;
} poly_node;
poly_ptr a, b, d;
void attach(int coefficient, int exponent, poly_ptr *ptr) {
poly_ptr temp;
temp = (poly_ptr) malloc( sizeof(poly_node) );
temp-coef = coefficient;
temp-exp = exponent;
(*ptr)-link = temp; /* attach temp to the node pointed by ptr */
*ptr = temp; /* ptr is updated to point to the new node (temp) */
free (temp);
}
poly_ptr padd( poly_ptr a, poly_ptr b) {
poly_ptr d, rear, temp; //템프도 필요없는거 같은데
int sum;
rear = (poly_ptr) malloc( sizeof(poly_node) );
d = rear;
while (a && b) //until both a & b are null
{
if ( a-exp b-exp ) { //case 2/
attach (b-coef, b-exp, &rear);
b = b-link; }
else if ( a-exp b-exp ) { //case 3
attach (a-coef, a-exp, &rear);
a = a-link; }
else { //case 1/
sum = a-coef + b-coef;
attach(sum, a-exp, &rear);
a = a-link;
b = b-link; }
}
/*
for (; a; a = a-link) attach (a-coef, a-exp, &rear);
for (; b; b = b-link) attach (b-coef, b-exp, &rear);
*/
//이건 필요없지 않나?
rear-link = null;
temp = d;
d = d-link; //다항식의 첫 항을 가리킴
free (rear); /* delete extra initial node */
return d;
}void input(poly_ptr p)
{
printf( 계수와 지수 입력 : );
scanf(%d %d, &p-coef, &p-exp);
while ( !(p-coef == -1 && p-exp == -1) ) {
printf( 계수와 지수 입력 : );
p=p-link;
scanf(%d %d, &p-coef, &p-exp);
}
}
void print(poly_ptr p)
{
while ( !(p-coef == -1 && p-exp == -1) )
{
if(p-coef !=0 )
printf(%dx^%d, p-coef, p-exp);
p=p-link;
if (p-coef == -1 && p-exp == -1)
printf(\n);
else
{
if(p-coef !=0) printf( + );
}
}
}
int main(void)
{
printf(p1 입력\n);
input(a);
printf(p2 입력\n);
input(b);
printf(\np1 = );
print(a);
printf(p2 = );
print(b);
printf(\n다항식 덧셈 결과 : \n);
d=padd(a,b);
print(d);
return 0;
}
번호 | 제 목 | 글쓴이 | 날짜 |
---|---|---|---|
2692451 | 이 문제좀 풀어주세요 ^^ | 게자리 | 2025-04-23 |
2692424 | 2차원배열 자료입력질문이요! (1) | 똘끼 | 2025-04-22 |
2692401 | 유닉스안에서 C언어를 이용한 명함 만들기 입니다; 이해안가는 부분이있네요 | 2gether | 2025-04-22 |
2692374 | 고수님들 댓글 마니부탁해요!!! (2) | 엄지 | 2025-04-22 |
2692343 | scnaf에 자꾸 선언을 참조하라는데;; (8) | 도래 | 2025-04-22 |
2692282 | 도스상에서 생성된 exe파일에 press~ 뜨게 하기 (4) | 회사원 | 2025-04-21 |
2692256 | scanf("%*c"); ㅠㅠ 고수님들 | 거북이 | 2025-04-21 |
2692230 | 하노이탑 질문입니다. (1) | 미쁘다 | 2025-04-21 |
2692210 | 정보 올림피아드 문제인데.. 풀이 과정이 궁금합니다.(재귀함수) (5) | 물티슈 | 2025-04-20 |
2692144 | C언어와 리눅스에 대한 질문입니다. | 싴흐한세여니 | 2025-04-20 |
2692114 | 컨텍스트 스위칭하는데 걸리는 시간 측정.. | YourWay | 2025-04-19 |
2692086 | 간접참조 연산자, 증감연산자 질문이용! (2) | 블랙캣 | 2025-04-19 |
2692056 | 주석좀 달아주세요. 몇개적엇는데 몇개만달아주세요. (2) | DevilsTears | 2025-04-19 |
2691978 | 진수 쉽게 이해하는법... (3) | 지지않는 | 2025-04-18 |
2691949 | getchar() 한 문자를 입력받는 함수 질문 | 채꽃 | 2025-04-18 |
2691919 | 배열 정렬 및 합치기 질문입니다. | 사과 | 2025-04-18 |
2691845 | c언어왕초보 질문이 있습니다........ | 루나 | 2025-04-17 |
2691815 | void add(int num); 함수... (4) | 살랑살랑 | 2025-04-17 |
2691756 | 명령 프롬프트 스크롤바가 없어요 | 두메꽃 | 2025-04-16 |
2691725 | 자료구조에 관련해서 질문이 있어 글을 올립니다. | 누리알찬 | 2025-04-16 |