Expression Simplification (Adding like terms) [C++]
ProgrammingThis forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.
Notices
Welcome to LinuxQuestions.org, a friendly and active Linux Community.
You are currently viewing LQ as a guest. By joining our community you will have the ability to post topics, receive our newsletter, use the advanced search, subscribe to threads and access many other special features. Registration is quick, simple and absolutely free. Join our community today!
Note that registered members see fewer ads, and ContentLink is completely disabled once you log in.
If you have any problems with the registration process or your account login, please contact us. If you need to reset your password, click here.
Having a problem logging in? Please visit this page to clear all LQ-related cookies.
Get a virtual cloud desktop with the Linux distro that you want in less than five minutes with Shells! With over 10 pre-installed distros to choose from, the worry-free installation life is here! Whether you are a digital nomad or just looking for flexibility, Shells can put your Linux machine on the device that you want to use.
Exclusive for LQ members, get up to 45% off per month. Click here for more info.
Expression Simplification (Adding like terms) [C++]
I am having a little trouble understanding how to loop through 6x^2 + 10x^2 + 9x^2 + 6x^1 + 15x^1 + 9 to combine like terms.
Each polynomial is stored as a LinkedList holding a coefficient and exponent. Thus, I can just compare the exponents to see if they match, and if they do, add them together and insert into new LinkedList. If they don't match, then add them into the LinkedList.
I actually used the List ADT to create a list of polynomials. So I've got a list of polynomials, each containing a struct that stores a coefficient and exponent.
I am having a hard time combining like terms... I know what I need to do, but I just can't seem to get it in code form.
Hint: Write a function that receives a single argument the exponent value. This function will step through the linked list and return the node with the matching exponent value or null if there is no match.
I've gotta use iterators, I've written a simplify that works using pointers. This is due to having access to next, which I don't with iterators.
Some pseudo code,
Code:
for(i = list.begin(); i != list.end(); i++) {
if(i->exp == (this is where I would have i->next->exp)) // to check to see if the next exponent is the same value
}
if it was the same, then it just added the coefficients and input them into a new list
My problem is combining like terms using iterators and the list ADT.
Well unless you know that the equation has been entered in order you will need a second iterator to step through each node in the original list. Although I suspect that given you will have two lists you can work with two iterators one on each list.
So on that example you iterate through the list getting the node and then add it to your result list. You will insert the node to the list if the previous node (of the result list) has a different exponent otherwise you will increment the coefficient of the previous node if the exponent is the same. The trick is with the first time through when the previous node doesn't exist and so you will need to add a comparison to null to your test.
However this would not work with a list stored as follows: 10x^2 + 6x^3 + 9x^2 + 6x^1 + 15x^1 + 9 .
I have one list, which stores 6x^3 + 10x^2 + 9x^2 + 6x^1 + 15x^1 + 9 .
EDIT: I actually found a fix, but it's very ghetto yet works. I am still interested if any of you know how to combine like terms using iterators.
Please show your solution. Since this task seems like a school-work problem, showing you a solution would be a breach of the (homework) code of conduct for this forum.
We can assume that the list will always be sorted in descending order.
That assumption eliminates the issue graemef and dwhitney67 have been discussing, as well as eliminating anything that would make the project non trivial.
The fact that you are reading one list and writing a new one (vs. modifying the original list in place) makes the task even simpler.
It's hard to guess why using iterators for this task is in any way inconvenient.
If you have code that is more awkward than you would like, post the code and you probably will get some suggestions about what you might do better.
LinuxQuestions.org is looking for people interested in writing
Editorials, Articles, Reviews, and more. If you'd like to contribute
content, let us know.