Better Functions to check for a symbol, operator or number.

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 ?
For the last one, there is isdigit:
http://www.cplusplus.com/reference/clibrary/cctype/isdigit/

It could also be made shorter by writing the following:
1
2
3
4
bool isNumber(char num)
{
    return num>='0' && num<='9';
}
Thanks for replying..
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
Topic archived. No new replies allowed.