Better Functions to check for a symbol, operator or number.
Jul 25, 2012 at 7:33am UTC
I have tried to create functions to check whether a given character is a symbol or an operator or a number. These functions will be used in a program to convert an infix expression to postfix expression.
Here is my attempt:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52
bool isOperator(char op)
{
switch (op)
{
case '+' :
case '-' :
case '*' :
case '/' :
return true ;
default :
return false ;
}
}
bool isSymbol(char sym)
{
switch (sym)
{
case '%' :
case '^' :
case '&' :
case '(' :
case ')' :
case '!' :
case '@' :
case '#' :
case '$' :
return true ;
default :
return false ;
}
}
bool isNumber(char num)
{
switch (num)
{
case '0' :
case '1' :
case '2' :
case '3' :
case '4' :
case '5' :
case '6' :
case '7' :
case '8' :
case '9' :
return true ;
default :
return false ;
}
}
Are there any inbuilt functions that can be used for this purpose ?
or the functions written above a good enough ?
Jul 25, 2012 at 8:15am UTC
Jul 25, 2012 at 9:52am UTC
Thanks for replying..
Jul 25, 2012 at 10:37am UTC
char enterYourKeY;
cin>>enterYourKey;
int ascii = atoi(enterYourKey);
if(ascii > 42 && ascii < 48)
cout << "operator";
else if(ascii > 32 && ascii < 43)
cout << "Symbol";
else if(ascii > 47 && ascii < 58)
cout << " number";
else
cout << "letter";
Last edited on Jul 25, 2012 at 10:38am UTC
Topic archived. No new replies allowed.