help with polynomials

Please support our C++ advertiser: Intel Parallel Studio Home
Reply

Join Date: Oct 2007
Posts: 1
Reputation: rkreed94 is an unknown quantity at this point 
Solved Threads: 0
rkreed94 rkreed94 is offline Offline
Newbie Poster

help with polynomials

 
0
  #1
Oct 11th, 2007
I am new to this site and it was recommended by a friend and class mate. I am having serious issues in OOP. Did great in C++ last semester but our new book is killing me. I am trying to get this program going with polynomials. The professor gave us his prototypes and main, which I think has confused me more than if I'd written it myself.
The problem wants us to use dynamic arrays to implement a polynomial class with +, -, and *. I am having trouble grasping hold of the overloaded operators mainly but input on anything else would be helpful. Here is what I have so far. May have some silly errors in it yet though. Any help is greatly appreciated.
  1. #include <iostream>
  2. using namespace std;
  3. #include <cstdlib>
  4.  
  5. class Polynomial
  6. {
  7. private:
  8. double * coef;
  9. int size;
  10. public:
  11. Polynomial(); // creates an empty polynomial
  12. Polynomial(const Polynomial & p); // copy ctor
  13. // size of the coefficient array is degree of the polynomial + 1
  14. Polynomial(double coefnt[], int size);
  15. ~Polynomial();
  16. // use as r-value to inspect coefficient, as l-value to assign coefficient;
  17. double & operator[](int degree); // required for 'const' correctness
  18. const double & operator[](int degree)const;
  19. //const Polynomial & operator=(const Polynomial & p);
  20. int getSize();
  21. //double eval(double arg) const; // evaluate polynomial for arg
  22. Polynomial operator+(const Polynomial & p);
  23. Polynomial operator-(const Polynomial & p);
  24. Polynomial operator*(const Polynomial & p);
  25. };
  26.  
  27. int main()
  28. {
  29. double one[] = {1};
  30. Polynomial One(one, 1);
  31. double quad[] = {3, -2.1, 1.3};
  32. double cubic[] = {1, 2, 0, 3.2};
  33.  
  34. Polynomial q(quad, 3); // q is 3 - 2.1*x + 1.3*x*x
  35. Polynomial c(cubic, 4); // c is 1 + 2*x + 0*x*x + 3.2*x*x*x
  36. Polynomial p(q); // test copy constructor
  37.  
  38. cout << " value of q(2) is " << q.eval(2) << endl;
  39. cout << " value of p(2) is " << p.eval(2) << endl;
  40. Polynomial r;
  41. r = c; //test operator=
  42. cout << " value of r(2) is " << r.eval(2) << endl;
  43. cout << " value of c(2) is " << c.eval(2) << endl;
  44. cout << "\nPolynomial q " << endl;
  45.  
  46. for ( int k = 0; k < q.getSize(); k++ )
  47. cout << " term with degree " << k << " has coefnt " << q[k] << endl;
  48.  
  49. cout << "\nPolynomial c " << endl;
  50.  
  51. for ( int k = 0; k < c.getSize(); k++ )
  52. cout << " term with degree " << k << " has coefnt " << c[k] << endl;
  53. r = q + c; // test operator+
  54. cout << "\nPolynomial r (= q + c) " << endl;
  55.  
  56. for ( int k = 0; k < r.getSize(); k++ )
  57. cout << " term with degree " << k << " has coefnt " << r[k] << endl;
  58. cout << " value of (q + c)(2) is " << r.eval(2) << endl;
  59.  
  60. r = q - c; // test operator-
  61. cout << "\nPolynomial r (= q - c) " << endl;
  62.  
  63. for ( int k = 0; k < r.getSize(); k++ )
  64. cout << " term with degree " << k << " has coefnt " << r[k] << endl;
  65. cout << " value of (q - c)(2) is " << r.eval(2) << endl;
  66. r = q * c; // test operator*
  67.  
  68. cout << " size of q*c is " << r.getSize() << endl;
  69. cout << "\nPolynomial r (= q * c) " << endl;
  70. for ( int k = 0; k < r.getSize(); k++ )
  71. cout << " term with degree " << k << " has coefnt " << r[k] << endl;
  72. cout << " value of (q * c)(2) is " << r.eval(2) << endl;
  73. return 0;
  74. }
  75.  
  76. Polynomial::Polynomial()
  77. {//creates an empty polynomial
  78. size = 0;
  79. coef = new double[size+1];
  80. coef[0] = 0;
  81. }
  82.  
  83. //copy constructor
  84. Polynomial::Polynomial(const Polynomial & p)
  85. {
  86. size = p.size;
  87. coef = new double[size+1];
  88. for (int i = 0; i <= size; i++)
  89. coef[i] = p.coef[i];
  90. }
  91. Polynomial::Polynomial(double coefnt[], int size)
  92. {
  93. size = p.size;
  94. coef = new double[size+1];
  95. for (int i = 0; i <= size; i++)
  96. coef[i] = p.coef[i];
  97. }
  98.  
  99. //destructor
  100. Polynomial::~Polynomial()
  101. {
  102. delete [] coef;
  103. }
  104.  
  105. //overloaded []
  106. double Polynomial::& operator[](int degree) // required for 'const' correctness
  107. {
  108. if (degree < 0)
  109. {
  110. cout << "Invalid input";
  111. exit(1);
  112. }
  113. else if (degree > size)
  114. {
  115. cout << "Out of bounds";
  116. exit(1);
  117. }
  118. else
  119. return size;
  120. }
  121. /*
  122. const double Polynomial::& operator[](int degree)const
  123. {
  124.  
  125. }
  126. */
  127. const Polynomial & operator=(const Polynomial & p)
  128. {
  129. if ((size != p.size)
  130. {
  131. delete [] coef;
  132. coef= new double[p.size];
  133. for (int i = 0; i < size; i++)
  134. coef[p.size] = new double[size];
  135. }
  136.  
  137. sizes = p.size;
  138. assert(coef != NULL);
  139.  
  140. for (int i=0; i < size; i++)
  141. coef[i] = p.coef[i];
  142.  
  143. return *this;
  144. }
  145. int Polynomial:: getSize()
  146. {
  147. return size;
  148.  
  149. }
  150. /*double Polynomial::eval(double arg) const // evaluate polynomial for arg
  151. {
  152.  
  153. }
  154. */
  155.  
  156. Polynomial::Polynomial operator+(const Polynomial & p)
  157. {
  158.  
  159. }
  160. Polynomial::Polynomial operator-(const Polynomial & p)
  161. {
  162.  
  163.  
  164. }
  165. Polynomial::Polynomial operator*(const Polynomial & p)
  166. {
  167.  
  168. }
Last edited by Ancient Dragon; Oct 11th, 2007 at 11:03 pm. Reason: add code tags
Reply With Quote Quick reply to this message  
Join Date: May 2006
Posts: 1,827
Reputation: ithelp is a name known to all ithelp is a name known to all ithelp is a name known to all ithelp is a name known to all ithelp is a name known to all ithelp is a name known to all 
Solved Threads: 118
ithelp's Avatar
ithelp ithelp is offline Offline
Posting Virtuoso

Re: help with polynomials

 
0
  #2
Oct 12th, 2007
a+b means
a.+(b) (class a has a function + which is taking b as an argument)
Stroustrup has better explanation.
Reply With Quote Quick reply to this message  
Reply

This thread is more than three months old.
Perhaps start a new thread instead?
Message:



Similar Threads
Other Threads in the C++ Forum
Thread Tools Search this Thread



About Us | Contact Us | Advertise | DaniWeb | Acceptable Use Policy | RSS Feed

©2003 - 2009 DaniWeb® LLC