确定两个多项式相乘结果的项数
Determining the number of terms in the result of the multiplication of two polynomials
这是一个对多项式执行不同运算的 c++ 程序。在此程序中,multiplication()
函数存在逻辑问题。我无法计算出结果多项式中的项数,这是两个多项式相乘的结果。
该程序在大多数情况下给出了正确的乘法输出,但在少数情况下乘法未正确执行。以下是完整的程序。
#include<iostream>
using namespace std;
struct poly
{
int coef, pow;
};
int k=0;
poly* getdata(poly *a,int n)
{
for(int i=0; i<n; i++)
{
cout<<"\n Enter the coefficient and power of the term";
cin>>a[i].coef>>a[i].pow;
}
return a;
}
poly* add(poly *c, poly *a, poly *b, int n1, int n2)
{
int i=0, j=0;
while(i<n1 && j<n2)
{
if(a[i].pow<b[j].pow)
{
c[k].pow = a[i].pow;
c[k].coef = a[i].coef;
i++;
k++;
}
else if(a[i].pow == b[j].pow)
{
c[k].pow = a[i].pow;
c[k].coef = a[i].coef+b[j].coef;
i++; j++; k++;
}
else
//(a[i].pow > b[j].pow)
{
c[k].pow = b[j].pow;
c[k].coef = b[j].coef;
j++; k++;
}
}
while(i<n1)
{
c[k].pow = a[i].pow;
c[k].coef = a[i].coef;
i++;
k++;
}
while(j<n2)
{
c[k].pow = b[j].pow;
c[k].coef = b[j].coef;
j++; k++;
}
return c;
}
void display(poly *p,int n)
{
int i;
cout<<"\n The polynomial is :-\n";
for(i=0; i<n-1; i++)
{
cout<<p[i].coef<<"x^"<<p[i].pow<<"+";
}
cout<<p[i].coef<<"x^"<<p[i].pow;
cout<<endl;
}
void multiplication(poly *c, poly *a, poly* b, int n1, int n2)
{
int i, j;
//int count = 0;
for(i=0; i<n1*n2; i++)
{
c[i].coef = 0;
c[i].pow = 0;
}
for(i=0; i<n1; i++)
{
for(j=0; j<n2; j++)
{
c[i+j].coef = a[i].coef*b[j].coef + c[i+j].coef;
c[i+j].pow = a[i].pow + b[j].pow;
}
}
//return count;
}
int main()
{
poly a[10], b[10], c[10];
poly *p, *q , *result;
int n1, n2;
cout<<"\n Enter the number of terms in polynomial 1 : ";
cin>>n1;
p = getdata(a,n1);
display(p, n1);
cout<<"\n Enter the number of terms in polynomial 2 : ";
cin>>n2;
q = getdata(b,n2);
display(q, n2);
result = add(c, a, b, n1, n2);
cout<<"\n The Sum of the two polynomial is ";
display(result, k);
poly *mresult;
mresult = new poly[n1+n2];
multiplication(mresult, a, b, n1, n2);
cout<<"\n The Multiplication of the two polynomials is :- ";
display(mresult,n1+n2-1);
return 0;
}
少数情况下输出:-
Enter the number of terms in polynomial 1 : 2
Enter the coefficient and power of the term1
1
Enter the coefficient and power of the term3
9
The polynomial is :-
1x^1+3x^9
Enter the number of terms in polynomial 2 : 2
Enter the coefficient and power of the term1
3
Enter the coefficient and power of the term2
6
The polynomial is :-
1x^3+2x^6
The Sum of the two polynomial is
The polynomial is :-
1x^1+1x^3+2x^6+3x^9
The Multiplication of the two polynomials is :-
The polynomial is :-
1x^4+5x^12+6x^15
这是正确的。
但是考虑这种情况:-
Enter the number of terms in polynomial 1 : 2
Enter the coefficient and power of the term1
1
Enter the coefficient and power of the term3
9
The polynomial is :-
1x^1+3x^9
Enter the number of terms in polynomial 2 : 2
Enter the coefficient and power of the term1
3
Enter the coefficient and power of the term2
6
The polynomial is :-
1x^3+2x^6
The Sum of the two polynomial is
The polynomial is :-
1x^1+1x^3+2x^6+3x^9
The Multiplication of the two polynomials is :-
The polynomial is :-
1x^4+5x^12+6x^15
这是错误的。这里的乘法应该是:- x^4+2x^7+3x^12+6x^15
我试过的逻辑很少,例如:-
int multiplication(poly *c, poly *a, poly* b, int n1, int n2)
{
int i, j;
int count = 0;
for(i=0; i<n1*n2; i++)
{
c[i].coef = 0;
c[i].pow = 0;
}
for(i=0; i<n1; i++)
{
for(j=0; j<n2; j++)
{
c[count].coef = a[i].coef*b[j].coef + c[count].coef;
c[count].pow = a[i].pow + b[j].pow;
if(c[count].pow != c[count-1].pow) count++;
}
}
return count;
}
此函数返回多项式 c 中的项数。
注意:- c 是两个多项式 a 和 b 相乘的结果。
你想要更像这样的逻辑:
for(i=0; i<n1; i++)
{
for(j=0; j<n2; j++)
{
int coef = a[i].coef * b[j].coef;
int pow = a[i].pow + b[j].pow;
count = add_term_to(c,count,coef,pow);
}
}
现在您只需要实现 add_term_to()
以将具有相同幂的项组合起来。
当f和g是x的多项式时,[=14的次数=]f×g是f的次数加上g的次数.结果多项式的系数将是几对乘积的总和; 例如 乘以 (f_2 x^2 + f_1 x + f_0)(g_2 x^2 + g_1 x + g_0), x^2 的系数将为 f_2*g_0 + f_1*g_1 + f_0*g_2。您不会事先知道这些总和是否为零。二项式 (x+1)(x+1) 有三个非零项,但 (x+1)(x-1) 有两个,而 (x^2+1)(x^4+x^3) 有四个.
您可以通过对一个向量中的系数求和来高效地计算此多项式,该向量的索引是每一项的次数。您可以在转换回来时消除与零相同的项。
这是一个对多项式执行不同运算的 c++ 程序。在此程序中,multiplication()
函数存在逻辑问题。我无法计算出结果多项式中的项数,这是两个多项式相乘的结果。
该程序在大多数情况下给出了正确的乘法输出,但在少数情况下乘法未正确执行。以下是完整的程序。
#include<iostream>
using namespace std;
struct poly
{
int coef, pow;
};
int k=0;
poly* getdata(poly *a,int n)
{
for(int i=0; i<n; i++)
{
cout<<"\n Enter the coefficient and power of the term";
cin>>a[i].coef>>a[i].pow;
}
return a;
}
poly* add(poly *c, poly *a, poly *b, int n1, int n2)
{
int i=0, j=0;
while(i<n1 && j<n2)
{
if(a[i].pow<b[j].pow)
{
c[k].pow = a[i].pow;
c[k].coef = a[i].coef;
i++;
k++;
}
else if(a[i].pow == b[j].pow)
{
c[k].pow = a[i].pow;
c[k].coef = a[i].coef+b[j].coef;
i++; j++; k++;
}
else
//(a[i].pow > b[j].pow)
{
c[k].pow = b[j].pow;
c[k].coef = b[j].coef;
j++; k++;
}
}
while(i<n1)
{
c[k].pow = a[i].pow;
c[k].coef = a[i].coef;
i++;
k++;
}
while(j<n2)
{
c[k].pow = b[j].pow;
c[k].coef = b[j].coef;
j++; k++;
}
return c;
}
void display(poly *p,int n)
{
int i;
cout<<"\n The polynomial is :-\n";
for(i=0; i<n-1; i++)
{
cout<<p[i].coef<<"x^"<<p[i].pow<<"+";
}
cout<<p[i].coef<<"x^"<<p[i].pow;
cout<<endl;
}
void multiplication(poly *c, poly *a, poly* b, int n1, int n2)
{
int i, j;
//int count = 0;
for(i=0; i<n1*n2; i++)
{
c[i].coef = 0;
c[i].pow = 0;
}
for(i=0; i<n1; i++)
{
for(j=0; j<n2; j++)
{
c[i+j].coef = a[i].coef*b[j].coef + c[i+j].coef;
c[i+j].pow = a[i].pow + b[j].pow;
}
}
//return count;
}
int main()
{
poly a[10], b[10], c[10];
poly *p, *q , *result;
int n1, n2;
cout<<"\n Enter the number of terms in polynomial 1 : ";
cin>>n1;
p = getdata(a,n1);
display(p, n1);
cout<<"\n Enter the number of terms in polynomial 2 : ";
cin>>n2;
q = getdata(b,n2);
display(q, n2);
result = add(c, a, b, n1, n2);
cout<<"\n The Sum of the two polynomial is ";
display(result, k);
poly *mresult;
mresult = new poly[n1+n2];
multiplication(mresult, a, b, n1, n2);
cout<<"\n The Multiplication of the two polynomials is :- ";
display(mresult,n1+n2-1);
return 0;
}
少数情况下输出:-
Enter the number of terms in polynomial 1 : 2
Enter the coefficient and power of the term1
1
Enter the coefficient and power of the term3
9
The polynomial is :-
1x^1+3x^9
Enter the number of terms in polynomial 2 : 2
Enter the coefficient and power of the term1
3
Enter the coefficient and power of the term2
6
The polynomial is :-
1x^3+2x^6
The Sum of the two polynomial is
The polynomial is :-
1x^1+1x^3+2x^6+3x^9
The Multiplication of the two polynomials is :-
The polynomial is :-
1x^4+5x^12+6x^15
这是正确的。 但是考虑这种情况:-
Enter the number of terms in polynomial 1 : 2
Enter the coefficient and power of the term1
1
Enter the coefficient and power of the term3
9
The polynomial is :-
1x^1+3x^9
Enter the number of terms in polynomial 2 : 2
Enter the coefficient and power of the term1
3
Enter the coefficient and power of the term2
6
The polynomial is :-
1x^3+2x^6
The Sum of the two polynomial is
The polynomial is :-
1x^1+1x^3+2x^6+3x^9
The Multiplication of the two polynomials is :-
The polynomial is :-
1x^4+5x^12+6x^15
这是错误的。这里的乘法应该是:- x^4+2x^7+3x^12+6x^15
我试过的逻辑很少,例如:-
int multiplication(poly *c, poly *a, poly* b, int n1, int n2)
{
int i, j;
int count = 0;
for(i=0; i<n1*n2; i++)
{
c[i].coef = 0;
c[i].pow = 0;
}
for(i=0; i<n1; i++)
{
for(j=0; j<n2; j++)
{
c[count].coef = a[i].coef*b[j].coef + c[count].coef;
c[count].pow = a[i].pow + b[j].pow;
if(c[count].pow != c[count-1].pow) count++;
}
}
return count;
}
此函数返回多项式 c 中的项数。
注意:- c 是两个多项式 a 和 b 相乘的结果。
你想要更像这样的逻辑:
for(i=0; i<n1; i++)
{
for(j=0; j<n2; j++)
{
int coef = a[i].coef * b[j].coef;
int pow = a[i].pow + b[j].pow;
count = add_term_to(c,count,coef,pow);
}
}
现在您只需要实现 add_term_to()
以将具有相同幂的项组合起来。
当f和g是x的多项式时,[=14的次数=]f×g是f的次数加上g的次数.结果多项式的系数将是几对乘积的总和; 例如 乘以 (f_2 x^2 + f_1 x + f_0)(g_2 x^2 + g_1 x + g_0), x^2 的系数将为 f_2*g_0 + f_1*g_1 + f_0*g_2。您不会事先知道这些总和是否为零。二项式 (x+1)(x+1) 有三个非零项,但 (x+1)(x-1) 有两个,而 (x^2+1)(x^4+x^3) 有四个.
您可以通过对一个向量中的系数求和来高效地计算此多项式,该向量的索引是每一项的次数。您可以在转换回来时消除与零相同的项。