473,581 Members | 2,647 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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 1889
Rick <as******@hotma il.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.hel sinki.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.hel sinki.fi> wrote in message
news:bn******** *@oravannahka.h elsinki.fi...
Rick <as******@hotma il.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******@hotma il.com> wrote in message news:<3f******* *************** *@news.xs4all.n l>...
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
8022
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 something like this: 1) e = (a, b, c, d); // concatenate a,b,c,d into e 2) (a, b, c, d) = e; // get the bits of e into a,b,c, and d For example,...
30
10408
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. However, I've come upon a question that I can neither answer from "The Book" or a Google search (so yes, at least I RTFBed). I'm hoping that someone...
2
5867
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 overloading. I just cannot find any explanation that clearly point out what the parts of the statement refer to. For example the book says: comp...
2
6396
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 ---------- #include <iostream> #include <string.h> using namespace std; Class String
4
2077
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 or not. My first idea, using the const and non-const versions of operator, was clearly not correct, as was pointed out. Julián Albo suggested I...
20
3851
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 a named namespace namespace foo { class Foo
2
2164
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 Debug_Level ddl;
67
8606
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 in low-level numerical computations, replacing Fortran in newer projects. Check, for example, sourceforge.net or freshmeat.net But neither...
56
4743
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
3212
by: Ranganath | last post by:
Why is throw keyword considered as an operator?
0
7869
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, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main...
0
7797
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language...
0
8151
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, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. ...
0
8306
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 tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that...
1
7900
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 Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For...
0
6555
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then...
1
5677
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes...
0
5365
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert...
0
1139
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating...

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.