Click here to Skip to main content
15,887,027 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
So, the goal of the program is:
1) get an equation from the user with one unknown as a std::string (e.g. "62 = x * 1000")
2) solve the equation and print the value of the unknown (denoted as "x")

What I have tried:

i found TinyExpr, which is pretty amazing, but it cannot solve equations with unknown variables.
i also have installed Boost, but i almost never used it.
Posted
Updated 23-May-23 9:23am
v3
Comments
Rick York 23-May-23 13:14pm    
Your program should solve them exactly as you would do it yourself. An expression handler would be a good front end to use.
Avtem 23-May-23 13:23pm    
"Your program should solve them exactly as you would do it yourself." - but to implement such a functionality probably will require hundreds lines of code - parsing numbers, signs, operator precedence and of course testing and error handling.
TinyExpr is only capable to solve one part of equation and it's already pretty large.
i feel like someone already created such a library, that's why i asked for help, because i haven't found one yet.
Rick York 23-May-23 13:46pm    
There probably is code around that can do this but the point of most homework is to do it yourself. A secondary point of it is to learn how to find what you need for yourself.
0x01AA 23-May-23 14:25pm    
There are a lot of solver examples: Google for "solver c++". Solver in very easy words means for your example to solve the equation (x + 38)%4") - 30 = 0 by varying parameter x (also called root search).
Avtem 23-May-23 14:41pm    
So far i haven't found any code that solves the problem i am trying to solve.
Unfortunately googling for "solver C++" gave me results that don't mention equations with one unknown variable

1 solution

Quote:
... to implement such a functionality probably will require hundreds lines of code - parsing numbers, signs, operator precedence and of course testing and error handling.
Yes, it would.

Unfortunately, that's because it's a complex project - to expand on what Rick has said, you need to do several things:
1) Verify the entered expression is valid.
2) Break the expression into operators and operands
3) Identify the unknown.
4) Reformat the expression so that the unknown is alone on one side of the equals sign evaluating "chunks" as you go:
30=2x+4
2x=30-4
2x=26
x=26/2
x=13
Exactly the same way you would if you were doing it manually.

This may help you get started: How to Write Code to Solve a Problem, A Beginner's Guide[^]

But ... do note that your example 30 = (x + 38)%4 fails the first step as there is no unique solution: you cannot "reverse" modulus operators to get back the original input as it discards info. So you can't get from 30 = (x + 38)%4 to (x + 38) = 30 <any operator> 4 as you can with the paired operators like add/subtract and mutiply/divide.
 
Share this answer
 
Comments
Avtem 23-May-23 15:22pm    
i apologize! i didn't notice that my example equation wasn't even solvable! i changed the example string to a solvable "62 = x * 1000"

Sorry, only now i realize that i should have mentioned that i am only looking for an existing library. i could implement this library, but it's not worth the effort and time.
Rick York 23-May-23 23:58pm    
That's a rather trivial one. Try just changing the % to a /. Then you have a solvable equation that is a bit less trivial : 30=(x+38)/4;

If you analyze the algebraic steps taken to solve the equation you can correlate them to the parsing tree that (most) expression evaluators generate.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900