题解 | #表达式求值#

表达式求值

https://www.nowcoder.com/practice/c215ba61c8b1443b996351df929dc4d4

双堆求值

/**
 * 代码中的类名、方法名、参数名已经指定,请勿修改,直接返回方法规定的值即可
 *
 * 返回表达式的值
 * @param s string字符串 待计算的表达式
 * @return int整型
 */
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#define MAXSIZE 100
struct stack_op {
    char data[MAXSIZE];
    int top;
};
struct stack_num {
    int data[MAXSIZE];
    int top;
};
void push(struct stack_op* stac, char temp) {
    if (stac->top != MAXSIZE - 1) {
        stac->data[++stac->top] = temp;
    }
}
void push_num(struct stack_num* stac, int temp) {
    if (stac->top != MAXSIZE - 1) {
        stac->data[++stac->top] = temp;
    }
}
void pop(struct stack_op* stac, char* temp) {
    if (stac->top != -1) {
        *temp = stac->data[stac->top--];
    }
}
void pop_num(struct stack_num* stac, int* temp) {
    if (stac->top != -1) {
        *temp = stac->data[stac->top--];
    }
}
int cal(int k1, int k2, char opt) {
    if (opt == '+') return k2 + k1;
    else if (opt == '-') return k2 - k1;
    else return k2 * k1;
}
int solve(char* s ) {
    //初始化
    struct stack_op* opt = (struct stack_op*)malloc(sizeof(struct stack_op));
    struct stack_num* num = (struct stack_num*)malloc(sizeof(struct stack_num));
    opt->top = -1;
    num->top = -1;
    int i = 0;
    //
    while (s[i] != '\0') {
        //判断当前值是否为数字
        if (s[i] >= '0' && s[i] <= '9') {
            int k = 0;
            while (s[i] != '\0' && s[i] >= '0' && s[i] <= '9') {
                k = k * 10 + (s[i] - '0');
                i++;
            }
            push_num(num, k);
        } else {
            //左括号
            if (s[i] == '(')
                push(opt, s[i]);
            //右括号,到做左括号为止
            else if (s[i] == ')') {
                char temp_opt = ')';
                while (opt->data[opt->top] != '(') {
                    int temp_num1;
                    int temp_num2;
                    pop_num(num, &temp_num1);
                    pop_num(num, &temp_num2);
                    pop(opt, &temp_opt);
                    push_num(num, cal(temp_num1, temp_num2, temp_opt));
                }
                pop(opt, &temp_opt);
            } else if (s[i] == '*') {
                push(opt, s[i]);
            } else {
                while (num->top >= 1) {
                    if (opt->data[opt->top] == '(')
                        break;
                    int temp_num1;
                    int temp_num2;
                    char temp_opt;
                    pop_num(num, &temp_num1);
                    pop_num(num, &temp_num2);
                    pop(opt, &temp_opt);
                    push_num(num, cal(temp_num1, temp_num2, temp_opt));
                }
                push(opt, s[i]);
            }
            i++;
        }
    }
    //最后可能的情况
    if (opt->top != -1) {
        int temp_num1;
        int temp_num2;
        char temp_opt;
        pop_num(num, &temp_num1);
        pop_num(num, &temp_num2);
        pop(opt, &temp_opt);
        push_num(num, cal(temp_num1, temp_num2, temp_opt));
    }
    int resul;
    pop_num(num, &resul);
    return resul;
}

全部评论

相关推荐

我是没经验的毕业生,这啥情况啊会不会是hr在刷kpi
JamesGosli...:字节boss属于是群发了,我都快入职字节了,其他部门还在和我boss打招呼
点赞 评论 收藏
分享
不愿透露姓名的神秘牛友
07-04 14:35
点赞 评论 收藏
分享
小浪_Coding:找硬件测试,也可兼顾软测欧, 简历还可以的 ,注意排版,项目写的有条理一点, 然后个人技能多加点, 润色好简历之后就开始沟通海投了,深圳,东莞这边做硬件相关的公司还不少, 医疗类,仪器类的都可以尝试
点赞 评论 收藏
分享
评论
点赞
收藏
分享

创作者周榜

更多
牛客网
牛客网在线编程
牛客网题解
牛客企业服务