数组存储或计算错误
本帖最后由 littleTD 于 2013-06-19 09:26:10 编辑
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
#include <sstream>
//user constructor
Polynomial::Polynomial( const char*up){
std::istringstream istr(up);
int n = 0;
char c = '+';
for(int k = 0;k<10;k++){
coefficients_[k] = 0;
}
while(istr >> n)
{
if(c == '-')
n = -n;
istr >> c;
while(c == ' ')
istr >> c;
int power = 0;
if(c == 'x')
{
istr >> power;
istr >> c;
}
coefficients_[power] += n;
}
}
没看出你要的输出是一个啥子规律的东西.
1. 你已经处理过的字符都不跳过的吗? 下次循环还继续处理已经处理过的?
2. 你的 power 变量表示的什么? 是输出的下标还是上一次的输入值? 还是什么其他的含义?
尝试过跳过但是结果一样
power是指数 这里指数和在数列中的位置是一致的
明白你的意思了, 不过你写的太复杂了, 看起来实在头疼, 而且里面跳空格那些完全没准啊, 参考这个版本:
#include <sstream>
//user constructor
Polynomial::Polynomial( const char*up){
std::istringstream istr(up);
int n = 0;
char c = '+';
for(int k = 0;k<10;k++){
coefficients_[k] = 0;
}
while(istr >> n)
{
if(c == '-')
n = -n;
istr >> c;
while(c == ' ')
istr >> c;
int power = 0;
if(c == 'x')
{
istr >> power;
istr >> c;
}
coefficients_[power] += n;
}
}
////////////////////////////////////////////////////////////
// N字母一元n次多项式的分割,不带容错处理,不处理带括号的情况。
char * ploynSplit(const char * s,const char **end,char *item)
{
if(s==NULL
[解决办法]
*s=='\0')return NULL;
while(*s==' ')s++;//前空格
if(*s=='+'
[解决办法]
*s== '-')*item++= *s++;//符号
while(*s==' ')s++;//后空格
while(isdigit(*s) )*item++=*s++; //系数的整数部分
if(*s == '.') *item++=*s++; //系数的小数点
while(isdigit(*s) )*item++=*s++; //系数的尾数
if(*s =='e'
[解决办法]
*s=='E')*item++=*s++; //系数的指数标志
if(*s=='+'
[解决办法]
*s== '-')*item++= *s++; //系数的指数的符号
while(isdigit(*s) )*item++=*s++; //系数的指数数值
//if(*s=='x')
if (isalpha(*s)){
*item++ = *s++; //未知数
while(*s==' ')s++;//未知数指数前空格
while(isdigit(*s)) *item++ = *s++; //未知数的指数
}
while(*s==' ')s++;//后空格
*end=s;
*item ='\0';
return item;
}
int splitItem(const char *s ,double *coef,char *alpha,int *exp)
{
int sign =1;
if(*s=='+') s++;
else if(*s=='-'){sign=-1;s++;}
const char* pre = s;
//while(!isalpha(*s))s++;
while(isdigit(*s)) s++;
if(*s=='.')s++;
while(isdigit(*s))s++;//系数的整数部分
if(*s == '.') s++; //系数的小数点
while(isdigit(*s) )*s++; //系数的尾数
if(*s =='e'
[解决办法]
*s=='E')s++; //系数的指数标志
if(*s=='+'
[解决办法]
*s== '-')s++; //系数的指数的符号
while(isdigit(*s) )*s++; //系数的指数数值
if(s != pre)
*coef = atof(pre);
else *coef =sign;
pre=s;
while(isalpha(*s)) *alpha++= *s++;
*alpha='\0';
if(s== pre) *exp=0;
else if(!*s) *exp=1 ;
else *exp = atoi(s);
return 0;
}
int ploynGetItems(char *s,char Items[][80]){
const char *next=s;
char *item=s;
int n=0;
while(item && *next ){
item=ploynSplit(next,&next,Items[n]);
if(item)n++;
}
return n;
}
int main(int argc, char* argv[])
{
char a[]=" x11 + x10 -x9 +1234x8 + 5x7 +6x3 + 2x2 +x +10 ";
char Items[20][80];
int n = ploynGetItems(a,Items);
double coef;
int exp;
char alpha[80];
for(int i=0;i<n;i++){
printf("%s \n",Items[i]);
splitItem(Items[i],&coef,alpha,&exp);
printf("%lg %s ^ %d \n",coef,alpha,exp);
}
//cout<<Items[i]<<endl;
printf("Hello World!\n");
getchar();
return 0;
}