Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations bkrike on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Polynomials

Status
Not open for further replies.

chineerat

Technical User
Oct 12, 2003
42
TT
i am trying to add 2 polynomials

2x^2 + 3x^1 + 10
5x^2 - 2

note the format of the numbers.
my problem is to store the corresponding exponents in a list structure
how do i do this?
thanks in advance
 
Use std::list from STL :

Code:
#include <list>
using namespace std;

int main ()
{
  list<int> exp_of_poly1;
  list<int> exp_of_poly2;

  exp_of_poly1.push_back (2);
  exp_of_poly1.push_back (1);
  exp_of_poly1.push_back (0);

  exp_of_poly2.push_back (2);
  exp_of_poly2.push_back (0);

  //...

  return 0;
}


--
Globos
 
Or try map<unsigned,long> where 1st arg represents degree (^n) and 2nd represents coefficient. You may declare very interesting class like:
Code:
class Polynom: public std::map<unsigned,long>
{
public: // define poly ops (+,-,*,operator-()) etc
...
};
Add
Code:
 ostream& operator << (ostream&,const Polynom&)
and what else and enjoy with full polynomial arith and stream i/o. You may declare Polynom class as wrapper, not as successor of std::map. Anyway it will be a very good practice in OOP...
It seems map container is more suitable than (two) list(s) for polynomes...
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top