473,396 Members | 1,971 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,396 software developers and data experts.

Char Math Symbols

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
2 4830
Ganon11
3,652 Expert 2GB
Not that I know of. The way you're doing it is the way I would do it.
Nov 9 '08 #2
JosAH
11,448 Expert 8TB
@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

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

Similar topics

12
by: Nitin Manoharan | last post by:
Hello, How can I convert an int to a *char (best way please) I know a few round about methods myself :) Thank you
3
by: schwehr | last post by:
Done any one have a way (and examples) of how to do math markups in the docstrings of function and class definitions such that the equations get typeset in the generated html documentation? I'll...
13
by: Nicholas | last post by:
How can I compare char* with integers and characters contained in the str, where integers can be one digit or more? void Access(char *str) { char *pt = str; while (pt != '0') { if...
80
by: Gaijinco | last post by:
Is there an elegant way to do something like // given a char {0,1,2,3,4,5,6,7,8,9} returns it as a short short char2digit(char c) { short d = c-48; return d; } ?
1
by: Andrew Dabrowski | last post by:
I'm working on a simple Python program, with Tkinter, and I need to display the math symbols for set union and intersection, in unicode \N{N-ARY UNION} and \N{N-ARY INTERSECTION}. Everything was...
1
by: dabrowsa | last post by:
I'm working on a simple Python program, with Tkinter, and I need to display the math symbols for set union and interesection, in unicode \N{N-ARY UNION} and \N{N-ARY INTERSECTION}. Everything was...
15
by: Alok Kumar | last post by:
#include <string.h> void myfn() { char a = 'A'; char b; strcpy(b, &a); } Would I always get 'A' in b and '\0' in b after the strcpy?
9
by: anon.asdf | last post by:
In terms of efficieny: Is it better to use multiple putchar()'s after one another as one gets to new char's OR is it better to collect the characters to a char-array first, and then use...
3
by: gradeexrex | last post by:
I'm not a professional dba or dbd, but I'm proficient in the basics of database design and sql. I want to create a database of math definitions, and I'm wondering how one would go about creating a...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
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,...
0
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,...
0
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...
0
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...

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.