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

  • 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, 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.

Asker's rating & comment

4 out of 5
Thanks man ill make one to solve quadratic equations :)
  • Rate
  • Comment

Other Answers (2)

Rated Highest
  • Rated Highest
  • Oldest
  • Newest
  • tobi answered 5 years ago
    Take the equation as an input string... then parse the string into something meaningful for the program.... then you solve!
    • 2
    • Comment
  • Shadow Wolf answered 5 years ago
    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.

    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 Wolf


    Something of an expert in C/C++, Basic and even a bit of Pascal when I need it.
    • Rate
    • Comment
  • Sign In 

    to add your answer

Who is following this question?

    Member Since:
    Points: Points: Level
    Total Answers:
    Points this week: