Basic Calculator II

Implement a basic calculator to evaluate a simple expression string.

The expression string contains only non-negative integers, +, -, *, / operators and empty spaces . The integer division should truncate toward zero.

You may assume that the given expression is always valid.

Some examples:
“3+2*2” = 7
” 3/2 ” = 1
” 3+5 / 2 ” = 5

说明:先将中缀表达式转化为后缀表达式,再由后缀表达式计算表达式的值。

代码:

struct pri
{
    char op;
    int pri;
};

//设置符号的优先级,便于后续比较符号优先级
struct pri PriArray[5] = {{'=',0}, {'+',1}, {'-',1}, {'*',2}, {'/',2}};

//查找符号的优先级
int findpri(char c)
{
    int i = 0;
    for(i = 0; i < 5; i++)
    {
        if(c == PriArray[i].op)
            return PriArray[i].pri;
    }
    return 0;
}
//比较两个符号的优先级(符号栈栈顶符号和遍历中缀表达式的符号)
int pricom(char top, char tmp)
{
        if(findpri(top) < findpri(tmp))//栈顶符号优先级低,入栈
        return 1;
    else    //栈顶符号优先级高,出栈
        return -1;
}

//中缀表达式转换为后缀表达式
void transfer(char *s, char* postfix)
{
    char *opStack = (char *)malloc(sizeof(char) * strlen(s));  //存放中缀表达式符号的栈
    int top = -1;
    top++;
    char c;
    int i = 0, j = 0;

    opStack[top] = '=';

    while(s[i] != '\0')
    {
        c = s[i];
        if(c >= '0' && c <= '9')
        {
            while(s[i] >= '0' && s[i] <= '9')
            {
                postfix[j++] = s[i++];
            }
            postfix[j++] = '#';
        }
        else if(c == ' ')
        {
            i++;
        }
        else
        {
            switch(pricom(opStack[top], c)){
                case 1:
                    top++;
                    opStack[top] = c;
                    i++;
                    break;
                case -1:
                    while(pricom(opStack[top], c) == -1)
                    {
                        postfix[j++] = opStack[top];
                        top--;
                    }
                    top++;
                    opStack[top] = c;
                    i++;
                    break;
            }

        }
    }
    while(top > 0)    //将符号栈剩余符号全部出栈
    {
        postfix[j++] = opStack[top];
        top--;
    }
    postfix[j] = '\0';
    free(opStack);
}

//计算表达式的值
int calculate(char* s) {
    int *num = (int *)malloc(sizeof(int) * strlen(s)); //存储后缀表达式的数字
    int a = 0, b = 0;
    char *postfix = (char *)malloc(sizeof(char) * 2 * strlen(s)); //存储后缀表达式
    transfer(s, postfix);
    //printf("%s\n",postfix);
    int top = -1, i = 0;
    int tmp;
    //由后缀表达式计算表达式的值
    while(postfix[i] != '\0')  
    {

        switch(postfix[i]){
            case '+':
                a = num[top--];
                b = num[top--];
                num[++top] = a + b;
                i++;
                break;
            case '-':
                a = num[top--];
                b = num[top--];
                num[++top] = b - a;
                i++;
                break;
            case '*':
                a = num[top--];
                b = num[top--];
                num[++top] = a * b;
                i++;
                break;
            case '/':
                a = num[top--];
                b = num[top--];
                num[++top] = b / a;
                i++;
                break;
            default:
                tmp = 0;
                while(postfix[i] >= '0' && postfix[i] <= '9')
                {
                    tmp = 10 * tmp + postfix[i] - '0';
                    i++;
                }
                num[++top] = tmp;
                if(postfix[i] == '#')
                    i++;
                break;
        }
    }
    i = num[top];
    free(num);
    return i;
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值