数组存储或计算错误
Polynomial::Polynomial( const char*up){
char *po = new char [100];
int i,j,coef,power;
coef = 0;
power = 0;
for(int k = 0;k<10;k++){
coefficients_[k] = 0;
}//end for
for(i = 0; i < 100; i++){
po[i] = up[i];
if(up[i] == '\0')
break;
}//copy the array.
for(j = 0; j < 100; j++){
if( po[j] == '-'){//negative item
if( po[j+1] == ' '){//negative item with space before number
if( po[j+3]== 'x' || po[j+4] == 'x'||po[j+5] == 'x'){//negative item with x signal
if(po[j+3] == 'x'){//single digit
coef = (-1)* (po[j+2]-48);
power = po[j+4] - 48;
}
if (po[j+4] == 'x'){//ten digit
coef = (-1)*( ((po[j+2]-48)* 10 )+ (po[j+3]- 48));
power = po[j+5] - 48;
}
if(po[i+5] == 'x'){//hundred digit
coef = (-1)*( ((po[j+2]-48)* 100) + ((po[j+3]-48)* 10) + (po[j+4] - 48));
power = po[i+6] - 48;
}
coefficients_[power] = coef;
}
else if(po[j+3] ==' '||po[j+4] == ' '||po[j+5] == ' ')//negative item without x signal
{
power = 0;
if(po[j+3] == ' '){//single digit
coef = (-1) * (po[j+2]- 48);
}
if(po[j+4] == ' '){//ten digit
coef = (-1)*( ((po[j+2]-48)* 10) + (po[j+3]- 48));
}
if(po[j+5] == ' '){//hundred digit
coef = (-1)*( ((po[j+2]-48)* 100) + ((po[j+3]- 48)* 10 )+ (po[j+4] - 48));
}
coefficients_[power] = coef;
}//end if else
}
if(po[j+2] == 'x' || po[j+3] == 'x'||po[j+4] == 'x'){//negative item with x signal
if(po[j+2] == 'x'){//single digit
coef = (-1)* (po[j+1]-48);
power = po[j+3] - 48;
}
if (po[j+3] == 'x'){//single digit
coef = (-1)*( ((po[j+1]-48)* 10 )+ (po[j+2]- 48));
power = po[j+4] - 48;
}
if(po[i+4] == 'x'){//single digit
coef = (-1)*( ((po[j+1]-48)* 100 )+ ((po[j+2]- 48)* 10) + (po[j+3] - 48));
power = po[i+5] - 48;
}
coefficients_[power] = coef;
}//end if if
else if(po[j+2] ==' '||po[j+3] == ' '||po[j+4] == ' ')//negative item without x signal
{
power = 0;
if(po[j+2] == ' '){//single digit
coef = (-1) * (po[j+1]- 48);
}
if(po[j+3] == ' '){//ten digit
coef = (-1)*( ((po[j+1]-48)* 10) + (po[j+2]- 48));
}
if(po[j+4] == ' '){//hundred digit
coef = (-1)*( ((po[j+1]-48)* 100) +((po[j+2]- 48)* 10) + (po[j+3] - 48));
}
coefficients_[power] = coef;
}//end if else
}//end if
else {//positive item
if(po[j+2] == 'x' || po[j+3] == 'x'||po[j+4] == 'x'){//positive item with x signal
if(po[j+2] == 'x'){
coef = po[j+1]-48;
power = po[j+3] - 48;
}
if (po[j+3] == 'x'){
coef = (po[j+1]-48)* 10 + (po[j+2]- 48);
power = po[j+4] - 48;
}
if(po[i+4] == 'x'){
coef = (po[j+1]-48)* 100 + (po[j+2]- 48)* 10 + (po[j+3] - 48);
power = po[i+5] - 48;
}
coefficients_[power] = coef;
}//end else if
else if(po[j+2] ==' '||po[j+3] == ' '||po[j+4] == ' ')//item without x signal
{
power = 0;
if(po[j+2] == ' '){
coef = po[j+1]- 48;
}
if(po[j+3] == ' '){
coef = (po[j+1]-48)* 10 + (po[j+2]- 48);
}
if(po[j+4] == ' '){
coef = (po[j+1]-48)* 100 + (po[j+2]- 48)* 10 + (po[j+3] - 48);
}
coefficients_[power] = coef;
}//end else if else if
if(po[j] == '\0') {
break;
}//end if
}//end else if
if(po[j] == '+'||po[j] == ' '){ }
}//end for loop
}//end user constructor输入a = new Polynomial("-12 + 7x2 + 7x9 - 21x5 - 11x7")
需要得出-12,0,7,0,0,-21,0,-11,0,7
现在结果为-3,0,7,0,0,1,0,1,0,7
正数及power部分正确 唯一问题是负数部分计算出错 求解决方法
...全文


