다항식 곱셈 질문이요 급합니다 도움좀!!
다온
요약하자면 첨부파일에 있는 파일처럼 항갯수 차수 계수로 두개의 다항식 정보가 저장되있구요
그걸 불러내서 다항식의 곱을 하는 함수를 만든다음 곱한 다항식을 리턴해서 보여주는겁니다질문 내용 : 현재 진행상황은 파일에서 정보 받은후에 곱셈을 해서 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
*/}