944,057 Members | Top Members by Rank

Ad:
  • C++ Discussion Thread
  • Unsolved
  • Views: 576
  • C++ RSS
Oct 4th, 2009
-1

Polynomial help!

Expand Post »
I am working on a project to solve a class in polynomial. I have successfully managed to do the header file and 75% of the code..but two functions to write and add the polynomials don't seem to work. Can someone help? The last part of the code!
C++ Syntax (Toggle Plain Text)
  1. #include <cassert>
  2. #include <iostream>
  3. #include "poly.h"
  4.  
  5. using namespace std;
  6.  
  7. #define NULL 0
  8.  
  9. const unsigned int poly::SIZEINCR = 5;
  10.  
  11. poly::poly()
  12. {
  13.  
  14. variable = 'x';
  15. nterms = 0;
  16. terms = new term[MAXSIZE = SIZEINCR];
  17. };
  18.  
  19. void poly::copy (const poly & p)
  20. {
  21.  
  22.  
  23. variable = p.variable;
  24. nterms = p.nterms;
  25. terms = new term[MAXSIZE = p.MAXSIZE];
  26. for (unsigned int i=0; i<nterms; i++)
  27. terms[i] = p.terms[i];
  28. };
  29.  
  30. poly::poly (const poly & p)
  31. {
  32.  
  33. copy (p);
  34. };
  35.  
  36. void poly::free (void)
  37. {
  38.  
  39. nterms = 0;
  40. delete [] terms;
  41. terms = NULL;
  42. };
  43.  
  44. poly::~poly (void)
  45. {
  46.  
  47. free ();
  48. };
  49.  
  50. poly & poly::operator= (const poly & p)
  51. {
  52.  
  53. if (this != &p)
  54. {
  55. free ();
  56. copy (p);
  57. };
  58. return (*this);
  59. };
  60.  
  61. void poly::expand ()
  62. {
  63.  
  64. termptr p;
  65.  
  66. p = new term[MAXSIZE += SIZEINCR];
  67. for (unsigned int j=0; j<nterms; j++) p[j] = terms[j];
  68. delete [] terms;
  69. terms = p;
  70. };
  71.  
  72. void poly::InsertTerm (term t)
  73. {
  74.  
  75.  
  76. int i = nterms-1;
  77. unsigned int e = t.exp;
  78.  
  79. if (nterms == MAXSIZE) expand ();
  80.  
  81. nterms++;
  82. while ((i>=0) && (e > terms[i].exp))
  83. {
  84. terms[i+1] = terms[i];
  85. i--;
  86. };
  87.  
  88. assert ((i<0) || (e != terms[i].exp));
  89. terms[i+1] = t;
  90. };
  91.  
  92. void poly::read ()
  93. {
  94.  
  95.  
  96. poly temp;
  97. int coeff;
  98. int exp;
  99. cout << "Input a polynomial by specifying the variable and all terms in any order." << endl
  100. << "Each term is specified by an integer coefficient and" << endl
  101. << "a non-negative integer exponent." << endl
  102. << "Indicate END by specifying a dummy term with" << endl
  103. << "a zero coefficient and/or a negative exponent." << endl;
  104. cin >> temp.variable;
  105. do
  106. {
  107. cin >> coeff;
  108. if (coeff)
  109. {
  110. cin >> exp;
  111. if (exp >= 0)
  112. temp.InsertTerm (term(coeff, exp));
  113. }
  114. else
  115. while (cin && (cin.peek() != '\n')) cin. ignore();
  116. } while (coeff && (exp >= 0));
  117. *this = temp;
  118. };
  119.  
  120. void poly::write() const
  121. {
  122. // I need help!!!
  123. };
  124.  
  125. poly poly::plus (const poly & right) const
  126. {
  127.  
  128. };
  129.  
  130. poly poly::minus (const poly & right) const
  131. {
  132.  
  133. };
  134.  
  135. float poly::evaluate (float value) const
  136. {
  137.  
  138. };
Similar Threads
Reputation Points: 10
Solved Threads: 0
Newbie Poster
trac15 is offline Offline
4 posts
since Oct 2009
Oct 4th, 2009
1

Re: Polynomial help!

I would insert each term in terms in ascending order based on the exponent so that the exp term with exponent 0 has index 0 in terms, etc. I would expand terms if the exponent of a term to be added to terms is above value of nterms. The default coefficient of each term in terms would be zero. Each term inserted into terms would appropriately adjust the value of the coeffient of the term with the same exponent.

To write in standard polynomial form with largest exponent first I would then start at the end of terms and loop backward. Each term could be output only in the value of the coefficient wasn't zero. The display format would be coefficient followed by variable followed by ^ sign followed by exponent without spaces between each of the values in the display.

To add two terms I would determine the polynomial with the smallest value of nterms and expand it to the same number of nterms as the larger one. Looping through each term in the polynomials and adding ( or subtracting as the case may be) the coefficients of terms with the same exponent will result in the coefficient of the term a third polynomial representing the sum.
Reputation Points: 718
Solved Threads: 373
Nearly a Posting Maven
Lerner is offline Offline
2,253 posts
since Jul 2005
Oct 4th, 2009
0

Re: Polynomial help!

Thank you!
But the write function doesn't seem to work or i am going in the wrong direction. can u help me
Reputation Points: 10
Solved Threads: 0
Newbie Poster
trac15 is offline Offline
4 posts
since Oct 2009
Oct 4th, 2009
2

Re: Polynomial help!

Click to Expand / Collapse  Quote originally posted by trac15 ...
Thank you!
But the write function doesn't seem to work or i am going in the wrong direction. can u help me
Post poly.h and poly::write (). We have no idea what you might be doing wrong unless you post the code.
Featured Poster
Reputation Points: 2614
Solved Threads: 687
Posting Expert
VernonDozier is offline Offline
5,375 posts
since Jan 2008
Oct 4th, 2009
1

Re: Polynomial help!

this is the poly.h file
C++ Syntax (Toggle Plain Text)
  1. class poly
  2. {
  3. public:
  4. poly();
  5. poly (const poly &);
  6. ~poly ();
  7. poly & operator= (const poly &);
  8. void read ();
  9. void write () const;
  10. poly plus (const poly &) const;
  11. poly minus (const poly &) const;
  12. float evaluate (float) const;
  13.  
  14. private:
  15. struct term
  16. {
  17. int coeff;
  18. unsigned int exp;
  19. term () {}; declaration.
  20. term (int c, unsigned int e) : coeff(c), exp(e) {};
  21. char sign () const {return ((coeff<0)?'-':((coeff==0)?'0':'+'));};
  22. };
  23. typedef term * termptr;
  24. static const unsigned int SIZEINCR;
  25. unsigned int MAXSIZE;
  26. unsigned int nterms;
  27. termptr terms;
  28. char variable;
  29.  
  30. void copy (const poly &);
  31. void free ();
  32. void InsertTerm (term);
  33. void expand ();
  34. };
Reputation Points: 10
Solved Threads: 0
Newbie Poster
trac15 is offline Offline
4 posts
since Oct 2009
Oct 4th, 2009
0

Re: Polynomial help!

Click to Expand / Collapse  Quote originally posted by trac15 ...
Thank you!
But the write function doesn't seem to work or i am going in the wrong direction. can u help me
The poly::write () function you posted in post 1 is empty. You're saying "it doesn't seem to work", so presumably it's no longer empty. Please post the updated poly::write () function.
Featured Poster
Reputation Points: 2614
Solved Threads: 687
Posting Expert
VernonDozier is offline Offline
5,375 posts
since Jan 2008
Oct 4th, 2009
0

Re: Polynomial help!

That is where i am working on! am not able to go further
Reputation Points: 10
Solved Threads: 0
Newbie Poster
trac15 is offline Offline
4 posts
since Oct 2009
Oct 4th, 2009
1

Re: Polynomial help!

Click to Expand / Collapse  Quote originally posted by trac15 ...
That is where i am working on! am not able to go further
Read Lerner's earlier post again. You want to loop through your terms[] array. For each element, print the sign, then print the coefficient, then print the variable symbol ('x'), then the '^' symbol, then the exponent.
Featured Poster
Reputation Points: 2614
Solved Threads: 687
Posting Expert
VernonDozier is offline Offline
5,375 posts
since Jan 2008
Oct 5th, 2009
0

Re: Polynomial help!

Maybe the following may be of some help:

http://www.daniweb.com/code/snippet217091.html
Featured Poster
Reputation Points: 1536
Solved Threads: 431
Posting Expert
iamthwee is offline Offline
5,865 posts
since Aug 2005

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: Pattern design..
Next Thread in C++ Forum Timeline: Finding three highest scores





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


Follow us on Twitter


© 2011 DaniWeb® LLC