判断字符是否为运算符及运算符优先级

/*extend.h*/

//判断ch是否为运算符
int Comop(char ch)
{
    switch(ch)
    {
        case '+':
        case '-':
        case '*':
        case '/':
        case '(':
        case ')':
        case '#':return 1;
        default:return 0;
    }
}//Comop

//比较两个运算符的优先级
char precede(char ch,char c)//ch是栈顶字符,c 是表达式字符
{
    switch(ch)
    {
    case '+':
        if(c=='+')return '>';
        if(c=='-')return '>';
        if(c=='*')return '<';
        if(c=='/')return '<';
        if(c=='(')return '<';
        if(c==')')return '>';
        if(c=='#')return '>';
    case '-':
        if(c=='+')return '>';
        if(c=='-')return '>';
        if(c=='*')return '<';
        if(c=='/')return '<';
        if(c=='(')return '<';
        if(c==')')return '>';
        if(c=='#')return '>';
    case '*':
        if(c=='+')return '>';
        if(c=='-')return '>';
        if(c=='*')return '>';
        if(c=='/')return '>';
        if(c=='(')return '<';
        if(c==')')return '>';
        if(c=='#')return '>';
    case '/':
        if(c=='+')return '>';
        if(c=='-')return '>';
        if(c=='*')return '>';
        if(c=='/')return '>';
        if(c=='(')return '<';
        if(c==')')return '>';
        if(c=='#')return '>';
    case '(':
        if(c=='+')return '<';
        if(c=='-')return '<';
        if(c=='*')return '<';
        if(c=='/')return '<';
        if(c=='(')return '<';
        if(c==')')return '=';
        if(c=='#')return ' ';
    case ')':
        if(c=='+')return '>';
        if(c=='-')return '>';
        if(c=='*')return '>';
        if(c=='/')return '>';
        if(c=='(')return ' ';
        if(c==')')return '>';
        if(c=='#')return '>';
    case '#':
        if(c=='+')return '<';
        if(c=='-')return '<';
        if(c=='*')return '<';
        if(c=='/')return '<';
        if(c=='(')return '<';
        if(c==')')return ' ';
        if(c=='#')return '=';
    default:
        return '$';
    }
}//precede

你可能感兴趣的:(算法相关,c)