variables holding operators?

Please support our C++ advertiser: Intel Parallel Studio Home
Reply

Join Date: Mar 2007
Posts: 8
Reputation: JadedTortoise is an unknown quantity at this point 
Solved Threads: 1
JadedTortoise JadedTortoise is offline Offline
Newbie Poster

variables holding operators?

 
0
  #1
Mar 18th, 2007
OK I am wondering Can you make a variable hold an operator.
Such as say +, -, / etc...

And if so can that variable be used to replace an operator in an equation?

for instance

char a = "+";
int b = (2 a 3);

thoguh that doesn't work itself i think it conveys what i am looking for.
Reply With Quote Quick reply to this message  
Join Date: Aug 2005
Posts: 5,266
Reputation: iamthwee is a splendid one to behold iamthwee is a splendid one to behold iamthwee is a splendid one to behold iamthwee is a splendid one to behold iamthwee is a splendid one to behold iamthwee is a splendid one to behold iamthwee is a splendid one to behold iamthwee is a splendid one to behold 
Solved Threads: 377
Featured Poster
iamthwee's Avatar
iamthwee iamthwee is offline Offline
Posting Expert

Re: variables holding operators?

 
0
  #2
Mar 18th, 2007
No.

What are you trying to do.
*Voted best profile in the world*
Reply With Quote Quick reply to this message  
Join Date: Aug 2005
Posts: 15,442
Reputation: Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute 
Solved Threads: 1474
Team Colleague
Featured Poster
Ancient Dragon's Avatar
Ancient Dragon Ancient Dragon is offline Offline
Still Learning

Re: variables holding operators?

 
0
  #3
Mar 18th, 2007
you can use it in a switch statement
  1. int b = 1;
  2. int c = 2;
  3. int d = 0;
  4. switch(a)
  5. {
  6. case '+': d = b * c; break;
  7. case '-': d = b - c; break;
  8. case '/': d = b / c; break;
  9. // etc
  10. }
Don't PM me with questions -- you might get a nasty PM in response. If you have a question then post it in one of the forums.
Reply With Quote Quick reply to this message  
Join Date: May 2005
Posts: 86
Reputation: vicky_dev is an unknown quantity at this point 
Solved Threads: 2
vicky_dev's Avatar
vicky_dev vicky_dev is offline Offline
Junior Poster in Training

Re: variables holding operators?

 
0
  #4
Mar 18th, 2007
Originally Posted by JadedTortoise View Post
OK I am wondering Can you make a variable hold an operator.
Such as say +, -, / etc...

And if so can that variable be used to replace an operator in an equation?

for instance

char a = "+";
int b = (2 a 3);

thoguh that doesn't work itself i think it conveys what i am looking for.
You can do this to have the same effect:

  1. #define a +
  2.  
  3. int b = (2 a 3);

But 'a' is not really a variable here.
And I see no reason why you would want to do that...
Reply With Quote Quick reply to this message  
Join Date: Aug 2005
Posts: 15,442
Reputation: Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute 
Solved Threads: 1474
Team Colleague
Featured Poster
Ancient Dragon's Avatar
Ancient Dragon Ancient Dragon is offline Offline
Still Learning

Re: variables holding operators?

 
0
  #5
Mar 18th, 2007
Originally Posted by vicky_dev View Post
You can do this to have the same effect:

  1. #define a +
  2.  
  3. int b = (2 a 3);

But 'a' is not really a variable here.
And I see no reason why you would want to do that...
You might want to do something like that when writing a calculator. for example, if you enter "2 * 3" then your program needs to figure out what to do.
Don't PM me with questions -- you might get a nasty PM in response. If you have a question then post it in one of the forums.
Reply With Quote Quick reply to this message  
Join Date: Mar 2007
Posts: 8
Reputation: JadedTortoise is an unknown quantity at this point 
Solved Threads: 1
JadedTortoise JadedTortoise is offline Offline
Newbie Poster

Re: variables holding operators?

 
0
  #6
Mar 18th, 2007
Originally Posted by Ancient Dragon View Post
You might want to do something like that when writing a calculator. for example, if you enter "2 * 3" then your program needs to figure out what to do.
That is pretty much my reasoning is calculations with the equation from an outside source. I did think of using switches/loops/etc... but they are all rather convoluted ways to do something that would be so simple. Man the more i am learning of C++ the more glaring flaws i am seeing.

Are there any compliers with additions that will fix some things, simple things like length_of(Array), treat_as_value(char). So many things that would make the programing both easier and less taxxing on memory and processor time. And if it was compiled to exe it should retain portability or am i missing something?

I don't know if not is there a langauge I should try that doesn't have such drastic oversights?
Reply With Quote Quick reply to this message  
Join Date: Aug 2005
Posts: 15,442
Reputation: Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute 
Solved Threads: 1474
Team Colleague
Featured Poster
Ancient Dragon's Avatar
Ancient Dragon Ancient Dragon is offline Offline
Still Learning

Re: variables holding operators?

 
0
  #7
Mar 18th, 2007
Originally Posted by JadedTortoise View Post
Are there any compliers with additions that will fix some things, simple things like length_of(Array), treat_as_value(char).
Not in C or C++ languages.

Originally Posted by JadedTortoise View Post
So many things that would make the programing both easier and less taxxing on memory and processor time. And if it was compiled to exe it should retain portability or am i missing something?
C/C++ is probably the least taxing of all computer languages except assembly and machine code. Other languages, such as basic, let you do some things a lot easier but the executable programs are generally larger and slower.

If you want to get reall deep into parsing equations then try YACC (Yet Another Compiler Compiler)
Last edited by Ancient Dragon; Mar 18th, 2007 at 3:38 pm.
Don't PM me with questions -- you might get a nasty PM in response. If you have a question then post it in one of the forums.
Reply With Quote Quick reply to this message  
Reply

This thread is more than three months old.
Perhaps start a new thread instead?
Message:



Similar Threads
Other Threads in the C++ Forum
Thread Tools Search this Thread



About Us | Contact Us | Advertise | DaniWeb | Acceptable Use Policy | RSS Feed

©2003 - 2009 DaniWeb® LLC