华为笔试题--LISP括号匹配 解析及源码实现
2017-11-25 11:56
495 查看
转载自点击打开链接,由于版本问题,我对某些调用的函数进行了修改
题目描述:
LISP语言唯一的语法就是括号要匹配。
形如(OP P1 P2 …),括号内元素由单个空格分割。
其中第一个元素OP为操作符,后续元素均为其参数,参数个数取决于操作符类型
注意:参数P1,P2也有可能是另外一个嵌套的(OP P1 P2 …)
其中OP类型为add/sub/mul/div(全小写),分别代表整数的加减乘除法
其中add/mul参数个数2或以上,sub/div参数个数为2
举例:
-输入:(mul 3 -7) 输出:-21
-输入:(add1 2 3) 输出:6
-输入:(sub (mul 2 4) (div 9 3)) 输出:5
-输入:(div 1 0) 输出:error
题目涉及数字均为整数,可能为负;不考虑32位溢出翻转
除零错误时,输出"error",除法遇除不尽,取整,即3/2=1
输入描述:
合法C字符串,字符串长度不超过512;用例保证了无语法错误
输出描述:
合法C字符串,字符包括'0'-'9'及负号'-'或者'error'
示例
输入 (add 1 2 3) 输出 6
*********************************************************
在linux终端输入g++ -std=c++11 lisp.C 进行编译
题目描述:
LISP语言唯一的语法就是括号要匹配。
形如(OP P1 P2 …),括号内元素由单个空格分割。
其中第一个元素OP为操作符,后续元素均为其参数,参数个数取决于操作符类型
注意:参数P1,P2也有可能是另外一个嵌套的(OP P1 P2 …)
其中OP类型为add/sub/mul/div(全小写),分别代表整数的加减乘除法
其中add/mul参数个数2或以上,sub/div参数个数为2
举例:
-输入:(mul 3 -7) 输出:-21
-输入:(add1 2 3) 输出:6
-输入:(sub (mul 2 4) (div 9 3)) 输出:5
-输入:(div 1 0) 输出:error
题目涉及数字均为整数,可能为负;不考虑32位溢出翻转
除零错误时,输出"error",除法遇除不尽,取整,即3/2=1
输入描述:
合法C字符串,字符串长度不超过512;用例保证了无语法错误
输出描述:
合法C字符串,字符包括'0'-'9'及负号'-'或者'error'
示例
输入 (add 1 2 3) 输出 6
*********************************************************
在linux终端输入g++ -std=c++11 lisp.C 进行编译
#include <iostream> #include <algorithm> #include <string> #include <stack> #include <vector> #include <cstdio> #include <cstdlib> #include <cstring> using namespace std; int main() { string str; getline(cin,str); bool error = false; stack<string> opstack; // operations stack<string> strstack; // divided strings for (int i = 0; i < str.size();) { if (str[i] == ' ') { i = i + 1; continue; } if (str[i] == '(') { strstack.push(string("(")); int spaceIndex = str.find(' ', i); string tmp = str.substr(i + 1, spaceIndex - i - 1); opstack.push(tmp); // operation i = spaceIndex; } else if (str[i] == ')') { string curOp = opstack.top(); opstack.pop(); vector<string> tmps; // strs temp while (strstack.top() != "(") { tmps.push_back(strstack.top()); strstack.pop(); } strstack.pop(); // add if (curOp == "add") { int temp = 0; for (int i = 0; i<=tmps.size() - 1; ++i) { temp += atoi(tmps[i].c_str()); } strstack.push(to_string(temp)); } // sub else if (curOp == "sub") { int temp = 0; if (tmps.size() > 0) temp = atoi(tmps[tmps.size() - 1].c_str()); for (int i = tmps.size() - 2; i >= 0; --i) { temp -= atoi(tmps[i].c_str()); } strstack.push(to_string(temp)); } // mul else if (curOp == "mul") { int temp = 0; if (tmps.size() > 0) temp = atoi(tmps[tmps.size() - 1].c_str()); for (int i = tmps.size() - 2; i >= 0; --i) { temp *= atoi(tmps[i].c_str()); } strstack.push(to_string(temp)); } // div else if (curOp == "div") { int temp = 0; if (tmps.size() > 0) temp = atoi(tmps[tmps.size() - 1].c_str()); for (int i = tmps.size() - 2; i >= 0; --i) { int data1 = atoi(tmps[i].c_str()); if (data1 == 0) { error = true; break; } else temp /= data1; } if (error) break; else strstack.push(to_string(temp)); } ++i; } else // substrs { // get substring by ' ' or ')' auto index = str.find(' ', i); auto index2= str.find(')', i); if (index < index2) { strstack.push(str.substr(i, index - i)); i = index; } else { strstack.push(str.substr(i, index2 - i)); i = index2; } } } if (error) cout << "error"<<endl; else cout << atoi(strstack.top().c_str())<<endl; return 0; }
相关文章推荐
- 华为笔试题--LISP括号匹配 解析及源码实现
- 括号匹配问题与经典笔试面试题目解析
- 括号匹配问题与经典笔试面试题目解析
- 华为-成研所笔试-java-括号匹配
- Lucene总结系列(三)--总述优化方案和呈现实时内存索引实现(结合RAMDirectory源码解析)
- 括号匹配问题——用栈实现
- 华为机试—括号匹配
- [绝冬城]Sniff C++实现源码解析
- 使用Android自带Gallery组件实现CoverFlow,源码+解析
- 利用RobHess源码实现SIFT算法及RANSAC去错的图像特征提取匹配及去除错匹配
- Kafka源码深度解析-序列7 -Consumer -coordinator协议与heartbeat实现原理
- Spring 源码深入解析(1)之bean容器的基本实现(二)
- 【升级版】如何使用阿里云云解析API实现动态域名解析,搭建私有服务器【含可执行文件和源码】
- OpenCV中CascadeClassifier类实现多尺度检测源码解析
- c语言栈实现括号匹配
- jsp基于XML实现用户登录与注册的实例解析(附源码)
- java实现用栈解决一个串中的括号是否匹配
- Java集合-HashMap源码实现深入解析
- 用栈实现括号匹配
- epoll实现细节——源码解析