By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
455,431 Members | 1,811 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 455,431 IT Pros & Developers. It's quick & easy.

Char Math Symbols

P: 1
I'm new to C++ and need to write a program that reads and calculates math equations from a char string. My question: is there a way to convert a char math symbol such as '+' into a + the compiler can use in an equation?

Right now I'm using a bunch of If statements to handle this, but would like to know if there's a way to work the char sign directly into the equation.

i.e.

char num1 = '5';
char sign = '+';
char num2 = '2';

if (sign == '+')
{
int sum = (num1 - '0') + (num2 - '0');
// want to do somthing like: int sum = (num1 - '0') sign (num2 - '0');
}
if (sign == '-')

.... etc

Thanks.
Nov 9 '08 #1
Share this Question
Share on Google+
2 Replies


Ganon11
Expert 2.5K+
P: 3,652
Not that I know of. The way you're doing it is the way I would do it.
Nov 9 '08 #2

Expert 10K+
P: 11,448
@OP: you're talking about the rudiments of an expression interpreter. Google for
infix-expression and postfix-expression. The second form is easy to evaluate by
using a simple stack.

kind regards,

Jos
Nov 9 '08 #3

Post your reply

Sign in to post your reply or Sign up for a free account.