GIDForums  

Go Back   GIDForums > Computer Programming Forums > C++ Forum
User Name
Password
Register FAQ Members List Calendar Search Today's Posts Mark Forums Read

 
 
Thread Tools Search this Thread Rate Thread
  #1  
Old 04-Sep-2003, 07:51
calculus87's Avatar
calculus87 calculus87 is offline
Junior Member
 
Join Date: Sep 2003
Location: FLorida
Posts: 41
calculus87 is on a distinguished road

char to operator


hello,
Here is my question.
Is it possible to go from char to operator.

char a='+';
//code to switch from char to operator
int b,c=3,d=5;
b=c a d;
  #2  
Old 04-Sep-2003, 09:22
Garth Farley Garth Farley is offline
Invalid Email Address
 
Join Date: May 2002
Location: Ireland
Posts: 638
Garth Farley is a jewel in the roughGarth Farley is a jewel in the roughGarth Farley is a jewel in the rough
Not easily with C++. You're best bet is to do a bunch of if statements, so:
CPP / C++ / C Code:
if('+' == a){
        b = c + d;
}
else if('-' == a){
            ..........
}

And a switch statement would be neater.

GF
  #3  
Old 04-Sep-2003, 09:26
calculus87's Avatar
calculus87 calculus87 is offline
Junior Member
 
Join Date: Sep 2003
Location: FLorida
Posts: 41
calculus87 is on a distinguished road

ok thanks


I was thinking about implementing a massive switch statement, but I guess the problem goes a little deeper.
I would like to read in equations from a text file and be able to execute them. The equations would be variable and the inputs would be variable.
I guess I am looking for a formula builder.
  #4  
Old 04-Sep-2003, 10:05
Garth Farley Garth Farley is offline
Invalid Email Address
 
Join Date: May 2002
Location: Ireland
Posts: 638
Garth Farley is a jewel in the roughGarth Farley is a jewel in the roughGarth Farley is a jewel in the rough
This is exactly my second C++ programming assignment

Which is kinda good & bad news.

This is not an easy problem - you'd think it is - but it takes a good bit of work! (I know, I was surprised too).

A simple equation "12 / 6" won't be hard to code for, but how about "12 / 3 * 2" And then the dreaded "2 + 12 / 3" What operator first? (They all give you 6, By the way).

Take that last equation. Consider a simple parser which will step along the string, chunk (number or operator) by chunk at a time.

It'll see 2. Plus sign. 12. Then it'll execute it 2 + 12 =14. Then a division sign. 3. 14/3 = 4.6666... - Not correct!

Maybe brackets will help? They will, but it makes the coding tougher.

Try some ideas! Have you a lecturer, or are you learning this yourself. This is a tough exercise, but I'll try to help as best I can!

GF
 
 

Recent GIDBlogWriting a book by crystalattice

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Forum Jump

Similar Threads
Thread Thread Starter Forum Replies Last Post
C++ Irc Bot Boogy C++ Forum 16 05-Aug-2006 13:04
help with getting single char SANman C++ Forum 10 19-Jul-2006 08:23
convert long to pointer to char realpopeye C++ Forum 2 26-Sep-2003 10:22
Operator overloading (not happening) gmn C++ Forum 11 30-Aug-2003 08:18
a question about / operator Charunks C++ Forum 2 21-Jul-2003 11:38

Network Sites: GIDNetwork · GIDWebHosts · GIDSearch · Learning Journal by J de Silva, The

All times are GMT -6. The time now is 20:25.


vBulletin, Copyright © 2000 - 2008, Jelsoft Enterprises Ltd.