I'm making a 3d graphing utility y'all!

So I'm certain this has been done before and I'm certain it has been done well, because I've seen things that do it well, but I'm trying to figure out how to store mathematical equations in C. The equations that I'm trying to deal with are akin to those you would find in a system; up to three variables, in any order/form, and sometimes incomplete. Some examples:

Code:
```     Y = 4Z + 2X - 5     0 = 0.003Y - 53X + 9Y     3X + 7Z = 9 + (-2Y)     -6Z = 0 ```

In short, I'm looking for suggestions on how to structure the composite datatype that will store these types of equations, that will both make them easy to work with and still flexible.

I'm not asking anybody to do this for me; I'm just trying to hear from people who might have done this or something like it before on this subject before I make a mistake that will cost me a lot of time and effort. Most of the ideas I have come up with don't work within the datatype limits of c, or are not unified enough to work with well, so new thoughts are what I need.

I know I haven't given lots of information, so please tell me if you need more context

Thanks!

EDIT:

So my plan, since nobody has said anything otherwise, is this (I know this will only work for linear equations; I will expand later):

I'm planning to make a term struct which will contain a float for a coefficient, and a one-byte int for the variable index (x, y, z, or constant). From this there will be a term array struct, then an equation struct that will have two term arrays; one will contain the terms from before the equals sign, and one will contain those after. Then finally an equation array struct, which is self explanatory. I'm pretty sure my code will explain this better than me:

Code:
```struct Term {    uint8_t variableIndex;    float coefficient; }; struct TermArray {    size_t elements;    struct Term *data; }; struct Equation {    struct TermArray left;    struct TermArray right; }; struct EquationArray {    size_t elements;    struct Equation *data; }```

Please... if you think this is a terrible way to do this, take pity on me and let me know (and maybe also tell me how to improve it )
If you want to implement a generic mathematical expression, I would recommend that you use an abstract syntax tree, or ast for short. The idea is that you have a tree that represents a mathematical expression where each node is an operator, number or some symbol. For example

Code:
```   +  /  \ 1    *     /  \    2    3 ```

represents 1 + (2 * 3) and yields 7 when the tree is evaluated. In this way, you can model the order of operations nicely. You then write methods that manipulate this ast to compute some result.

I wrote a computer algebra system for the TI 84+ CE that parses the expressions from yvars into an AST that you can use in your project if you don't want to do all that work yourself. The project is here if you want to use it as a library or just get an example of an AST implementation: https://github.com/nathanfarlow/PineappleCAS

Register to Join the Conversation
Have your own thoughts to add to this or any other topic? Want to ask a question, offer a suggestion, share your own programs and projects, upload a file to the file archives, get help with calculator and computer programming, or simply chat with like-minded coders and tech and calculator enthusiasts via the site-wide AJAX SAX widget? Registration for a free Cemetech account only takes a minute.

»
» All times are GMT - 5 Hours

You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot vote in polls in this forum