题解 | #表达式求值#

表达式求值

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;
}

全部评论

相关推荐

来个大佬救一下,为上投了都是石沉大海了,没实习经历的话怕秋招直接进不了面。什么实习这么难找,基本
心态爆炸了:现在正式的岗位都少,实习基本不咋招的,除了大厂,中小企业其实没那么多岗位需求,就算是有,大多都是招一两个廉价劳动力,同时,他们也会希望你一来就能干活的,没时间培训你,就让你了解公司的项目,你了解完就可以开始干活。再者是,很多低质量的实习其实用处没有那么大的。我去年也是找实习找到破防,最后去了一家深圳的小公司实习,工作对我来说很简单,甚至不如我在学校做的项目,秋招的时候,这段实习经历也并没有帮上什么忙,投递简历,依旧非常低的回复率。低回复率是常态,尤其是找实习,找不到,那就把重心放在优化自己的简历和项目,多看八股文,锻炼自己的面试能力,多看别人的面经,自己模拟面试,等秋招的时候,只要有那么寥寥几次,好好抓住那几次机会。
点赞 评论 收藏
分享
06-08 22:25
门头沟学院 Java
从零开始的转码生活:这hr不会打开手机不分青红皂白给所有人群发这句话,过一会再给所有人再发一遍,这肯定会有重复的,不管,再过一会再发一遍
点赞 评论 收藏
分享
点赞 评论 收藏
分享
评论
点赞
收藏
分享

创作者周榜

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