C Board  

Go Back   C Board > General Programming Boards > C Programming

Reply
 
LinkBack Thread Tools Display Modes
Old 07-17-2007, 05:33 PM   #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.
MindWorX is offline   Reply With Quote
Old 07-17-2007, 09:33 PM   #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)
Cactus_Hugger is offline   Reply With Quote
Old 07-17-2007, 11:44 PM   #3
Algorithm Dissector
 
iMalc's Avatar
 
Join Date: Dec 2005
Location: New Zealand
Posts: 2,737
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
iMalc is offline   Reply With Quote
Old 07-18-2007, 12:34 AM   #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!
QuestionC is offline   Reply With Quote
Old 07-18-2007, 03:12 AM   #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.
MindWorX is offline   Reply With Quote
Old 07-18-2007, 04:45 AM   #6
Woof, woof!
 
zacs7's Avatar
 
Join Date: Mar 2007
Location: Australia
Posts: 3,295
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.T. gets the chicky-babes" - M. Kelly
bakefile | vim
zacs7 is offline   Reply With Quote
Old 07-18-2007, 05:11 AM   #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?
MindWorX is offline   Reply With Quote
Old 07-18-2007, 07:50 AM   #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!
QuestionC is offline   Reply With Quote
Old 07-18-2007, 09:26 AM   #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?
MindWorX is offline   Reply With Quote
Old 07-18-2007, 10:36 AM   #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!
QuestionC is offline   Reply With Quote
Old 07-18-2007, 10:52 AM   #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.
MindWorX is offline   Reply With Quote
Old 07-18-2007, 11:48 AM   #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!
QuestionC is offline   Reply With Quote
Old 07-18-2007, 12:08 PM   #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
MindWorX is offline   Reply With Quote
Old 07-18-2007, 12:10 PM   #14
Senior software engineer
 
brewbuck's Avatar
 
Join Date: Mar 2007
Location: Portland, OR
Posts: 5,768
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?
brewbuck is offline   Reply With Quote
Old 07-18-2007, 12:13 PM   #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?
MindWorX is offline   Reply With Quote
Reply

Thread Tools
Display Modes

Forum Jump

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


All times are GMT -6. The time now is 07:40 PM.


Powered by vBulletin® Version 3.8.1
Copyright ©2000 - 2010, Jelsoft Enterprises Ltd.
Search Engine Optimization by vBSEO 3.3.2

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22