本文整理汇总了C++中Polynomial::addTerm方法的典型用法代码示例。如果您正苦于以下问题:C++ Polynomial::addTerm方法的具体用法?C++ Polynomial::addTerm怎么用?C++ Polynomial::addTerm使用的例子?那么, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类Polynomial
的用法示例。
在下文中一共展示了Polynomial::addTerm方法的10个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的C++代码示例。
示例1: addPoly
Polynomial* Polynomial::addPoly(Polynomial *b){
Term* af = first;
Term* bf = b->first;
Polynomial *c = new Polynomial();
while(af&&bf){
if(af->exp==bf->exp){
int sum = af->coef + bf->coef;
if(sum) c->addTerm(sum, af->exp );
af = af->link; bf = bf->link;
}
else if(af->exp<bf->exp){
c->addTerm(bf->coef, bf->exp);
bf = bf->link;
}
else{
c->addTerm(af->coef, af->exp);
af = af->link;
}
}
while(af){
c->addTerm(af->coef, af->exp);
af = af->link;
}
while(bf){
c->addTerm(bf->coef, bf->exp);
bf = bf->link;
}
return c;
}
示例2: add
/* add two polynomials together */
const Polynomial Polynomial::add( const Polynomial& rhs ) const {
Polynomial retVal;
for( itr_t it = _list.begin(); it != _list.end(); ++it )
retVal.addTerm( it.getData().coeff, it.getData().exp );
for( itr_t it = rhs._list.begin(); it != rhs._list.end(); ++it )
retVal.addTerm( it.getData().coeff, it.getData().exp );
return retVal;
}
示例3: get_eps22_term
Polynomial get_eps22_term(int a1,int a2,int a3,int dir) {
Polynomial ret;
if (dir!=1) return ret;
PolynomialTerm T;
if (a2==0) T.coefficient=-1; else T.coefficient=1;
for (int j=0; j<6; j++) T.exponents << 0;
if (a1==0) T.exponents[1]=1; else T.exponents[0]=1;
if (a3==0) T.exponents[5]=1; else T.exponents[4]=1;
ret.addTerm(T);
return ret;
}
示例4: term
Polynomial Polynomial::operator*(const Polynomial &P) {
Polynomial ret;
for (int i=0; i<termCount(); i++)
for (int j=0; j<P.termCount(); j++) {
PolynomialTerm T;
T.exponents.clear();
for (int k=0; k<6; k++) {
T.exponents << term(i).exponents[k]+P.term(i).exponents[k];
}
T.coefficient=term(i).coefficient*P.term(i).coefficient;
ret.addTerm(T);
}
return ret;
}
示例5: integrate
/* exponent and divide the coefficient by it */
const Polynomial Polynomial::integrate() {
Polynomial retVal;
for( itr_t it = _list.begin(); it != _list.end(); ++it )
{
int exp = it.getData().exp;
exp++;
double coeff = it.getIter()->_data->coeff / static_cast<double>(exp);
retVal.addTerm( coeff, exp );
// no way for a term to now be zero, so don't need to check
}
return retVal;
}
示例6: getPolynomial
// Gets user input and turns it into a polynomail
Polynomial getPolynomial(Polynomial& p)
{
p.clear();
// Temp term to put into a list
Term temp;
// Line of input poly from user
string line;
// Lists of terms and strings to build a poly
list<Term> terms;
list<string> strings;
// User input
cin.ignore();
getline(cin, line);
// Put + in front of any - as a delimiter for terms in a poly
int stringSearchPosition(0);
while (line.find('-', stringSearchPosition) != string::npos)
{
line.insert(line.find('-', stringSearchPosition), 1, '+');
stringSearchPosition = line.find('-', stringSearchPosition) + 1;
}
// Position markers
size_t prev(0), pos;
// Break up the user input into substrings with + as the delimeter of terms
while ((pos = line.find_first_of("+", prev)) != string::npos)
{
if (pos > prev)strings.push_back(line.substr(prev, pos - prev));
prev = pos + 1;
}
// Take care of the end of the line
if (prev < line.length())strings.push_back(line.substr(prev, std::string::npos));
// Put the list of substrings into terms then into a poly
for (std::list<std::string>::const_iterator i = strings.begin(); i != strings.end(); ++i)
{
Term t(*i);
p.addTerm(t);
}
// Return the poly
return p;
}
示例7: div
// doesn't return a remainder
// N.B. this function will break if there is a remainder
const Polynomial Polynomial::div( const Polynomial& rhs ) const {
Polynomial retVal;
if( degree() < rhs.degree() )
{
return retVal; // return 0
}
Polynomial rSide( *this );
int rDeg = rhs.degree();
double rCoeff = rhs._list.begin().getData().coeff;
itr_t it = rSide._list.begin();
while( 1 )
{
if( it == rSide._list.end() ) break;
int deg_diff = it.getData().degree() - rDeg;
if( deg_diff < 0 ) break; // TODO: check this condition, maybe need to put rest into remainder?
double coeff = it.getData().coeff / rCoeff;
Polynomial tmp;
Term multiplier( coeff, deg_diff );
retVal.addTerm( multiplier );
for( itr_t itt = rhs._list.begin(); itt != rhs._list.end(); ++itt )
{
Term res = itt.getData() * multiplier;
tmp.addTerm( res );
}
rSide = rSide.sub( tmp );
it = rSide._list.begin();
}
return retVal;
}
示例8: mult
/* multiply two polynomials together */
const Polynomial Polynomial::mult( const Polynomial& rhs ) const {
Polynomial retVal;
for( itr_t it = _list.begin(); it != _list.end(); ++it )
{
for( itr_t rit = rhs._list.begin(); rit != rhs._list.end(); ++rit )
{
Term aterm = it.getData() * rit.getData();
if( aterm.coeff != 0 ) // don't have to have this check, since addTerm() already does it, but it can save a function jump
{
retVal.addTerm( aterm.coeff, aterm.exp );
}
}
}
return retVal;
}
示例9: differentiate
/* decrement the coefficient */
const Polynomial Polynomial::differentiate() {
Polynomial retVal;
for( itr_t it = _list.begin(); it != _list.end(); ++it )
{
int exp = it.getData().exp;
double coeff = it.getIter()->_data->coeff * exp;
if( (coeff != 0) && (exp != 0) )
{
exp--;
retVal.addTerm( coeff, exp );
}
}
return retVal;
}
示例10: main
int main(int argc, const char * argv[]) {
// insert code here...
Polynomial* a = new Polynomial();
Polynomial* b = new Polynomial();
a->addTerm(2, 7);
a->addTerm(2, 3);
a->addTerm(5, 2);
a->addTerm(-3, 1);
a->addTerm(10, 0);
b->addTerm(5, 5);
b->addTerm(-2, 3);
b->addTerm(-3, 2);
b->addTerm(7, 1);
b->addTerm(-8, 0);
Polynomial* c= a->addPoly(b);
a->printPoly();
b->printPoly();
c->printPoly();
return 0;
}