너무 급해요 ㅠㅠ 다항식 곱셈이요
슬기
요약하자면 첨부파일에 있는 파일처럼 항갯수 차수 계수로 두개의 다항식 정보가 저장되있구요
그걸 불러내서 다항식의 곱을 하는 함수를 만든다음 곱한 다항식을 리턴해서 보여주는겁니다
질문 내용 : 현재 진행상황은 파일에서 정보 받은후에 곱셈을 해서 p3에 넣어줬는데.. 이걸 다항식 곱셈할때에 같은 차수가 나와서 더해주는 그 알고리즘적인건 알겠는데 코드가 막히네요 어떻게 그걸 더해주면서 내림차순으로 정리해줄지..
제 코드 기본 바탕으로(곱셈 함수는 거의 바꾸셔도 됩니다 제가한게 틀린거 같아서..) 코드좀 부탁드립니다 ㅠㅠ 너무 급해서 어쩔수가없네요
#includestdio.h
#includestdlib.h
struct term{
int expo;
float coef;
};
struct poly{
int degree;
struct term *pt;
}p1,p2,p3;
void main()
{
int i;
file *fp=fopen(poly.txt,rb);
struct poly mulpoly(struct poly p1,struct poly p2);
fscanf(fp,%d,&p1.degree);
p1.pt=(struct term*)malloc(sizeof(struct term)*p1.degree*2);
printf(p1.degree 는 %d\n,p1.degree);
for(i=0; ip1.degree*2; i++)
{
if(i%2)
{
fscanf(fp,%f,&p1.pt[i/2].coef);
printf(p1.pt[%d].coef는 %f\n,i,p1.pt[i/2].coef);
}
else
{
fscanf(fp,%d,&p1.pt[i/2].expo);
printf(p1.pt[%d].expo는 %d\n,i,p1.pt[i/2].expo);
}
}
//----------------------------파일에서 읽어온 p2 입력-----------------------------------------//
fscanf(fp,%d,&p2.degree);
p2.pt=(struct term*)malloc(sizeof(struct term)*p2.degree*2);
printf(p2.degree 는 %d\n,p2.degree);
for(i=0; ip2.degree*2; i++)// degree 두배 하는 이유는 한 항당 차수 계수 두개씩 있기때문
{
if(i%2)//홀짝
{
fscanf(fp,%f,&p2.pt[i/2].coef); //홀짝
printf(p2.pt[%d].coef는 %f\n,i,p2.pt[i/2].coef);
}
else
{
fscanf(fp,%d,&p2.pt[i/2].expo);
printf(p2.pt[%d].expo는 %d\n,i,p2.pt[i/2].expo);
}
}
mulpoly(p1,p2);
fclose(fp);
delete[]p1.pt;
delete[]p2.pt;
}
struct poly mulpoly(struct poly p1,struct poly p2)
{
int i,j,k=0;
int a_index,b_index;
p3.pt=(struct term*)malloc(sizeof(struct term)*p1.degree*p2.degree+1); //p3 전역변수라 가능,항이 나올 최대 숫자 p1.degree*p2.degree
p3.degree=p1.degree*p2.degree+1;
for(i=0; ip3.degree+1; i++)
{
p3.pt[i].expo=0;
p3.pt[i].coef=0;
}
for(i=0; ip1.degree; i++)
{
for(j=0; jp2.degree; j++)
{
p3.pt[k].expo=p1.pt[i].expo+p2.pt[j].expo;
printf(곱셈결과p3.pt[%d].expo=%d\n,k,p3.pt[k].expo);
p3.pt[k].coef=p1.pt[i].coef*p2.pt[j].coef;
printf(곱셈결과p3.pt[%d].coef=%f\n,k,p3.pt[k].coef);
k++;
}
}
return p3;
/*
while(a_index= p1.degree && b_index)
{
if(p1.degreep2.degree)
{
p3.pt[0].coef=p1.pt[0].coef;
p1.degree--;
}
else if
*/
}
번호 | 제 목 | 글쓴이 | 날짜 |
---|---|---|---|
2693869 | stdio.h 는 왜 쓰는건가요? (1) | 큰꽃들 | 2025-05-06 |
2693842 | 포인터 변수의 주소값끼리 더하는 것에 대해서 질문드립니다. (1) | 진솔 | 2025-05-05 |
2693811 | 소수 출력;;;; | 화이트캣 | 2025-05-05 |
2693788 | 이런 함수는 없나요? (3) | 앤드류 | 2025-05-05 |
2693758 | txt파일 불러와서 행렬로 저장 | 큰애 | 2025-05-05 |
2693727 | scanf 오류 문제!! (2) | 큰나래 | 2025-05-04 |
2693704 | 구조체 주소록 문제인데 도와주세요 (2) | 도1도캣 | 2025-05-04 |
2693676 | 열혈강의 c언어 질문입니다 | 하양이 | 2025-05-04 |
2693647 | 12.620000 을요 12.620 으로 어떻게 표현해요? (2) | 파도 | 2025-05-04 |
2693619 | 타이틀 코드.. | 단순드립 | 2025-05-03 |
2693591 | 컴파일 에러에서 질문드립니다 (3) | 게자리 | 2025-05-03 |
2693463 | 동적할당 이용시 fwrite사용을 어떻게 해야하나요..? (10) | 일본어못해요 | 2025-05-02 |
2693387 | 배열문제입니다 수정오류캡쳐했습니다 (6) | 연하얀 | 2025-05-01 |
2693356 | text 입출력 내림차순 질문입니다 ㅠ | 빛글 | 2025-05-01 |
2693328 | C언어를이용해서 .txt파일 외에 다른 확장자 파일 삭제가 가능한지.. (2) | 대나무 | 2025-05-01 |
2693299 | 파일입출력 바이너리파일 | 독특한 | 2025-04-30 |
2693273 | 오류 (1) | 귀1여운렩 | 2025-04-30 |
2693080 | visual studio 2008 express edition 등록키 말인데요 | 얀별 | 2025-04-28 |
2693053 | 배열, 구조체 관련 프로그래밍 질문드립니다. | 싸리 | 2025-04-28 |
2693025 | 프로그램을 짜봤는데요 ㅠㅠ | 상처입은마음 | 2025-04-28 |