tree.cpp

来自「一个类c语言的解释器」· C++ 代码 · 共 64 行

CPP
64
字号
/*******************************************************
main.c
武汉大学国际软件学院软件工程05级7班
崔灿
200532580235
2007-11-10
********************************************************/
#include "stdafx.h"
#include "tree.h"
#include <string>
using namespace std;

void TreeNode::addChild(TreeNode *child){
	this->children->push_back(child);
}

TreeNode::TreeNode(){
	this->children = new vector<TreeNode*>;
	this->describe="";
}
TreeNode* build_tree(string s,int length){
	s = "\n"+s;
	TreeNode * t = new TreeNode;
	int begin=0, end = 0;
	string temp = "\n";
	for (int i=0;i<length;i++)
	{
		temp = temp+"  ";
	}
	temp = temp+"--";
	if (length!=0)
	{
		begin = s.find("\n  ",1);
		if (begin==string::npos)
		{
			t->describe = s.substr(0,s.length());
		}
		else{
			t->describe = s.substr(0,begin);
		}
	}
	begin = s.find(temp);
	end = s.find(temp,begin+1);
	if (begin ==string::npos)
	{
		return t;
	}
	while (end!=string::npos)
	{
		TreeNode* p = build_tree(s.substr(begin+length*2+3,end - (begin+length*2+3)),length+1);
		if (p !=NULL)
		{
			t->addChild(p);
		}
		begin = end;
		end = s.find(temp,begin+length*2+2);
	}
	TreeNode *q = build_tree(s.substr(begin+length*2+3,s.length() - (length*2+3)),length+1);
	if (q !=NULL)
	{
		t->addChild(q);
	}
	return t;
}

⌨️ 快捷键说明

复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?