活跃农民
- 积分
- 431
- 大米
- 颗
- 鳄梨
- 个
- 水井
- 尺
- 蓝莓
- 颗
- 萝卜
- 根
- 小米
- 粒
- 学分
- 个
- 注册时间
- 2017-7-28
- 最后登录
- 1970-1-1
|
第三题 recursive descend parser
- #include <iostream>
- #include <vector>
- #include <queue>
- using namespace std;
- struct TreeNode {
- string tag;
- string text;
- vector<TreeNode*> children;
- TreeNode(string tag_, string text_) {
- tag = tag_;
- text = text_;
- }
- };
- TreeNode* parse(string& html, int& i) {
- if(html[i] == '[') {
- i++; // skip '['
- int j = html.find(']', i);
- string tag = html.substr(i, j-i);
- i = j + 1;
-
- int start = i;
- TreeNode* root = new TreeNode(tag, "");
- while(html.substr(i,2) != "[/") {
- auto child = parse(html, i);
- if(child) root->children.push_back(child);
- }
- root->text = html.substr(start, i-start);
- i += 2;
- i = html.find(']', i);
- i++;// skip ']'
- return root;
- }
- else {
- while(i < html.length() && html[i] != '[') i++; // scan all text content
- return nullptr;
- }
- }
- TreeNode* parseHtml(string& html) {
- TreeNode* root = new TreeNode("","");
- for(int i = 0; i < html.length(); ) {
- auto tmp = parse(html, i);
- if(tmp) root->children.push_back(tmp);
- }
- return root;
- }
- void printTree(TreeNode* root) {
- queue<TreeNode*> q;
- q.push(root);
- while(!q.empty()) {
- int qsize = q.size();
- while(qsize-- > 0) {
- auto top = q.front();
- q.pop();
- cout << "tag:" << top->tag << " text:" << top->text << " ";
- for(auto c : top->children) q.push(c);
- }
- cout << endl;
- }
- }
- int main(int argc, const char * argv[]) {
- // insert code here...
- string html = "[text] Hello [/text] [bold]![/bold] [mark]word[/mark]";
- printTree(parseHtml(html));
- cout << endl;
- html = "[text] Hello [/text] [bold]abc[tiny] maketiny [/tiny]123[/bold] [mark]word[/mark]";
- printTree(parseHtml(html));
- return 0;
- }
复制代码 |
|