格式化我的输出方程

时间:2013-11-01 06:08:32

标签: c++ arrays

尝试打印多项式i.s 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<< "+";

}

1 个答案:

答案 0 :(得分:1)

使用:

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

此外,breakp->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<< "+";
    }