Thread: Help with making a Math Expression DLL

  1. #1
    Registered User
    Join Date
    Nov 2006
    Location
    82.192.160.50
    Posts
    17

    Help with making a Math Expression DLL

    Hey,

    I'm looking at creating a set of math expression functions. Here's a quick example of what i want:
    Code:
     int i = 0;
     mathexpr em = MathCreate(1); //Creates a unique handle, sets the initial value to 1
    
     MathAdd(em,10); //Makes the expression add 10 to the value. Not calculating yet.
     MathSub(em,6); //Makes the expression subtract 5 from the value. Not calculating yet.
    
     i = MathEval(em); //Will evaluate the expression "1+10-6" returning 5.
     MathDestroy(em); //Destroys an expression, releasing it's handle, and freeing the memory used.
    
     //Other functions would include
     MathSet(em,i); //Sets the value of the expression to i, clearing the whole expression.
     MathDiv(em,i); //Divisions
     MathMult(em,i); //Multiplication
     MathOr(em,i); //Bitwise Or Operation
     MathAnd(em,i); //Bitwise And Operation
     MathXor(em,i); //Bitwise Xor Operation
     MathPow(em,i); //Would add pow(MathEval(em),i)
     //This is just the things i could brainstorm, any math function i deem usefull should be added.
     //Also, if possible the different Math* should return an id, then a function called MathRemove(m)
     //could be used to remove a certain step from an expression. Example from above:
    
     int i = 0;
     mathstep m = 0;
     mathexpr em = MathCreate(1); //Creates a unique handle, sets the initial value to 1
    
     m = MathAdd(em,10); //Makes the expression add 10 to the value.
     MathSub(em,6); //Makes the expression subtract 5 from the value.
    
     MathRemove(em,m); //Removes the "+10" part of the expression, leaving "1-6".
     i = MathEval(em); //Will evaluate the expression "1-6" returning -5.
     MathDestroy(em); //Destroys an expression, releasing it's handle, and freeing the memory used.
    I hope that example states well enough what i want to do. To me, it sounds rather simple to do, but problem is, i don't really know where to start. I'm very new to c programming, and i've only made a handfull small things so far. Mainly using MSDN to lookup, and work from examples.

    Please know that i'm not asking for code, i'm asking for knowledge, ideas, usefull reading and really any usefull info.

  2. #2
    int x = *((int *) NULL); Cactus_Hugger's Avatar
    Join Date
    Jul 2003
    Location
    Banks of the River Styx
    Posts
    902
    What would be the advantage of what you posted over just i = 1+10-6 ?

    Otherwise, it sounds like you're looking at creating a library. To start, just write the functions as you would normally, keeping them in a separate source file(s). Later, you could look into wrapping them into a DLL.
    long time; /* know C? */
    Unprecedented performance: Nothing ever ran this slow before.
    Any sufficiently advanced bug is indistinguishable from a feature.
    Real Programmers confuse Halloween and Christmas, because dec 25 == oct 31.
    The best way to accelerate an IBM is at 9.8 m/s/s.
    recursion (re - cur' - zhun) n. 1. (see recursion)

  3. #3
    Algorithm Dissector iMalc's Avatar
    Join Date
    Dec 2005
    Location
    New Zealand
    Posts
    6,318
    Could you please help out by explaning the purpose of this?
    My homepage
    Advice: Take only as directed - If symptoms persist, please see your debugger

    Linus Torvalds: "But it clearly is the only right way. The fact that everybody else does it some other way only means that they are wrong"

  4. #4
    Registered User
    Join Date
    Sep 2001
    Posts
    752
    To be honest, what kind of help you need depends a lot how good a grip you have on the fundamentals of programming.

    Are you familiar with trees? That seems to be the natural way to go with this.
    Callou collei we'll code the way
    Of prime numbers and pings!

  5. #5
    Registered User
    Join Date
    Nov 2006
    Location
    82.192.160.50
    Posts
    17
    Quote Originally Posted by Cactus_Hugger View Post
    What would be the advantage of what you posted over just i = 1+10-6 ?

    Otherwise, it sounds like you're looking at creating a library. To start, just write the functions as you would normally, keeping them in a separate source file(s). Later, you could look into wrapping them into a DLL.
    Well, the purpose of this, is to add functionality to a scritping language i'm using, and also, to add some better execution speed to the language. Ofcourse simple additions wouldn't be faster, the calling of the function would slow it down, but for more advanced things, i think this would be faster. Also, the scripting language doesn't feature bitwise operations.

    Quote Originally Posted by iMalc View Post
    Could you please help out by explaning the purpose of this?
    Read above.
    Quote Originally Posted by QuestionC View Post
    To be honest, what kind of help you need depends a lot how good a grip you have on the fundamentals of programming.

    Are you familiar with trees? That seems to be the natural way to go with this.
    Haven't worked with trees no, but i think i get the concept. From what i can see, it works it's way up, storing the values as it goes, then using it in the next step.

  6. #6
    Woof, woof! zacs7's Avatar
    Join Date
    Mar 2007
    Location
    Australia
    Posts
    3,459
    Try and conform to C89, see the C89/90 draft as it's more portable.

    Before you dive into coding it, design it - look at linked lists and binary trees as QuestionC suggested. From there it should be smooth sailing.

  7. #7
    Registered User
    Join Date
    Nov 2006
    Location
    82.192.160.50
    Posts
    17
    Quote Originally Posted by zacs7 View Post
    Try and conform to C89, see the C89/90 draft as it's more portable.

    Before you dive into coding it, design it - look at linked lists and binary trees as QuestionC suggested. From there it should be smooth sailing.
    I've been looking at linked lists for doing this. And i've been thinking of how to do it, something like this might work:
    Code:
    struct node {
        int operation; //Would be set to a number representing the operation, 0 = add, 1 = sub, 2 = div, etc etc.
        long val;
        struct node *next;
    };
    MathEval would then flip through the list, and do the operation required using val, maybe using a switch statement to check what operation to perform. Then return the result in the end. Would that be a valid way of aproaching this? Going to test it a little later today, to see if i can get it working as i want it to.

    Btw, what's C89?

  8. #8
    Registered User
    Join Date
    Sep 2001
    Posts
    752
    C89 is the traditional C standard which it is safe to assume any given compiler supports.
    Callou collei we'll code the way
    Of prime numbers and pings!

  9. #9
    Registered User
    Join Date
    Nov 2006
    Location
    82.192.160.50
    Posts
    17
    Okay, so C89 would be like the math.h and stdio.h ? Atleast, that's the ones i assume any compiler should have. Is there a list of the C89 standard?

  10. #10
    Registered User
    Join Date
    Sep 2001
    Posts
    752
    The document defining the standard is pretty heavyweight and not free. Honestly, there is not a good reason to worry about it for the purposes of this project.
    Callou collei we'll code the way
    Of prime numbers and pings!

  11. #11
    Registered User
    Join Date
    Nov 2006
    Location
    82.192.160.50
    Posts
    17
    Okay, well, i'll try and do it with a linked list, and the process as i suggested earlier. I think it's going to work, and i'll get to learn about linked lists in the process.

  12. #12
    Registered User
    Join Date
    Sep 2001
    Posts
    752
    Quote Originally Posted by iMalc View Post
    Could you please help out by explaning the purpose of this?
    This could actually be useful for implementing a routine to perform bulk math operations...

    Code:
       // :1 is a placeholder for the variable
       math_transform("5 + 3 x :1 + 7", input, output, num_elements);
    Callou collei we'll code the way
    Of prime numbers and pings!

  13. #13
    Registered User
    Join Date
    Nov 2006
    Location
    82.192.160.50
    Posts
    17
    I seem to have run into a problem already
    Code:
     #include <stdlib.h>
    
     int main()
     {
         int *ptr = malloc( sizeof(int) );
    
         return 0;
     }
    This produces this compiler error:
    Quote Originally Posted by mingw32-gcc
    main.cpp: In function `int main()':
    main.cpp:5: error: invalid conversion from `void*' to `int*'
    But it's an example from a tutorial in here. I know how to type cast it to work, but i'm unsure if that's what i'm supposed to do, since the example doens't do it.
    http://www.cprogramming.com/tutorial/c/lesson6.html

  14. #14
    Officially An Architect brewbuck's Avatar
    Join Date
    Mar 2007
    Location
    Portland, OR
    Posts
    7,396
    Quote Originally Posted by MindWorX View Post
    This produces this compiler error:

    But it's an example from a tutorial in here. I know how to type cast it to work, but i'm unsure if that's what i'm supposed to do, since the example doens't do it.
    http://www.cprogramming.com/tutorial/c/lesson6.html
    Because you named your file .cpp, not .c, which causes the compiler to treat it as C++ instead of C. Why are you naming a C source file .cpp?

  15. #15
    Registered User
    Join Date
    Nov 2006
    Location
    82.192.160.50
    Posts
    17
    Don't know, that's what i've always done. But seems to work now. How will this affect my project if the rest of it is named cpp, and this file is going to be a .h file in the end? Should all my files be c then? Or should i do the linked list in c++ instead?

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Please Help - Problem with Compilers
    By toonlover in forum C++ Programming
    Replies: 5
    Last Post: 07-23-2005, 10:03 AM
  2. Expression Evaluator Contest
    By Stack Overflow in forum Contests Board
    Replies: 20
    Last Post: 03-29-2005, 10:34 AM
  3. .lib vs .h vs .dll
    By Shadow12345 in forum C++ Programming
    Replies: 13
    Last Post: 01-01-2003, 05:29 AM
  4. Passing parameters from VB to C++ through ActiveX DLL
    By torbjorn in forum Windows Programming
    Replies: 0
    Last Post: 12-10-2002, 03:13 AM
  5. Help! Program to evaluate expression...
    By Unregistered in forum C++ Programming
    Replies: 7
    Last Post: 02-19-2002, 06:20 AM