473,396 Members | 1,768 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,396 software developers and data experts.

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 1876
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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

7
by: Paul Davis | last post by:
I'd like to overload 'comma' to define a concatenation operator for integer-like classes. I've got some first ideas, but I'd appreciate a sanity check. The concatenation operator needs to so...
30
by: | last post by:
I have not posted to comp.lang.c++ (or comp.lang.c++.moderated) before. In general when I have a C++ question I look for answers in "The C++ Programming Language, Third Edition" by Stroustrup....
2
by: victor75040 | last post by:
Before you all start flaming me, I am not a student and this is not for any homework. Just someone learing c++ on their own. I am now up to the chapter in my book that describes operator...
2
by: wongjoekmeu | last post by:
Hello All, I have a question about a C++ listing that I don't understand from a book that I use to learn C++. In the listing a class String is declared and defined. The beginning look like this...
4
by: Mark Stijnman | last post by:
A while ago I posted a question about how to get operator behave differently for reading and writing. I basically wanted to make a vector that can be queried about whether it is modified recently...
20
by: Patrick Guio | last post by:
Dear all, I have some problem with insertion operator together with namespace. I have a header file foo.h containing declaration of classes, typedefs and insertion operators for the typedefs in...
2
by: Harry | last post by:
Hi all, I am writing a logger program which can take any datatype. namespace recordLog { enum Debug_Level {low, midium, high}; class L { std::ofstream os; Debug_Level cdl; const...
67
by: carlos | last post by:
Curious: Why wasnt a primitive exponentiation operator not added to C99? And, are there requests to do so in the next std revision? Justification for doing so: C and C++ are increasingly used...
56
by: spibou | last post by:
In the statement "a *= expression" is expression assumed to be parenthesized ? For example if I write "a *= b+c" is this the same as "a = a * (b+c)" or "a = a * b+c" ?
18
by: Ranganath | last post by:
Why is throw keyword considered as an operator?
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.