944,112 Members | Top Members by Rank

Ad:
  • C++ Discussion Thread
  • Unsolved
  • Views: 1723
  • C++ RSS
Oct 11th, 2007
0

help with polynomials

Expand Post »
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.
C++ Syntax (Toggle Plain Text)
  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
Similar Threads
Reputation Points: 10
Solved Threads: 0
Newbie Poster
rkreed94 is offline Offline
1 posts
since Oct 2007
Oct 12th, 2007
0

Re: help with polynomials

a+b means
a.+(b) (class a has a function + which is taking b as an argument)
Stroustrup has better explanation.
Reputation Points: 769
Solved Threads: 128
Banned
ithelp is offline Offline
1,910 posts
since May 2006

This thread is more than three months old

No one has posted to this discussion for at least three months. Please let old threads die and do not reply to them unless you feel you have something new and valuable to contribute that absolutely must be added to make the discussion complete. Otherwise, please start a new thread in this forum instead.
Message:
Previous Thread in C++ Forum Timeline: Participating in projects
Next Thread in C++ Forum Timeline: cin.getline not working in for loop or...?





About Us | Contact Us | Advertise | Acceptable Use Policy
Forum Index | Build Custom RSS Feed


Follow us on Twitter


© 2011 DaniWeb® LLC