Do you really need to give the 'x' treatment as a character?
It seems like the requirement would be to pass x to a function and return the calculated value.
thines01
Postaholic
2,433 posts since Oct 2009
Reputation Points: 447
Solved Threads: 408
Skill Endorsements: 7
...meaning: if you have a function that computes x:
int Solve2xMinusxPlus4x(int x)
{
return ((2*x) - x + (4*x));
}
...you could loop a call to that until you receive the answer you want.
If you test it by looping from 0 to 10, you would eventually (at 1) receive the answer 5.
The x would drop off of the equation because you are solving FOR x.
thines01
Postaholic
2,433 posts since Oct 2009
Reputation Points: 447
Solved Threads: 408
Skill Endorsements: 7
So, the user is going to type the full equation on the command-line?
If so, will there be an equal sign?
With a formula, you'll either be expecting a result (by providing x) or
you'll be expecting x (by providing the expected result).
thines01
Postaholic
2,433 posts since Oct 2009
Reputation Points: 447
Solved Threads: 408
Skill Endorsements: 7
traverse the array using a loop
when a character x is found convert the digit before the x using atoi() then save it temporarily to a variable then check the operation after the "x" after that check the next elements for the x and do the same
for example:
2x+3x
traverse the loop
when an x is found
convert and save 2 (the element before x)
check the operation (+) (make a condition for all operations)
check the set after the operator
check if the there is another x
convert and save 3
solve the current operation (3+2) and save the answer
continue traversing the array until the end of the array is found
anything else you need to be aware of are digits that doesn't have an x with them or x that is not preceded by a digit so the default should be 1 and finally negative numbers
note: I once did a similar program like this with algebraic equations but I used linked lists instead of arrays so I can have a better control of the elements but I believe the concept should be about similar...also coding this would be longer than the code you posted so good luck :)
zeroliken
Nearly a Posting Virtuoso
1,346 posts since Nov 2011
Reputation Points: 214
Solved Threads: 205
Skill Endorsements: 14
You can basically treat the appearance of x as command 'add previous number to x-count' and any other not-number as 'add previous number to constant value'. Only thing is that if number was not there, you must default to one for x case, 0 for constant.
pyTony
pyMod
6,312 posts since Apr 2010
Reputation Points: 879
Solved Threads: 987
Skill Endorsements: 26