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:
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:
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 )
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 )