I have my program working so far to read in a string of the form ax^n, and evaluate ax^n for an inputted x value. Now what I am trying to is to extend this to an nth order polynomial:
a[n]x^n + a[n-1]x^(n-1) + ... + a[2]x^2 + a[1]x + d
I have been racking my brain, and cannot think of any way to do it. What I need to do is to repeat the whole process for the next term in the polynomial, until the end of the inputted string is reached (polynomial[i]=='\n'), adding the value of each term as I go.
To me, a for loop seems like an obvious choice, since I can use this to sum the terms and terminate at the end of the string, but I can't see a way to repeat my process for the next term (so basically processing the string chunk by chunk).
The problem is that each term may have a different length, so I can't increment the loop by a fixed amount, but I can return the length of the given term as part of my 'process.' I can't explain this too well, but this might make it clearer:
-calculate term 1 (and return length of term) (I have this working already)
-move to term 2
-calculate term 2 (and return length of term)
-move to term 3
-etc....
-stop when polynomial[i]=='\n' (end of string)
-Add all terms
I there any way to do this? It would be useful If I could shift the contents of the string n places to the left (where n is the length of the given term), such that polynomial[n] is set to polynomial[0] before the next repeat.