As with most of my work I have managed to make it through most of the work assigned to me except I always have one problem that I can't fix. Here is a program that takes in two polynomials and then allows you to add or subtract them. My problem is that if the polynomials are put in so that the exponent on one starts out as being higher that the initial exponent of the other the lists will not advance through the nodes. Can anyone point me to where the problem is, give me some ideas on how to fix it, anything?
My problem is that if the polynomials are put in so that the exponent on one starts out as being higher that the initial exponent of the other the lists will not advance through the nodes.
I am allowed to assume that the polynomials will be put in in correct order (ie. +4x^4+3x^3) with the largest exponent starting out the polynomial going to the smallest.
My problem is that if the polynomials are put in so that the exponent on one starts out as being higher that the initial exponent of the other the lists will not advance through the nodes. Can anyone point me to where the problem is, give me some ideas on how to fix it, anything?
I just can't figure out why it's not advancing in this case.
This doesn't directly solve your problem, but IMHO, you really should write some helper functions.
For example, write a function that returns the coefficient for a given term in a polynomial. If the coefficient does not exist, return 0. This will allow you to greatly simplify your code.
Another suggestion: if X and Y are integers, then X - Y can be written as X + (-Y). Therefore, your operator-= could simply negate the coefficients of the right-hand side then call operator+=. This will reduce your code by 40%. (As it stands now, every bug in operator-= most likely exists in operator+= and vice versa because the functions are so similar).
A third suggestion: neither operator+= nor operator-= should output the polynomial. write operator<< or a print function (if you are not familiar with writing operator<<) instead. This will further simplify operator+=.
A fourth suggestion, as a matter of writing code that is intuitive to use: if X, Y, and Z are integers, then in C++ I can write the line of code X = ( Y += Z ); which adds Z to Y, then assigns the new value of Y to X. Your declaration of operator+= and operator-= do not allow me to do this, because both return void. Rather, they should both return a Polynomial& (and be *this).
A fifth suggestion, which I'm sure you aren't allowed to do, but just in case... use std::vector<> rather than a custom linked list to hold the coefficients; this simplifies traversal of the coefficients and eliminates all the memory management you have to deal with by using a custom linked list implementation.
A sixth suggestion, in case the previous one won't work for you: write a dedicated linked list class and have Polynomial contain one. Now the linked list class deals with the memory management and Polynomial deals with adding coefficients. As it stands now, Polynomial has to do both.