Basic Calculator II

题目

思路

和这个一样:Basic Calculator I

代码

class ExpressionTransformation {
public:
string trans_to_postfix_expression_to_s(string); // 将得到的表达式转化为后缀表达式
long long int calculate_from_postfix_expression(); // 依据后缀表达式计算值 private:
vector<string> ans_vector_post; // 存放后缀表达式
string post_string; // 存放后缀表达式
}; inline int prior(char op) { // 计算优先级函数
if (op == '+' || op == '-') {
return 1;
}
else if (op == '*' || op == '/' || op == '%') {
return 2;
}
else {
return 0;
}
} long long int string_to_int(string in) { // 将输入的字符串转化为对应数字函数
char s[50];
for (int i = 0; i < 50; i++) {
s[i] = '\0';
}
for (int i = 0; i < in.size(); i++) {
s[i] = in[i];
}
long long int ans;
sscanf(s, "%lld", &ans);
return ans;
} string deleteBlank(string in) {
string ans;
int size = in.size();
for (int i = 0; i < size; i++) {
if (in[i] != ' ') ans.push_back(in[i]);
}
return ans;
} string ExpressionTransformation::trans_to_postfix_expression_to_s(string in) { stack<char> op; // 操作符栈
ans_vector_post.clear(); // 后缀表达式存放数组
for (int i = 0; i < in.size();) {
char c = in[i];
if ('0' <= c && c <= '9') { // 是数字直接插入
string num;
int j;
for (j = i; j < in.size() && '0' <= in[j] && in[j] <= '9'; j++) {
num.push_back(in[j]);
}
ans_vector_post.push_back(num);
i = j;
}
else {
if (c == '(') { // 是开括号直接插入
op.push('(');
}
else {
if (c == ')') { // 是闭括号就把原本栈中的运算符都输出,直到遇到开括号,注意开括号要丢弃
while (op.top() != '(') {
string temp;
temp.push_back(op.top());
ans_vector_post.push_back(temp);
op.pop();
}
op.pop();
}
else { // 假如是加减乘除取余
if (op.empty()) { // 操作符栈是空就直接插入
op.push(c);
}
else { // 假设扫描到的运算符优先级高于栈顶运算符则,把运算符压入栈。否则的话,就依次把栈中运算符弹出加到数组ans的末尾,直到遇到优先级低于扫描到的运算符或栈空。而且把扫描到的运算符压入栈中
if (prior(c) > prior(op.top())) {
op.push(c);
}
else {
while (!op.empty() && prior(c) <= prior(op.top())) {
string temp;
temp.push_back(op.top());
ans_vector_post.push_back(temp);
op.pop();
}
op.push(c);
}
}
}
}
i++;
}
}
while (!op.empty()) { // 注意把操作符栈中的剩余操作符输出
string temp;
temp.push_back(op.top());
ans_vector_post.push_back(temp);
op.pop();
} post_string.clear(); // 构造string并返回
for (int i = 0; i < ans_vector_post.size(); i++) {
post_string += ans_vector_post[i];
} return post_string;
} long long int ExpressionTransformation::calculate_from_postfix_expression() { //利用栈对后缀表达式求值。直接从后缀表达式的左往右扫描。遇到数字放入栈中,遇到字符就把栈顶的两个数字拿出来算。然后再放进栈 stack<long long int> ans_post;
for (int i = 0; i < ans_vector_post.size(); i++) {
long long int x, y;
if ('0' <= ans_vector_post[i][0] && ans_vector_post[i][0] <= '9') {
ans_post.push(string_to_int(ans_vector_post[i]));
}
else {
y = ans_post.top(); // 注意顺序,这里好比xy+就是x+y
ans_post.pop();
x = ans_post.top();
ans_post.pop();
if (ans_vector_post[i][0] == '+') {
ans_post.push(x + y);
}
else if (ans_vector_post[i][0] == '-') {
ans_post.push(x - y);
}
else if (ans_vector_post[i][0] == '*') {
ans_post.push(x * y);
}
else if (ans_vector_post[i][0] == '/') {
ans_post.push(x / y);
}
else {
ans_post.push(x % y);
}
}
}
return ans_post.top();
} class Solution {
public:
int calculate(string s) {
ExpressionTransformation e;
s = deleteBlank(s);
e.trans_to_postfix_expression_to_s(s);
int postAns = e.calculate_from_postfix_expression();
return postAns;
}
};

LeetCode OJ Basic Calculator II的更多相关文章

  1. [LeetCode] 227. Basic Calculator II 基本计算器 II

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

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

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

  3. LeetCode#227.Basic Calculator II

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

  4. Java for LeetCode 227 Basic Calculator II

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

  5. (medium)LeetCode 227.Basic Calculator II

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

  6. [LeetCode] 224. Basic Calculator 基本计算器

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

  7. LeetCode 227. 基本计算器 II(Basic Calculator II)

    227. 基本计算器 II 227. Basic Calculator II 题目描述 实现一个基本的计算器来计算一个简单的字符串表达式的值. 字符串表达式仅包含非负整数,+,-,*,/ 四种运算符和 ...

  8. 【LeetCode】227. Basic Calculator II 解题报告(Python)

    [LeetCode]227. Basic Calculator II 解题报告(Python) 标签(空格分隔): LeetCode 作者: 负雪明烛 id: fuxuemingzhu 个人博客: h ...

  9. leetcode 224. Basic Calculator 、227. Basic Calculator II

    这种题都要设置一个符号位的变量 224. Basic Calculator 设置数值和符号两个变量,遇到左括号将数值和符号加进栈中 class Solution { public: int calcu ...

随机推荐

  1. caioj 1082 动态规划入门(非常规DP6:火车票)

    f[i]表示从起点到第i个车站的最小费用 f[i] = min(f[j] + dist(i, j)), j < i 动规中设置起点为0,其他为正无穷 (貌似不用开long long也可以) #i ...

  2. Java中四种引用的区分

    强引用(StrongReference) 强引用就是指在程序代码之中普遍存在的,比如下面这段代码中的object和str都是强引用: 1 2 Object object = new Object(); ...

  3. POJ1833 &amp; POJ3187 &amp; POJ3785 next_permutation应用

    要是没有next_permutation这个函数,这些题认为还不算特别水,只是也不一定,那样可能就会有对应的模板了. 反正正是由于next_permutation这个函数.这些题包含之前的POJ122 ...

  4. Codeforces Round #337 (Div. 2) 610C Harmony Analysis(脑洞)

    C. Harmony Analysis time limit per test 3 seconds memory limit per test 256 megabytes input standard ...

  5. 在Mac OS X上安装使用lazarus 1.6.4

    一直觉得delphi的OO做得比C++还完善, 但如今日落西真是让人感到唏嘘,  这并不意味着delphi比C++差, 它的创始人被微软挖墙脚后创造了C#系列开发工具, 设计理念大部分与Delphi相 ...

  6. 技术报告:APT组织Wekby利用DNS请求作为C&C设施,攻击美国秘密机构

    技术报告:APT组织Wekby利用DNS请求作为C&C设施,攻击美国秘密机构 最近几周Paloalto Networks的研究人员注意到,APT组织Wekby对美国的部分秘密机构展开了一次攻击 ...

  7. nodejs-website

    http://docs.nodejitsu.com/articles/file-system/how-to-read-files-in-nodejs http://nodeapi.ucdok.com/ ...

  8. Linux常用Office办公软件

    1.WPS Office是由金山软件股份有限公司自主研发的一款办公软件套件,可以实现办公最常用的文字.表格.演示等多种功能.免费提供海量的在线存储空间及文档模板.支持阅读和输出PDF文件.全面兼容Mi ...

  9. vue-cli · Failed to download repo vuejs-templates/webpack-simple: tunneling socket could not be established, cause=connect ECONNREFUSED 127.0.0.1:8086 && vue init webpack-simple xxx

    vue init webpack-simple mywork报错如下: vue-cli · Failed to download repo vuejs-templates/webpack-simple ...

  10. php自定义加密和解密

    <?php function _authcode($string, $operation = 'DECODE', $expiry = 0) { $key = 'c5s1t6o';    $cke ...