65,187
社区成员




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;
}
}
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;
}
重新写了一遍 但是这次是都不能存进数组相应的位置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次,而第一次是对的。
#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;
}
#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
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个字母的未知数;
可以添加代码,通过比较字符串,来检查未知数是否有错误(未知数不相同)来提高容错性。
////////////////////////////////////////////////////////////
// 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;
}
刚刚写了一个给你做参考,效率不高。
#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;
}