• ## Discover

1. Home >
2. All Categories >
3. Computers & Internet >
4. Programming & Design >
5. Resolved Question
Member since:
September 25, 2009
Total points:
95 (Level 1)

## Resolved Question

Show me another »

# How do you write a progarm that solves equations in c++?

how do you write a progarm that solves equations in c++??
Member since:
August 11, 2009
Total points:
11,854 (Level 6)

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, non-linear: 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.
Thanks man ill make one to solve quadratic equations :)

There are currently no comments for this question.

• Member since:
January 13, 2008
Total points:
19,820 (Level 6)
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.

### Source(s):

Something of an expert in C/C++, Basic and even a bit of Pascal when I need it.
• by tobi
Member since:
September 19, 2009
Total points:
1,713 (Level 3)
Take the equation as an input string... then parse the string into something meaningful for the program.... then you solve!
• 2 people rated this as good