944,155 Members | Top Members by Rank

Ad:
  • C++ Discussion Thread
  • Unsolved
  • Views: 1411
  • C++ RSS
Mar 18th, 2007
0

variables holding operators?

Expand 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.
Similar Threads
Reputation Points: 10
Solved Threads: 1
Newbie Poster
JadedTortoise is offline Offline
8 posts
since Mar 2007
Mar 18th, 2007
0

Re: variables holding operators?

No.

What are you trying to do.
Featured Poster
Reputation Points: 1536
Solved Threads: 431
Posting Expert
iamthwee is offline Offline
5,865 posts
since Aug 2005
Mar 18th, 2007
0

Re: variables holding operators?

you can use it in a switch statement
C++ Syntax (Toggle Plain Text)
  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. }
Sponsor
Team Colleague
Featured Poster
Reputation Points: 5608
Solved Threads: 2283
Retired and Enjoying Life
Ancient Dragon is offline Offline
21,963 posts
since Aug 2005
Mar 18th, 2007
0

Re: variables holding operators?

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...
Reputation Points: 28
Solved Threads: 2
Junior Poster in Training
vicky_dev is offline Offline
86 posts
since May 2005
Mar 18th, 2007
0

Re: variables holding operators?

Click to Expand / Collapse  Quote originally posted by vicky_dev ...
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.
Sponsor
Team Colleague
Featured Poster
Reputation Points: 5608
Solved Threads: 2283
Retired and Enjoying Life
Ancient Dragon is offline Offline
21,963 posts
since Aug 2005
Mar 18th, 2007
0

Re: variables holding operators?

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?
Reputation Points: 10
Solved Threads: 1
Newbie Poster
JadedTortoise is offline Offline
8 posts
since Mar 2007
Mar 18th, 2007
0

Re: variables holding operators?

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.

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.
Sponsor
Team Colleague
Featured Poster
Reputation Points: 5608
Solved Threads: 2283
Retired and Enjoying Life
Ancient Dragon is offline Offline
21,963 posts
since Aug 2005

This thread is more than three months old

No one has posted to this discussion for at least three months. Please let old threads die and do not reply to them unless you feel you have something new and valuable to contribute that absolutely must be added to make the discussion complete. Otherwise, please start a new thread in this forum instead.
Message:
Previous Thread in C++ Forum Timeline: Linked Lists Help C++
Next Thread in C++ Forum Timeline: Dynamic Allocation





About Us | Contact Us | Advertise | Acceptable Use Policy
Forum Index | Build Custom RSS Feed


Follow us on Twitter


© 2011 DaniWeb® LLC