solution.md 3.5 KB
Newer Older
ToTensor's avatar
ToTensor 已提交
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149
# 基本计算器

<p>给你一个字符串表达式 <code>s</code> ,请你实现一个基本计算器来计算并返回它的值。</p>

<p> </p>

<p><strong>示例 1:</strong></p>

<pre>
<strong>输入:</strong>s = "1 + 1"
<strong>输出:</strong>2
</pre>

<p><strong>示例 2:</strong></p>

<pre>
<strong>输入:</strong>s = " 2-1 + 2 "
<strong>输出:</strong>3
</pre>

<p><strong>示例 3:</strong></p>

<pre>
<strong>输入:</strong>s = "(1+(4+5+2)-3)+(6+8)"
<strong>输出:</strong>23
</pre>

<p> </p>

<p><strong>提示:</strong></p>

<ul>
	<li><code>1 <= s.length <= * 10<sup>5</sup></code></li>
	<li><code>s</code> 由数字、<code>'+'</code><code>'-'</code><code>'('</code><code>')'</code>、和 <code>' '</code> 组成</li>
	<li><code>s</code> 表示一个有效的表达式</li>
</ul>


## template

```cpp
#include <bits/stdc++.h>
using namespace std;

class Solution
{
public:
    int calculate(string s)
    {
        stack<int> myStack;
        stack<char> myOperator;
        int i;
        for (i = 0; i < s.length(); i++)
        {
            while (i < s.length() && s[i] == ' ')
                i++;
            if (i == s.length())
                break;
            if (s[i] == '+' || s[i] == '-' || s[i] == '(')
                myOperator.push(s[i]);
            else if (s[i] == ')')
            {
                while (myOperator.top() != '(')
                {
                    int element1 = myStack.top();
                    myStack.pop();
                    int element2 = myStack.top();
                    myStack.pop();
                    char op = myOperator.top();
                    myOperator.pop();
                    if (op == '+')
                        myStack.push(element1 + element2);
                    else if (op == '-')
                        myStack.push(element2 - element1);
                }
                if (!myOperator.empty())
                    myOperator.pop();
                while (!myOperator.empty() && (myOperator.top() != '('))
                {
                    int element1 = myStack.top();
                    myStack.pop();
                    int element2 = myStack.top();
                    myStack.pop();
                    char op = myOperator.top();
                    myOperator.pop();
                    if (op == '+')
                        myStack.push(element1 + element2);
                    else if (op == '-')
                        myStack.push(element2 - element1);
                }
            }
            else
            {

                long long int number = 0;
                int j = i;
                while (j < s.length() && (s[j] - '0' <= 9) && (s[j] - '0' >= 0))
                {
                    number = number * 10 + (s[j] - '0');
                    j++;
                }
                i = j - 1;
                myStack.push(number);
                while (!myOperator.empty() && (myOperator.top() != '('))
                {
                    int element1 = myStack.top();
                    myStack.pop();
                    int element2 = myStack.top();
                    myStack.pop();
                    char op = myOperator.top();
                    myOperator.pop();
                    if (op == '+')
                        myStack.push(element1 + element2);
                    else if (op == '-')
                        myStack.push(element2 - element1);
                }
            }
        }
        return myStack.top();
    }
};

```

## 答案

```cpp

```

## 选项

### A

```cpp

```

### B

```cpp

```

### C

```cpp

```