⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 tree.cpp

📁 一个类c语言的解释器
💻 CPP
字号:
/*******************************************************
Tree.cpp
武汉大学国际软件学院软件工程05级7班
崔灿
200532580235
2007-11-10
********************************************************/
#include "tree.h"
#include "globals.h"
#include <string>
using namespace std;

TreeNode::TreeNode(_TokenType type, TreeNode *parent){
	this->type=type;
	this->parent=parent;
	this->children = new vector<TreeNode*>;
	this->describe="";
}

Tree::Tree(TreeNode *root){
	this->root=root;
}

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

string TreeNode::toString(int offset){
	string s;
	for (int i=0;i<offset;i++)
	{
		s = s + "  ";
	}
	s = s+"--";
	s = s+this->describe;
	s = s+ "\n";
	return s;
}

void toString(TreeNode *root,int length,string &out){
	int size = root->children->size();
	out = out+root->toString(length);
	for (int i=0;i<size;i++)
	{
		toString(root->children->at(i),length+1,out);
	}
}

TreeNode::TreeNode(){
	this->children = new vector<TreeNode*>;
	this->describe="";
}
TreeNode::TreeNode(std::string s){

}

TreeNode::TreeNode(TreeNode* t){
	this->children = new vector<TreeNode*>;
	this->describe="";
	this->describe = t->describe;
	for (int i=0;i<t->children->size();i++)
	{
		this->addChild(t->children->at(i));
	}
	this->type = t->type;
}

TreeNode* build_tree(string s,int length){
	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 + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -