数组存储或计算错误

creepyOne 2013-06-19 09:15:35
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部分正确 唯一问题是负数部分计算出错 求解决方法
...全文
243 22 打赏 收藏 转发到动态 举报
AI 作业
写回复
用AI写文章
22 条回复
切换为时间正序
请发表友善的回复…
发表回复
www_adintr_com 2013-06-19
  • 打赏
  • 举报
回复
引用 12 楼 littleTD 的回复:
[quote=引用 11 楼 adlay 的回复:] 没看出你要的输出是一个啥子规律的东西. 1. 你已经处理过的字符都不跳过的吗? 下次循环还继续处理已经处理过的? 2. 你的 power 变量表示的什么? 是输出的下标还是上一次的输入值? 还是什么其他的含义?
尝试过跳过但是结果一样 power是指数 这里指数和在数列中的位置是一致的 [/quote] 明白你的意思了, 不过你写的太复杂了, 看起来实在头疼, 而且里面跳空格那些完全没准啊, 参考这个版本:

#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;
	}
}
creepyOne 2013-06-19
  • 打赏
  • 举报
回复
引用 11 楼 adlay 的回复:
没看出你要的输出是一个啥子规律的东西. 1. 你已经处理过的字符都不跳过的吗? 下次循环还继续处理已经处理过的? 2. 你的 power 变量表示的什么? 是输出的下标还是上一次的输入值? 还是什么其他的含义?
我换了一种写法能帮忙看一下么
creepyOne 2013-06-19
  • 打赏
  • 举报
回复
Polynomial::Polynomial( const char*up){
    int coef;
    int power;
    char* cur;
    char* exp;
    char* po = strdup(up);
    bool flag = false;

    cur = strtok(po, " ");
    cout << "term is " << cur << "\n";
    coef = atoi(cur);
    exp = strtok(NULL, " ");
    if(exp == cur){
        power = 0;
    } else {
        power = atoi(exp);
    }
    coefficients_[power] = coef;
	

    while(!flag){
        cur = strtok(NULL, " +-");
        if(cur == NULL){
            flag = true;
        } else {
            cout << "term is " << cur << "\n";
            coef = atoi(cur);
			exp = strtok(NULL, " ");
            if(exp == cur){
                power = 0;
		    } else {
                power = atoi(exp);
			}
            coefficients_[power] = coef;
        }
    }
	delete [ ] po;
}
重新写了一遍 但是这次是都不能存进数组相应的位置
creepyOne 2013-06-19
  • 打赏
  • 举报
回复
引用 11 楼 adlay 的回复:
没看出你要的输出是一个啥子规律的东西. 1. 你已经处理过的字符都不跳过的吗? 下次循环还继续处理已经处理过的? 2. 你的 power 变量表示的什么? 是输出的下标还是上一次的输入值? 还是什么其他的含义?
尝试过跳过但是结果一样 power是指数 这里指数和在数列中的位置是一致的
www_adintr_com 2013-06-19
  • 打赏
  • 举报
回复
没看出你要的输出是一个啥子规律的东西. 1. 你已经处理过的字符都不跳过的吗? 下次循环还继续处理已经处理过的? 2. 你的 power 变量表示的什么? 是输出的下标还是上一次的输入值? 还是什么其他的含义?
creepyOne 2013-06-19
  • 打赏
  • 举报
回复
引用 9 楼 lm_whales 的回复:
一个项,包含 1)符号 //+,- 或者无任何符号 符号的取值方法: 无符号===》 +1,+===》+1,- ===》-1 2)数值 //整数或者浮点数 读取浮点数或者整数 3)未知数 //x,y,z 等等 4)指数 //整数 读取整数 如果没有系数,则系数为1 首先 做一个函数用+,- 作为分隔符,把表达式,符号属于+,-号后项 其次 做一个函数 分析每一项 系数最终等于 系数*符号 这样一项就分析出来了,每一项都这样分析,直到最后一项,程序结束; 所有空格等空白符号直接滤除即可。
不允许添加任何函数 只允许照头文件写
lm_whales 2013-06-19
  • 打赏
  • 举报
回复
一个项,包含 1)符号 //+,- 或者无任何符号 符号的取值方法: 无符号===》 +1,+===》+1,- ===》-1 2)数值 //整数或者浮点数 读取浮点数或者整数 3)未知数 //x,y,z 等等 4)指数 //整数 读取整数 如果没有系数,则系数为1 首先 做一个函数用+,- 作为分隔符,把表达式,符号属于+,-号后项 其次 做一个函数 分析每一项 系数最终等于 系数*符号 这样一项就分析出来了,每一项都这样分析,直到最后一项,程序结束; 所有空格等空白符号直接滤除即可。
creepyOne 2013-06-19
  • 打赏
  • 举报
回复
引用 6 楼 zyaiwx 的回复:
你j的偏移不要再循环里使用,在每种情况下使用不同偏移,就好了。 而且这个程序不利于扩展,连位数都写死了。其实你输入有空格的话,你可以查找空格,然后查找x的位置,如果x在空格以后,说明找到了一个power==0的项,以此类推。
求实例 空格查找的话 如何判断之前的数字? 查找x的话之前之后如何操作?
creepyOne 2013-06-19
  • 打赏
  • 举报
回复
引用 5 楼 zyaiwx 的回复:
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
说实话,调试了一下,你这个问题还是挺多的。 比如,假设你输入的是-12+7x2+... 如果你判断第一个字母是负数,这个没错,然后看x的位置,也是没错的。 完成第一个判断后,j+1,为什么要加一呢?你这几个数都判断完了,这样滑动的时候,遇到空格之类的,就进入别的循环了。你都判断是两位数了,为什么j不加3.判断下一个? 你想这边j==1的时候,判断j+2也就是加号之前出现了空,j+4也是空。。。这你把power=0的情况赋值了n次,而第一次是对的。
因为负数有两种情况 一种是空格 负号 空格一个数 一种是开头就是负数 之后没有空格 所以不得不判断一次
zybjtu 2013-06-19
  • 打赏
  • 举报
回复
你j的偏移不要再循环里使用,在每种情况下使用不同偏移,就好了。 而且这个程序不利于扩展,连位数都写死了。其实你输入有空格的话,你可以查找空格,然后查找x的位置,如果x在空格以后,说明找到了一个power==0的项,以此类推。
zybjtu 2013-06-19
  • 打赏
  • 举报
回复
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
说实话,调试了一下,你这个问题还是挺多的。 比如,假设你输入的是-12+7x2+... 如果你判断第一个字母是负数,这个没错,然后看x的位置,也是没错的。 完成第一个判断后,j+1,为什么要加一呢?你这几个数都判断完了,这样滑动的时候,遇到空格之类的,就进入别的循环了。你都判断是两位数了,为什么j不加3.判断下一个? 你想这边j==1的时候,判断j+2也就是加号之前出现了空,j+4也是空。。。这你把power=0的情况赋值了n次,而第一次是对的。
creepyOne 2013-06-19
  • 打赏
  • 举报
回复
引用 1 楼 zyaiwx 的回复:
一大堆if,这么长的程序,思路也简单,调试下就好了啊。 要么把完整代码发出来,大家调试下,也能节约时间
我贴出来了 求指导
creepyOne 2013-06-19
  • 打赏
  • 举报
回复
主方法
#include "polynomial.h"

#include <iostream>
using std::cout;
using std::cin;
using std::endl;

#include <iomanip>
using std::setw;

void show(Polynomial* p) {
	cout << "Coefficients are: " << p->coefficients_[0];
	for (int i=1; i<10; i++) {
		cout << "," << p->coefficients_[i];
	}
	cout << endl;
}

int main()
{
	int i;

	cout << "Start of default constructor, Polynomial(), tests..." << endl;
	Polynomial *a;
	Polynomial b, c;
	a = new Polynomial();
	show(a);
	cout << "End of Polynomial() tests." << endl << endl;

	cout << "Start of user-defined constructor, Polynomial(const char*), tests..." << endl;
    	a = new Polynomial("-12 + 7x2 + 7x9 - 21x5 - 11x7");
	show(a);
	delete a;

	a = new Polynomial("-2x9 + 7x7 - 18x3 + 6x1 - 14");
	show(a);
	delete a;
	cout << "End of Polynomial(const char*) tests." << endl << endl;

	cout << "Start of >> and << tests..." << endl;
	cout << "Please enter: 3x9 - 6x7 - 18x3 + 6x1 + 14" << endl;
	cin >> b;
	cout << b << endl;
	cout << "Please enter: -4x7 + 12x4 + 9x1 - 9" << endl;
	cin >> c;
	cout << c << endl;
	cout << "End of >> and << tests." << endl << endl;

	cout << "Start of copy constructor tests..." << endl;
	Polynomial d(b);
	cout << d << endl; 
	Polynomial e(c); 
	cout << e << endl;
	cout << "End of copy tests...." << endl << endl;

	Polynomial f("3x9 - 6x7 - 18x3 + 6x1 + 14");
	Polynomial g("-2x9 + 7x7 - 18x3 + 6x2 - 11");
	Polynomial h("-4x7 + 12x4 + 9x1 - 9");

	cout << "Start of + tests..." << endl;
	e = f + g;
	cout << e << endl;
	e = f + h;
	cout << e << endl;
	cout << "End of + tests...." << endl << endl; 

	cout << "Start of - tests..." << endl;
  	e = f - g;
	cout << e << endl;
	e = f - h;
	cout << e << endl;
	cout << "End of - tests...." << endl << endl; 

	cout << "Start of * tests..." << endl;
	e = f * g;
	cout << e << endl;
	e = f * h;
	cout << e << endl;
	cout << "End of * tests...." << endl << endl;   
	 
	cout << "Start of += tests..." << endl;
	e = f;	
	e += g;	
	cout << e << endl;
	e = f;	
	e += h;	
	cout << e << endl;
    cout << "End of += tests...." << endl << endl; 

	cout << "Start of -= tests..." << endl;
	e = f;	
	e -= g;	
	cout << e << endl;
	e = f;	
	e -= h;	
	cout << e << endl;
	cout << "End of -= tests...." << endl << endl; 

	cout << "Start of *= tests..." << endl;
	e = f;	
	e *= g;	
	cout << e << endl;
	e = f;	
	e *= h;	
	cout << e << endl;
	cout << "End of *= tests...." << endl << endl;
      
	cout << "Start of [] tests..." << endl;
	for (i = 9; i >= 0; i--)
		cout << "e[" << setw(1) << i << "] = " << setw(1) << e[i] << "\t";
	cout << endl;
	for (i = 9; i >= 0; i--)
		e[i] = i;
	for (i = 9; i >= 0; i--)
		cout << "e[" << setw(1) << i << "] = " << setw(1) << e[i] << "\t";
	cout << endl;
	cout << "End of [] tests...." << endl << endl;
  
	cout << "Start of () tests..." << endl;
	cout << "Evaluation = " << e(1) << endl;
	cout << "Evaluation = " << e(4) << endl;
	cout << "End of () tests...." << endl;

	return 0;
}



creepyOne 2013-06-19
  • 打赏
  • 举报
回复
头文件
#ifndef POLYNOMIAL_H
#define POLYNOMIAL_H

#include <iostream>
#include <iomanip>
#include <cmath>
#include <cstring>
using namespace std;

class Polynomial {

   friend istream& operator>>(istream& in, Polynomial& v);
   friend ostream& operator<<(ostream& out, const Polynomial& v);
   friend void show(Polynomial* p);

public:
   Polynomial();
   Polynomial( const Polynomial& cp );
   Polynomial( const char *up);
   Polynomial operator+( const Polynomial& p ) const;
   Polynomial operator-( const Polynomial& p) const;
   Polynomial operator*( const Polynomial& p);
   Polynomial& operator+=( const Polynomial& e);
   Polynomial& operator-=( const Polynomial& e);
   Polynomial& operator*=( const Polynomial& e);
   int operator[]( int i) const;
   int& operator[]( int j);
   int operator()( int x );

private:
   int coefficients_[10];
};

#endif
函数方法
#include "Polynomial.h"
#include <iostream>
#include <iomanip>
#include <cmath>
#include <cstring>

using namespace std;

//insert method
istream& operator>>(istream& in, Polynomial& v){
	Polynomial();
	char* temp = new char [50];	
	in.getline (temp,50,'\0');
	v = Polynomial(temp);

	return in;
}//end istream

//output method
ostream& operator<<(ostream& out,const Polynomial& v){
	out<<v.coefficients_[0];

	for(int i = 1; i<10; i++){	

		if(v.coefficients_[i] == 0){}

		if(v.coefficients_[i] > 0){
			out<<" "<<"+"<<" "<<v.coefficients_ [i];
		}else{
			out<<" "<<"-"<<" "<<(-1)*v.coefficients_[i];		
		}
	}
	return out;
}//end ostream


//default constructor
Polynomial::Polynomial(){
	int i = 0;
	for(;i<10;i++){
		coefficients_[i] = 0;
	}//end for	
	
}//end Polynomial

//copy constructor
Polynomial::Polynomial( const Polynomial& cp){
    char* arr = new char [10]; 
    for(int i=0;i<10;i++){  
        arr[i] = cp.coefficients_[i];  
    }//end for
}//end copy

//user constructor
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

//Polynomials add
Polynomial Polynomial::operator+(const Polynomial &p) const{
	Polynomial temp;	
	int i;

	for( i = 0; i < 10; i++){
		temp.coefficients_[i] = coefficients_[i]+ p.coefficients_[i];
	}//end for
	return temp;
}//end operator+

//Polynomials substract
Polynomial Polynomial::operator-(const Polynomial &p) const{
	Polynomial temp;
	int i;

	for( i = 0; i < 10; i++){
		temp.coefficients_[i] =coefficients_[i] - p.coefficients_[i];
	}//end for
	return temp;
}//end operator-

//Polynomials multipaly
Polynomial Polynomial::operator*( const Polynomial& p){
	Polynomial temp;
	int i,j;

	for(i = 0; i < 10; i++){
		for(j = 0; j < 10; i++){
			temp.coefficients_[i] = coefficients_[i] * p.coefficients_[j];
		}//end j for
	}//end i for
	return temp;
}//end operator*

//modify polynomial add
Polynomial &Polynomial::operator+=( const Polynomial& e){
	Polynomial temp;	
	int i;

	for( i = 0; i < 10; i++){
		temp.coefficients_[i] = coefficients_[i]+ e.coefficients_[i];
	}//end for
	return *this;  
}//end +=

//modify polynomial substract
Polynomial &Polynomial::operator-=( const Polynomial& e){
	Polynomial temp;
	int i;

	for( i = 0; i < 10; i++){
		temp.coefficients_[i] =coefficients_[i] - e.coefficients_[i];
	}//end for
	return *this;
}//end -=

//modify polynomial multiply
Polynomial &Polynomial::operator*=( const Polynomial& e){
	Polynomial temp;
	int i,j;

	for(i = 0; i < 10; i++){
		for(j = 0; j < 10; i++){
			temp.coefficients_[i] = coefficients_[i] * e.coefficients_[j];
		}//end j for
	}//end i for
	return *this;
}//end *=

//set coefficient
int Polynomial::operator[]( int i ) const{
	return coefficients_[i];
}//end set

//get coefficient
int& Polynomial::operator[]( int j){
	return coefficients_[j];
}//end get

//evaluate the polynomial
int Polynomial::operator()( int x ){
	int res=0;  
    for(int i=0;i<10; i++){  
        res += (int)(coefficients_[i] * pow((double)x,(double)i));  
    }  
    return res;  
}//end evaluate
zybjtu 2013-06-19
  • 打赏
  • 举报
回复
一大堆if,这么长的程序,思路也简单,调试下就好了啊。 要么把完整代码发出来,大家调试下,也能节约时间
失散糖 2013-06-19
  • 打赏
  • 举报
回复
每天回帖即可获得10分可用分!
lm_whales 2013-06-19
  • 打赏
  • 举报
回复
 
if (isalpha(*s))    {
         *item++ = *s++;      //未知数         
           while(*s==' ')   s++;//未知数指数前空格         
           while(isdigit(*s)) *item++ = *s++; //未知数的指数    
 } 
改成

   while(isalpha(*s))
        *item++ = *s++;//未知数 
    while(*s==' ')s++;//未知数指数前空格
     while(isdigit(*s)) *item++ = *s++; //未知数的指数
  
可以分析 N个字母的未知数; 可以添加代码,通过比较字符串,来检查未知数是否有错误(未知数不相同)来提高容错性。
lm_whales 2013-06-19
  • 打赏
  • 举报
回复
////////////////////////////////////////////////////////////
// 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;
}
刚刚写了一个给你做参考,效率不高。
www_adintr_com 2013-06-19
  • 打赏
  • 举报
回复
要调整的地方很多... 都没有对 x 的处理, 你如何提取出指数的... 你只有单步调试, 看哪里出的问题.
creepyOne 2013-06-19
  • 打赏
  • 举报
回复
引用 17 楼 lm_whales 的回复:
[quote=引用 15 楼 adlay 的回复:] [quote=引用 12 楼 littleTD 的回复:] [quote=引用 11 楼 adlay 的回复:] 没看出你要的输出是一个啥子规律的东西. 1. 你已经处理过的字符都不跳过的吗? 下次循环还继续处理已经处理过的? 2. 你的 power 变量表示的什么? 是输出的下标还是上一次的输入值? 还是什么其他的含义?
尝试过跳过但是结果一样 power是指数 这里指数和在数列中的位置是一致的 [/quote] 明白你的意思了, 不过你写的太复杂了, 看起来实在头疼, 而且里面跳空格那些完全没准啊, 参考这个版本:

#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;
	}
}
[/quote] 这个基本可用,只是没有处理特殊情况 例如 "-x2 +x -3" ;" x4 - 11x3 + 2x2 - x + 10" 遇到这种,不带系数的,以及很多空格的就会出问题,这些都是多项式的正常写法。 [/quote] 请问这样写可以么?或者说这样写需要调整些什么呢
Polynomial::Polynomial( const char*up){
    int coef;
    int power;
    char* cur;
    char* exp;
    char* po = strdup(up);
    bool flag = false;
 
    cur = strtok(po, " ");
    cout << "term is " << cur << "\n";
    coef = atoi(cur);
    exp = strtok(NULL, " ");
    if(exp == cur){
        power = 0;
    } else {
        power = atoi(exp);
    }
    coefficients_[power] = coef;
     
 
    while(!flag){
        cur = strtok(NULL, " +-");
        if(cur == NULL){
            flag = true;
        } else {
            cout << "term is " << cur << "\n";
            coef = atoi(cur);
            exp = strtok(NULL, " ");
            if(exp == cur){
                power = 0;
            } else {
                power = atoi(exp);
            }
            coefficients_[power] = coef;
        }
    }
    delete [ ] po;
}
加载更多回复(2)

65,187

社区成员

发帖
与我相关
我的任务
社区描述
C++ 语言相关问题讨论,技术干货分享,前沿动态等
c++ 技术论坛(原bbs)
社区管理员
  • C++ 语言社区
  • encoderlee
  • paschen
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
  1. 请不要发布与C++技术无关的贴子
  2. 请不要发布与技术无关的招聘、广告的帖子
  3. 请尽可能的描述清楚你的问题,如果涉及到代码请尽可能的格式化一下

试试用AI创作助手写篇文章吧