预定义的运算符优先级

时间:2014-11-19 02:18:58

标签: c algorithm

我做了一个练习,我得到3个整数和2个运算符^,/,+, - ,*,%之一。 为了评估结果,它是六个运营商中每个运营商的预定义优先级表。 我在交换机中使用了Switch 6次,每个运营商共有36种组合,如下所示(例如^)

 switch(oper1){
        case '^':
            switch(oper2){
                case '^':
                    result=first_Num^second_Num^third_Num;
                    printf("Result: %.2f",result);
                    break;
                case '/':
                    if(third_Num==0){
                        printf("Error");
                    }
                    else{
                        result=(first_Num^second_Num)/(float)third_Num;
                        printf("Result :%.2f",result);
                    }
                        break;
                case '-':
                    result=(first_Num^second_Num)-third_Num;
                    printf("Result: %.2f",result);
                    break;
                case '+':
                    result=(first_Num^second_Num)+third_Num;
                    printf("Result: %.2f",result);
                    break;
                case '*':
                    result=(first_Num^second_Num)*third_Num;
                    printf("Result: %.2f",result);
                    break;
                case '%':
                    if(third_Num==0){
                        printf("Error");
                    }
                    else{
                        result=(first_Num^second_Num)%third_Num;
                        printf("Result: %.2f",result);
                    }
                    break;
                default:;
            }
    }

我试图使用每个运算符的ASCII值,并通过减去两个运算符来找出哪些运算符,并通过正负号来确定顺序。 例如,如果第一个运算符是/且第二个运算符是*,则它将是47-42 = 5,如果结果是-5,则它将是*第一和/或第二。

我正在尝试找到可用支票数量最少的算法

1 个答案:

答案 0 :(得分:1)

只需要设置一个运算符优先级表,将每个运算符映射到它的优先级,并指向一个执行所需操作的函数的指针。

然后你的算法计算结果(硬编码为2个运算符和3个整数参数)就像下面的calculate()函数。

#include <stdio.h>

/* operator implementations */
int add(int a, int b) { return (a + b); }
int sub(int a, int b) { return (a - b); }
int mul(int a, int b) { return (a * b); }
int div(int a, int b) { return (a / b); }
int mod(int a, int b) { return (a % b); }
int xor(int a, int b) { return (a ^ b); }

struct operator
{
    char token;
    int precedence;
    int (*op)(int, int);
};

struct operator operators[] = {
/*  map operator token to precendence and implementation function */
    {'+', 1, add},
    {'-', 1, sub},
    {'%', 1, mod},
    {'*', 2, mul},
    {'/', 2, div},
    {'^', 4, xor},
};

struct operator *get_operator(char token)
{
    int i;

    for (i=0; i<(sizeof(operators)/sizeof(struct operator)); i++)
        if (operators[i].token == token)
            return &operators[i];
    return (struct operator *)NULL;
}

struct operator *static_get_operator(char token)
{
    /* hard-coded but faster than iterating get_operator() above */
    switch (token) {
        case '+': return &operators[0];
        case '-': return &operators[1];
        case '%': return &operators[2];
        case '*': return &operators[3];
        case '/': return &operators[4];
        case '^': return &operators[5];
    }
   return (struct operator *)NULL;
}

int calculate(char op_token1, char op_token2, int arg1, int arg2, int arg3)
{
    struct operator *op1 = get_operator(op_token1);
    struct operator *op2 = get_operator(op_token2);

    if (op1->precedence > op2->precedence)
        return op2->op(op1->op(arg1, arg2), arg3);
    else
        return op1->op(arg1, op2->op(arg2, arg3));
}

int main()
{
    int result;

    result = calculate('+', '*', 1, 2, 3);
    printf("result = %d\n", result);
    result = calculate('*', '+', 1, 2, 3);
    printf("result = %d\n", result);
    result = calculate('*', '^', 10, 10, 2);
    printf("result = %d\n", result);
    result = calculate('-', '-', 10, 10, 2);
    printf("result = %d\n", result);
    result = calculate('-', '/', 10, 10, 3);
    printf("result = %d\n", result);
}

当然,这仅限于2个运算符和3个参数,它需要对无效令牌进行错误处理,并为'/''%'操作分为零错误。