Calc

So the user enters something like x = sin(4y^2) I want the program to solve the equation.
So, I'd say your first step would be finding a branch of mathematics you want to focus on first, start writing the code for said the equations used in said branch of mathematics, and eventually start focusing on the algorithm(s) used to determine what equation to use at any given time.
You will have to input as a string then parse it for order of operations. Then you can simply calculate what it says. Show some code then we may be willing to show you some code.
Lets begin with algebra, X * X ^ 2 = X ^ 3

so the program stores the 3rt(X^3) as X

You can use what I call the tokens algorithm to do this but X is already a variable, not data stored in a variable so the parsing wont work. The source doesn't say mutch but...

1
2
3
4
5
6
7
8
9
options:
cin>> Voptions;
//math
if (Voptions == "math") {
cin>> mopt;

}
//math
//end 


The algebra is solved within the math command.
Here are really two quite different tasks:
1) Parsing math expression and calculating its value for arbitrary values of arguments
2) Being able to calculate value of some function, search for its zeroes (i.e. solutions of equations)

First task requires splitting string to tokens and then either converting them to reverse polish notation to calculate or to calculate it recursively going into all subexpressions etc. It is complex for beginner, but not really hard.

Second task is easy for some simple functions, but as a matter of fact it has no precise solution. Obviously for some complex equations it would be too hard to understand whether solution exists at all. In simpler cases many simple approaches could be used, even binary search:

http://codeabbey.com/index/task_view/binary-search
Number 1 I can do (tokens algorithm) it splits the equation up character by character and stores them as const char* or char*. Using this I can solve things like 2 + 2 = (output variable) but this will not solve 2 + 2 + 2 or X + 2 or sqrt(4). So my tokens solution will not work. Do you have a working one?
As I've told, you need use some more cunning parsing algorithm after tokenization:

http://en.wikipedia.org/wiki/Operator-precedence_parser

http://en.wikipedia.org/wiki/Shunting-yard_algorithm
The operator-precedence parser is exactly what I'm looking for.
Damn you know your stuff! The code would be huge to write wouldn't it?
Is there a smaller parsing technique that I can use?
Topic archived. No new replies allowed.