How do you write a progarm that solves equations in c++?
Follow
 ✓Follow publicly
 ✓Follow privately
 Unfollow
how do you write a progarm that solves equations in c++??
Best AnswerAsker's Choice
It's easy to make programs that solve a PARTICULAR KIND of equations. For example, here's a program that solves a quadratic equation Ax²+Bx+C=0:
input A, B, C
output (B±sqrt(B²4AC)/2A
However in order to solve ARBITRARY equations, you'd need to write a parser (to turn your input equation into an annotated parse tree), and a huge set of algorithms to manipulate the resulting parse tree.
It's NOT an easy task, as there are hundreds of different types of equations (polynomial: Ax³+Bx=0, differential: dx/dt+x=0, integral: ∫f(x)dx + x = 0, nonlinear: x³sin(x)=π, series: AΣx = 4, complex: e^(iωt)=1, vector, matrix, parametric... the list goes on and on), and each requires a totally different approach to solving.
Even top software packages like Maple can't solve ALL types of equations.
input A, B, C
output (B±sqrt(B²4AC)/2A
However in order to solve ARBITRARY equations, you'd need to write a parser (to turn your input equation into an annotated parse tree), and a huge set of algorithms to manipulate the resulting parse tree.
It's NOT an easy task, as there are hundreds of different types of equations (polynomial: Ax³+Bx=0, differential: dx/dt+x=0, integral: ∫f(x)dx + x = 0, nonlinear: x³sin(x)=π, series: AΣx = 4, complex: e^(iωt)=1, vector, matrix, parametric... the list goes on and on), and each requires a totally different approach to solving.
Even top software packages like Maple can't solve ALL types of equations.
Other Answers (2)
Rated Highest
Take the equation as an input string... then parse the string into something meaningful for the program.... then you solve!

This is getting into basic compilers and interpreters. The first part you'll generally want to do is make it solve simple things such as add, subtract, multiply and divide. However, there is a lot more to it than that.
Possibly the easiest to understand is Crenshaw's compiler tutorial. It uses Pascal as the programming language and I think Motorola 68000 machine code instructions in the example, but go ahead and convert it to C/C++ and work with it. Rather than have it generate machine or assembly code, you can also change it to interpret a given formula input as a text string. I know it will work since I had it generating C source code from Qbasic source using Qbasic as the programming language and some special C libraries.
http://compilers.iecc.com/crenshaw/
I won't attempt to try to explain it all, but the tutorial does a very nice job and shows you how to do some things in very small pieces.
Shadow WolfSource(s):
Something of an expert in C/C++, Basic and even a bit of Pascal when I need it.
Sign In
to add your answer