尝试打印多项式,即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<< "+";

}

最佳答案

采用:

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<< "+";
    }

09-27 05:54