0

尝试打印出多项式邮件,即 10x^0+1*x^1 和 9*x^0+1*x^1 但是多项式打印为

10x^0+1*x^1 +9*x^0+1*x^1,这是我的for循环方程

 for(int i=0; i<=p->deg; i++) { 
     if (p->coeffs[i]==0)
       break; //dont want to print out any 0 constants

   cout << p->coeffs[i] <<" * " << x << "^"<<i << " ";

   if (p->coeffs[i]>0 && p->coeefs[i+1]!=0)
      cout<< "+";

}
4

1 回答 1

1

利用:

if (p->coeffs[i]>0 && (i != p->deg) )
      cout<< "+";

此外,您不应该 break在 时p->coeffs[i] == 0,可能是其他即将到来的系数不为零。

 if (p->coeffs[i]==0)
           continue; //dont want to print out any 0 constants

另外,所以我认为以下应该有效

 for(int i=0; i< p->deg; i++) { //Notice only < sign
     if (p->coeffs[i]==0)
       continue; //dont want to print out any 0 constants

   cout << p->coeffs[i] <<" * " << x << "^"<<i << " ";

   if ( p->coeffs[i+1] > 0 )
        cout<< "+";
    }
于 2013-11-01T06:14:05.957 回答