| | |
Polynomial help!
Please support our C++ advertiser: Intel Parallel Studio Home
![]() |
•
•
Join Date: Oct 2009
Posts: 4
Reputation:
Solved Threads: 0
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)
#include <cassert> #include <iostream> #include "poly.h" using namespace std; #define NULL 0 const unsigned int poly::SIZEINCR = 5; poly::poly() { variable = 'x'; nterms = 0; terms = new term[MAXSIZE = SIZEINCR]; }; void poly::copy (const poly & p) { variable = p.variable; nterms = p.nterms; terms = new term[MAXSIZE = p.MAXSIZE]; for (unsigned int i=0; i<nterms; i++) terms[i] = p.terms[i]; }; poly::poly (const poly & p) { copy (p); }; void poly::free (void) { nterms = 0; delete [] terms; terms = NULL; }; poly::~poly (void) { free (); }; poly & poly::operator= (const poly & p) { if (this != &p) { free (); copy (p); }; return (*this); }; void poly::expand () { termptr p; p = new term[MAXSIZE += SIZEINCR]; for (unsigned int j=0; j<nterms; j++) p[j] = terms[j]; delete [] terms; terms = p; }; void poly::InsertTerm (term t) { int i = nterms-1; unsigned int e = t.exp; if (nterms == MAXSIZE) expand (); nterms++; while ((i>=0) && (e > terms[i].exp)) { terms[i+1] = terms[i]; i--; }; assert ((i<0) || (e != terms[i].exp)); terms[i+1] = t; }; void poly::read () { poly temp; int coeff; int exp; cout << "Input a polynomial by specifying the variable and all terms in any order." << endl << "Each term is specified by an integer coefficient and" << endl << "a non-negative integer exponent." << endl << "Indicate END by specifying a dummy term with" << endl << "a zero coefficient and/or a negative exponent." << endl; cin >> temp.variable; do { cin >> coeff; if (coeff) { cin >> exp; if (exp >= 0) temp.InsertTerm (term(coeff, exp)); } else while (cin && (cin.peek() != '\n')) cin. ignore(); } while (coeff && (exp >= 0)); *this = temp; }; void poly::write() const { // I need help!!! }; poly poly::plus (const poly & right) const { }; poly poly::minus (const poly & right) const { }; float poly::evaluate (float value) const { };
•
•
Join Date: Jul 2005
Posts: 1,705
Reputation:
Solved Threads: 274
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.
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.
Klatu Barada Nikto
•
•
Join Date: Oct 2009
Posts: 4
Reputation:
Solved Threads: 0
this is the poly.h file
C++ Syntax (Toggle Plain Text)
class poly { public: poly(); poly (const poly &); ~poly (); poly & operator= (const poly &); void read (); void write () const; poly plus (const poly &) const; poly minus (const poly &) const; float evaluate (float) const; private: struct term { int coeff; unsigned int exp; term () {}; declaration. term (int c, unsigned int e) : coeff(c), exp(e) {}; char sign () const {return ((coeff<0)?'-':((coeff==0)?'0':'+'));}; }; typedef term * termptr; static const unsigned int SIZEINCR; unsigned int MAXSIZE; unsigned int nterms; termptr terms; char variable; void copy (const poly &); void free (); void InsertTerm (term); void expand (); };
•
•
Join Date: Jan 2008
Posts: 3,831
Reputation:
Solved Threads: 501
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.
•
•
Join Date: Jan 2008
Posts: 3,831
Reputation:
Solved Threads: 501
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.
![]() |
Similar Threads
- Code Snippet: Simple Polynomial class (C++)
- Polynomial operators-addition,substraction,division,remainder etc (C++)
- polynomial ADT (C)
- Find 3rd degree irreducible polynomial of [x4 + x3 + x2 + x +1] with '+' & 'X' table (Community Introductions)
- Represent array into polynomial (Computer Science)
- Polynomial Output (C++)
- class polynomial (C++)
Other Threads in the C++ Forum
- Previous Thread: Pattern design..
- Next Thread: Finding three highest scores
| Thread Tools | Search this Thread |
api application array arrays based beginner binary bmp c++ c/c++ calculator char char* class classes code coding compile compiler console conversion convert count data database delete deploy developer dll download dynamiccharacterarray email encryption error file format forms fstream function functions game generator givemetehcodez graph gui homeworkhelp iamthwee ifstream image input int java lib library list loop looping loops map math matrix memory multiple newbie news number numbertoword output pointer problem program programming project python random read recursion recursive reference rpg simple sorting string strings temperature template text text-file tree url variable vector video visual visualstudio win32 windows winsock wordfrequency wxwidgets






