469,612 Members | 1,673 Online
Bytes | Developer Community
New Post

Home Posts Topics Members FAQ

Post your question to a community of 469,612 developers. It's quick & easy.

Operator question

Hi again,

I once saw that it was possible to define operators in C++ or something so I
was thinking, is it possible to store and use operators in C? For example,
first I read out a formula char by char. At some point I detect a operator
char ( + / - * ^ mod etc. ). After parsing the formula I want to use it as
quick as possible. So instead of reading that string again and again I'd
like to store all the functions/values and operators in structs or something
like that. I build this before in Visual Basic and when it came to the
execution of an operator I used something like this :

switch ( operator ) // operator is a field of a
struct( called type in VB )
case 0: output = input + x; // 0 stands for +
case 1: output = input - x; // 1 stands for minus
case 2: output = input * x;
case 3: output = input / x;
< etc. >
It worked well but maybe in C it could be faster by just doing :
output = input operator x;

The same thing for functions, while I'm now using
switch( function )
case 0: x = cos( x );
case 1: x = sin( x );
< and so on >
I'd like to store the function itself so I can directly use it like :
x = function( x );

Is this all possible?

Greetings,
Rick
Nov 13 '05 #1
6 1644
Rick <as******@hotmail.com> scribbled the following:
Hi again, I once saw that it was possible to define operators in C++ or something so I
was thinking, is it possible to store and use operators in C? For example,
first I read out a formula char by char. At some point I detect a operator
char ( + / - * ^ mod etc. ). After parsing the formula I want to use it as
quick as possible. So instead of reading that string again and again I'd
like to store all the functions/values and operators in structs or something
like that. I build this before in Visual Basic and when it came to the
execution of an operator I used something like this : switch ( operator ) // operator is a field of a
struct( called type in VB )
case 0: output = input + x; // 0 stands for +
case 1: output = input - x; // 1 stands for minus
case 2: output = input * x;
case 3: output = input / x;
< etc. >
It worked well but maybe in C it could be faster by just doing :
output = input operator x; The same thing for functions, while I'm now using
switch( function )
case 0: x = cos( x );
case 1: x = sin( x );
< and so on >
I'd like to store the function itself so I can directly use it like :
x = function( x ); Is this all possible?


No. In C (C90 at least) operators are an entirely compile-time concept.

--
/-- Joona Palaste (pa*****@cc.helsinki.fi) ------------- Finland --------\
\-- http://www.helsinki.fi/~palaste --------------------- rules! --------/
"I am lying."
- Anon
Nov 13 '05 #2
On Tue, 28 Oct 2003 11:54:55 +0100, Rick wrote:
Hi again,

I once saw that it was possible to define operators in C++ or something so I
was thinking, is it possible to store and use operators in C? For example,
first I read out a formula char by char. At some point I detect a operator
char ( + / - * ^ mod etc. ). After parsing the formula I want to use it as
quick as possible. So instead of reading that string again and again I'd
like to store all the functions/values and operators in structs or something
like that. I build this before in Visual Basic and when it came to the
execution of an operator I used something like this :

switch ( operator ) // operator is a field of a
struct( called type in VB )
case 0: output = input + x; // 0 stands for +
case 1: output = input - x; // 1 stands for minus
case 2: output = input * x;
case 3: output = input / x;
< etc. >
It worked well but maybe in C it could be faster by just doing :
output = input operator x;

The same thing for functions, while I'm now using
switch( function )
case 0: x = cos( x );
case 1: x = sin( x );
< and so on >
I'd like to store the function itself so I can directly use it like :
x = function( x );

Is this all possible?


Unfortunately this isn't possible in C. It also isn't possible in
C++. However, your idea *is* very good and very powerful and is
allowed in programming languages called "functional languages".
Some examples of languages that let you do this are the languages
"scheme", "common lisp", and "ocaml". If you google those terms
you'll get more information than you can handle.

-Sheldon

Nov 13 '05 #3

"Joona I Palaste" <pa*****@cc.helsinki.fi> wrote in message
news:bn*********@oravannahka.helsinki.fi...
Rick <as******@hotmail.com> scribbled the following:
Hi again,

I once saw that it was possible to define operators in C++ or something so I was thinking, is it possible to store and use operators in C? For example, first I read out a formula char by char. At some point I detect a operator char ( + / - * ^ mod etc. ). After parsing the formula I want to use it as quick as possible. So instead of reading that string again and again I'd
like to store all the functions/values and operators in structs or something like that. I build this before in Visual Basic and when it came to the
execution of an operator I used something like this :

switch ( operator ) // operator is a field of a
struct( called type in VB )
case 0: output = input + x; // 0 stands for +
case 1: output = input - x; // 1 stands for minus
case 2: output = input * x;
case 3: output = input / x;
< etc. >
It worked well but maybe in C it could be faster by just doing :
output = input operator x;
C and C++ are very different languages, though I don't think there is
another way to do it in C++. Switch should be pretty efficient, at least
many compilers will implement a jump table. Don't forget the break
statement, though.
The same thing for functions, while I'm now using
switch( function )
case 0: x = cos( x );
case 1: x = sin( x );
< and so on >
I'd like to store the function itself so I can directly use it like :
x = function( x );

Is this all possible?


No. In C (C90 at least) operators are an entirely compile-time concept.


Well, this one can be done with function pointers.I believe that function
pointers can be used on C library functions, though I don't think I have
ever tried. Some languages, especially ones with generic library
functions, don't allow that, as the compiler wouldn't know what which
routine to call.

-- glen
Nov 13 '05 #4
Rick wrote:

I once saw that it was possible to define operators in C++ or something so I
was thinking, is it possible to store and use operators in C? For example,
first I read out a formula char by char. At some point I detect a operator
char ( + / - * ^ mod etc. ). After parsing the formula I want to use it as
quick as possible. So instead of reading that string again and again I'd
like to store all the functions/values and operators in structs or something
like that. I build this before in Visual Basic and when it came to the
execution of an operator I used something like this :

switch ( operator ) // operator is a field of a
struct( called type in VB )
case 0: output = input + x; // 0 stands for +
case 1: output = input - x; // 1 stands for minus
case 2: output = input * x;
case 3: output = input / x;
< etc. >
It worked well but maybe in C it could be faster by just doing :
output = input operator x;
Not faster, but you could do it differently.
The same thing for functions, while I'm now using
switch( function )
case 0: x = cos( x );
case 1: x = sin( x );
< and so on >
I'd like to store the function itself so I can directly use it like :
x = function( x );

Is this all possible?


Only with function pointers.

#include <math.h>

double ad(double a, double b) { return a + b; }
double sb(double a, double b) { return a - b; }
double ml(double a, double b) { return a * b; }
double dv(double a, double b) { return a / b; }

double (*f1[])(double) = { sin, cos, tan };
double (*f2[])(double, double) = { ad, sb, dv, ml };

int main()
{
int i;

for (i=0; i<sizeof f1 / sizeof *f1; i++) printf("f1[%d] = %f\n", i, f1[i](42));
for (i=0; i<sizeof f2 / sizeof *f2; i++) printf("f2[%d] = %f\n", i, f2[i](42, 11));

return 0;
}

Jirka

Nov 13 '05 #5
"Rick" <as******@hotmail.com> wrote in message news:<3f***********************@news.xs4all.nl>...
Hi again,

I once saw that it was possible to define operators in C++ or something so I
was thinking, is it possible to store and use operators in C? For example,
first I read out a formula char by char. At some point I detect a operator
char ( + / - * ^ mod etc. ). After parsing the formula I want to use it as
quick as possible. So instead of reading that string again and again I'd
like to store all the functions/values and operators in structs or something
like that. I build this before in Visual Basic and when it came to the
execution of an operator I used something like this :

switch ( operator ) // operator is a field of a
struct( called type in VB )
case 0: output = input + x; // 0 stands for +
case 1: output = input - x; // 1 stands for minus
case 2: output = input * x;
case 3: output = input / x;
< etc. >
It worked well but maybe in C it could be faster by just doing :
output = input operator x;

The same thing for functions, while I'm now using
switch( function )
case 0: x = cos( x );
case 1: x = sin( x );
< and so on >
I'd like to store the function itself so I can directly use it like :
x = function( x );

Is this all possible?


You can store function pointers...

#include <stdio.h>

int addition (int x, int y) { return x + y; }
int subtraction (int x, int y) { return x - y; }
int multiplication (int x, int y) { return x * y; }
int division (int x, int y) { return x / y; }

int (*operators[])(int, int) =
{
addition, subtraction, multiplication, division
};

int main()
{
int x = 42, y = 3;
size_t i;

for (i = 0; i < sizeof operators / sizeof *operators; i++)
{
printf("%d\n", operators[i](x, y));
}

return 0;
}

--
Peter
Nov 13 '05 #6
Thanks again! Too bad about the operators but hey, there are not that much
operators so a case will do( thanks about noticing the break, I really
forgot that!).

Greetings,
Rick
Nov 13 '05 #7

This discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

30 posts views Thread by | last post: by
2 posts views Thread by victor75040 | last post: by
2 posts views Thread by wongjoekmeu | last post: by
20 posts views Thread by Patrick Guio | last post: by
2 posts views Thread by Harry | last post: by
67 posts views Thread by carlos | last post: by
56 posts views Thread by spibou | last post: by
18 posts views Thread by Ranganath | last post: by
reply views Thread by devrayhaan | last post: by
reply views Thread by gheharukoh7 | last post: by
By using this site, you agree to our Privacy Policy and Terms of Use.