Operator question

R

Rick

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
 
J

Joona I Palaste

Rick said:
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.
 
S

Sheldon Simms

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
 
G

Glen Herrmannsfeldt

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.
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
 
J

Jirka Klaue

Rick said:
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(42));
for (i=0; i<sizeof f2 / sizeof *f2; i++) printf("f2[%d] = %f\n", i, f2(42, 11));

return 0;
}

Jirka
 
P

Peter Nilsson

Rick said:
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(x, y));
}

return 0;
}
 
R

Rick

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
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
474,091
Messages
2,570,605
Members
47,225
Latest member
DarrinWhit

Latest Threads

Top