+ 3
Can someone help me with an adapted algorithm?
I work on a program that make the addition of two polynomials with linked list. Our tutor says he wants the user to enter the whole polynomial at once instead of entering the coefficients and degrees successively! I am working with linked lists and I have a function which adds monomials to form a polynomial through a linked list (ie: function addmonome(Poly p, coef, deg)). If you want some code , tells me!
16 Answers
+ 1
Romaric Would you mind posting your progress here? I mean, the code upto now.
+ 1
Okay !
+ 1
How can I post some code with pc?
+ 1
Romaric Simple; make a new code in SoloLearn (copy and paste, if you have already made the code in some place other than SoloLearn) and clikc on the share icon (top right), copy the link and paste it here
+ 1
Okay
+ 1
I tried something in function saisie() but it don't work. When i make a function in which the user puts the coefficient and the degree successively, the program work correctly but the tutor tells that the user must enter whole of the polynomial at once
+ 1
Romaric Hmm, so how about implementing a parser? It takes an input polynomial and separates it into its constituent terms, par example, celui-ci:-
"3x+45-2x^2" -> {"3x", "45", "-2x^2"}
+ 1
This will not help me too much. In the example you gave, I still need to recover the coefficients and degrees without the "X"
+ 1
Romaric Here's what you wanna do:
1. Take the input polynomial
2. Split it into terms (whenever a plus or a minus is encountered)
3. The starting of each term is the coefficient. If the starting is an alphabet instead, then the coefficient is 1. If the 'caret' sign isn't specified in a given term, then the power is 1. Else, read from between the caret and the end, like this:
"2x^2" -> {"coeff": 2, "pow": 2, "var_name": "x"}
Now, just sort the terms by their power; add the terms with the same power.
+ 1
I see! With which function in C can i split the input?
+ 1
Romaric You may use the strtok() function from the string.h header file to split a string (Google about it for more info). Here's how you can do it without including the string.h header:
https://code.sololearn.com/cTBD1eb6B6q9/?ref=app
Just make sure to not give an unsimplified polynomial as the first argument to to_poly()
+ 1
Thanks for your contribution 🙏😊 You help me so much
+ 1
Your code work well!
+ 1
Romaric Bien, but make sure to post the completed code here for us to see!
+ 1
Okay