# 基本计算器

<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 <= 3 * 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

```java
class Solution {
	public int calculate(String s) {
		int res = 0, num = 0;
		Stack<Integer> sta = new Stack<Integer>();
		char preSign = '+';
		int len = s.length();

		for (int i = 0; i < len; i++) {
			char ch = s.charAt(i);

			if (Character.isDigit(ch)) {
				num = num * 10 + ch - '0';
			}

			if ((!Character.isDigit(ch) && ch != ' ') || i == len - 1) {
				if (preSign == '+') {
					sta.push(num);
				} else if (preSign == '-') {
					sta.push(-1 * num);
				} else if (preSign == '*') {
					sta.push(num * sta.pop());
				} else if (preSign == '/') {
					sta.push(sta.pop() / num);
				}

				num = 0;
				preSign = ch;
			}
		}

		while (!sta.isEmpty()) {
			res += sta.pop();
		}

		return res;

	}
}


```

## 答案

```java

```

## 选项

### A

```java

```

### B

```java

```

### C

```java

```