Implement a basic calculator to evaluate a simple expression string.

The expression string may contain open ( and closing parentheses ),
the plus + or minus sign -, non-negative integers
and empty spaces .

You may assume that the given expression is always valid.

Some examples:

"1 + 1" = 2
" 2-1 + 2 " = 3
"(1+(4+5+2)-3)+(6+8)" = 23

Note: Do not use the eval built-in
library function.

实现:

void skipws(string &s, int * beg, bool b = false)

    {

        if (b) {

            while (s[*beg] == ' ') {

                (*beg)++;

            }

        } else {

            while (s[*beg] == ' ' || s[*beg] == '(' || s[*beg] == ')') {

                (*beg)++;

            }

        }

    }

    

    

    int numend(string &s, int beg)

    {

        while (s[beg] >= '0' && s[beg] <= '9') {

            beg++;

        }

        return beg;

    }

    

    int parenthesisend(string& s, int beg){

        int brace = 0;

        while (s[beg] != ')' || brace != 0) {

            if (s[beg] == '(') {

                brace++;

            } else if (s[beg] == ')') {

                brace--;

            }

            beg++;

        }

        return beg;

    }

    

    int calculate(string s) {

        int start = 0;

        

        int result = 0;

        while (start < s.size()) {

            skipws(s, &start);

            

            if (s[start] == '+') {

                start++;

                skipws(s, &start);

                int end = numend(s, start);

                result += atoi(s.substr(start, end-start).c_str());

                start = end;

            } else if (s[start] == '-') {

                start++;

                skipws(s, &start, true);

                if (s[start] == '(') {

                    start++;

                    int end = parenthesisend(s, start);

                    result -= calculate(s.substr(start, end-start));

                    start = end+1;

                }

                else {

                    int end = numend(s, start);

                    result -= atoi(s.substr(start, end-start).c_str());

                    start = end;

                }

            } else {

                int end = numend(s, start);

                result = atoi(s.substr(start, end-start).c_str());

                start = end;

            }

            skipws(s, &start);

        }

        return result;

    }

LeetCode224——Basic Calculator的更多相关文章

  1. LeetCode224. Basic Calculator (用栈计算表达式)

    解题思路 用两个栈分别存字符和数字. 顺序读入字符,处理方式分为字符和数字两种. 处理字符分为')'和非')'两种. 处理数字需要读取字符栈栈顶,分为'+'.'-'和非'+'.'-'. 代码 clas ...

  2. [Swift]LeetCode224. 基本计算器 | Basic Calculator

    Implement a basic calculator to evaluate a simple expression string. The expression string may conta ...

  3. [LeetCode] Basic Calculator II 基本计算器之二

    Implement a basic calculator to evaluate a simple expression string. The expression string contains ...

  4. [LeetCode] Basic Calculator 基本计算器

    Implement a basic calculator to evaluate a simple expression string. The expression string may conta ...

  5. Basic Calculator II

    Implement a basic calculator to evaluate a simple expression string. The expression string contains ...

  6. 数据结构与算法(1)支线任务2——Basic Calculator

    题目:https://leetcode.com/problems/basic-calculator/ Implement a basic calculator to evaluate a simple ...

  7. Basic Calculator

    本博客介绍leetcode上的一道不难,但是比较经典的算法题. 题目如下: Implement a basic calculator to evaluate a simple expression s ...

  8. LeetCode#227.Basic Calculator II

    题目 Implement a basic calculator to evaluate a simple expression string. The expression string contai ...

  9. Java for LeetCode 227 Basic Calculator II

    Implement a basic calculator to evaluate a simple expression string. The expression string contains ...

随机推荐

  1. Windows下使用MINGW编译ffplay

    之前考虑到需要快速配置编译ffplay,使用了比较暴力的方法,具体可以参考编译ffplay.exe简化版. 这里介绍下相对规范的做法. 前提:已经安装了Windows下GCC开发环境--MINGW+m ...

  2. crontab格式,命令

    http://www.blogjava.net/xiaomage234/archive/2007/12/26/170490.html crontab格式: 第1列分钟1-59 第2列小时1-23(0表 ...

  3. 【转】关于 SELECT /*!40001 SQL_NO_CACHE */ * FROM 的解惑

    由于 在数据库做了缓存,在对数据库做了备份,然后在慢查询日志中发现了这一串字符: SELECT /*!40001 SQL_NO_CACHE */ * FROM 上网查了一下,发现好多答案,好多人说的都 ...

  4. H3C交换机SNMP配置

    1.启动/关闭SNMP Agent服务 在系统视图模式下: 启用:snmp-agent 关闭:undo snmp-agent 注:缺省情况下snmp agent是关闭的 2. 使能或禁止SNMP相应版 ...

  5. Leetcode: Remove Duplicates from Sorted List II 解题报告

    Remove Duplicates from Sorted List II Given a sorted linked list, delete all nodes that have duplica ...

  6. Python(二)之对象类型

    本篇记录Python对象类型 开始仍然是先掌握几个查看对象有用的函数,id,type,print 查看对象身份.类型.值:记住以下两个命令id,type 两个对象的比较 以上三个分别是 身份比较.对象 ...

  7. Mybatis generator 自动生成代码(2)

    最近准备开始做一个项目,需要开始手动创建sql,于是将Mybatis generator 工具功能强化了下. 首先,这里引入到版本一点的包 <dependency> <groupId ...

  8. mysql 实现多列唯一性约束

    alter table j_assistants add constraint unique_name_course_class unique(name_id,course_id,class_id);

  9. 【进阶修炼】——改善C#程序质量(4)

    46, 显示释放资源,需要实现IDisposable接口. 最好按照微软建议的Dispose模式实现.实现了IDisposable接口后,在Using代码块中,垃圾会得到自动清理. 47, 即使提供了 ...

  10. 提高Java代码质量的Eclipse插件之Checkstyle的使用具体解释

    CheckStyle是SourceForge下的一个项目,提供了一个帮助JAVA开发者遵守某些编码规范的工具.它可以自己主动化代码规范检查过程.从而使得开发者从这项重要可是枯燥的任务中解脱出来. Ch ...