227. Basic Calculator II
2016-06-27 15:39
281 查看
Implement a basic calculator to evaluate a simple expression string.
The expression string contains only non-negative integers,
and empty spaces . The integer division should truncate toward zero.
You may assume that the given expression is always valid.
Some examples:
Note: Do not use the
library function.
题意:实现简单的运算器,处理整数的加减乘除操作。
思路:用数字栈和符号栈进行运算操作。
class Solution {
public:
int calculate(string s) {
stack<int> numStack;
stack<char> operatorStack;
int p1 = 0;
int p2 = 0;
const int size = s.size();
while (p2 < size){
if (s[p2] == '+' || s[p2] == '-' || s[p2] == '*' || s[p2] == '/'){
numStack.push(stringToInt(s.substr(p1, p2 - p1)));
if (operatorStack.empty()){
operatorStack.push(s[p2]);
}
else{
switch (s[p2]){
int a;
int b;
int c;
case '+':
case '-':
while (!operatorStack.empty()){
b = numStack.top();
numStack.pop();
a = numStack.top();
numStack.pop();
char ch = operatorStack.top();
operatorStack.pop();
if (ch == '+')
c = a + b;
else if (ch == '-')
c = a - b;
else if (ch == '*')
c = a*b;
else
c = a / b;
numStack.push(c);
}
operatorStack.push(s[p2]);
break;
case '*':
case '/':
char ch = operatorStack.top();
if (ch == '+' || ch == '-'){
operatorStack.push(s[p2]);
break;
}
else{
b = numStack.top();
numStack.pop();
a = numStack.top();
numStack.pop();
operatorStack.pop();
if (ch == '*')
c = a*b;
if (ch == '/')
c = a / b;
numStack.push(c);
operatorStack.push(s[p2]);
break;
}
}
}
p2++;
p1 = p2;
}
else{
p2++;
}
}
numStack.push(stringToInt(s.substr(p1, p2 - p1)));
while (!operatorStack.empty()){
int b = numStack.top();
numStack.pop();
int a = numStack.top();
numStack.pop();
char ch = operatorStack.top();
operatorStack.pop();
int c;
switch (ch){
case '+':
c = a + b;
break;
case '-':
c = a - b;
break;
case '*':
c = a * b;
break;
case '/':
c = a / b;
break;
}
numStack.push(c);
}
return numStack.top();
}
private:
int stringToInt(string s){
int a;
stringstream ss;
ss << s;
ss >> a;
return a;
}
};
The expression string contains only non-negative integers,
+,
-,
*,
/operators
and empty spaces . The integer division should truncate toward zero.
You may assume that the given expression is always valid.
Some examples:
"3+2*2" = 7 " 3/2 " = 1 " 3+5 / 2 " = 5
Note: Do not use the
evalbuilt-in
library function.
题意:实现简单的运算器,处理整数的加减乘除操作。
思路:用数字栈和符号栈进行运算操作。
class Solution {
public:
int calculate(string s) {
stack<int> numStack;
stack<char> operatorStack;
int p1 = 0;
int p2 = 0;
const int size = s.size();
while (p2 < size){
if (s[p2] == '+' || s[p2] == '-' || s[p2] == '*' || s[p2] == '/'){
numStack.push(stringToInt(s.substr(p1, p2 - p1)));
if (operatorStack.empty()){
operatorStack.push(s[p2]);
}
else{
switch (s[p2]){
int a;
int b;
int c;
case '+':
case '-':
while (!operatorStack.empty()){
b = numStack.top();
numStack.pop();
a = numStack.top();
numStack.pop();
char ch = operatorStack.top();
operatorStack.pop();
if (ch == '+')
c = a + b;
else if (ch == '-')
c = a - b;
else if (ch == '*')
c = a*b;
else
c = a / b;
numStack.push(c);
}
operatorStack.push(s[p2]);
break;
case '*':
case '/':
char ch = operatorStack.top();
if (ch == '+' || ch == '-'){
operatorStack.push(s[p2]);
break;
}
else{
b = numStack.top();
numStack.pop();
a = numStack.top();
numStack.pop();
operatorStack.pop();
if (ch == '*')
c = a*b;
if (ch == '/')
c = a / b;
numStack.push(c);
operatorStack.push(s[p2]);
break;
}
}
}
p2++;
p1 = p2;
}
else{
p2++;
}
}
numStack.push(stringToInt(s.substr(p1, p2 - p1)));
while (!operatorStack.empty()){
int b = numStack.top();
numStack.pop();
int a = numStack.top();
numStack.pop();
char ch = operatorStack.top();
operatorStack.pop();
int c;
switch (ch){
case '+':
c = a + b;
break;
case '-':
c = a - b;
break;
case '*':
c = a * b;
break;
case '/':
c = a / b;
break;
}
numStack.push(c);
}
return numStack.top();
}
private:
int stringToInt(string s){
int a;
stringstream ss;
ss << s;
ss >> a;
return a;
}
};
相关文章推荐
- addEventListener事件
- C++ 虚函数表解析
- P2P的逾期、坏账与违约
- HDU1536&&POJ2960 S-Nim(SG函数博弈)
- 论文笔记之:Deep Reinforcement Learning with Double Q-learning
- JAVA 后台全局异常控制
- 反转字符串
- ACL权限
- fzu 2090 旅行社的烦恼【Floyd求最小环】
- HttpClient 4 和 HttpClient 3 设置超时
- c++ 检查一个字符串中是否有汉字
- 使用AppMonitor软件在PC上对手机APP实时抓包分析
- Tomcat环境变量如何配置
- CacheConcurrencyStrategy五种缓存方式
- 学习 Java 布局 groupLayout
- C语言一些容易忘得知识点汇总
- 05.Shell乾坤大挪移
- String放入运行时常量池的时机与String.intern方法解惑
- 十步图解CSS的position
- lease 脑裂