dot.cpp

来自「我的红黑树的c++实现。主要特点是可以用dot工具把红黑树画出来」· C++ 代码 · 共 104 行

CPP
104
字号
// file dot.cpp
//created by alpha 2008.11.3

#include "../headers/dot.h"
#include "../headers/control.h"
#include <fstream>
#include <iostream>
#include <iterator>

dot::dot()
{
	lt = new list<triple *>;
}

void dot::dotInsert(string *from, string *to, string info)
{
	string *newinfo = new string();
	if(!info.empty())
	{
		string *s1 = new string("[label = \"");
		string *s2 = new string("\"]");
		*newinfo = *s1 + info + *s2;
	}
	else ;
	triple *t = new triple(from, to, newinfo);
	lt->push_back(t);
	return;
}

void dot::dotInsert(string *s, string color)  //vertex s is color is black or white
{
	string s1("[color = ");
	string s2("]");
	string *newinfo = new string();
	*newinfo = s1 + color;
	if(s->substr(0,3) == "NIL")
		*newinfo += ",shape = box ,hight = 0.01, width=0.02";
	*newinfo += s2;
	string *to = new string();
	triple *t = new triple(s, to, newinfo);
	lt->push_back(t);
	return;
}

void dot::dotInsert(string *from, string *to, string *info)
{
	string *newinfo = new string();
	if(!(*info).empty())
	{
		string *s1 = new string("[label = \"");
		string *s2 = new string("\"]");
		*newinfo = *s1 + *info + *s2;
	}
	else ;
	triple *t = new triple(from, to, newinfo);
	lt->push_back(t);
	return;     
}
string *dot::dotToString()
{
	list<triple *>::iterator iter = lt->begin();
	string *s = new string("\n\n");
	if (lt->empty())
	{
		return new string();
	}
	string *temp1,*temp2,*temp3,temp4("\""),temp5("->"),temp6(";\n");
	while (iter != lt->end())
	{
		temp1 = (string *)((*iter)->getFirst());
		temp2 = (string *)((*iter)->getSecond());
		temp3 = (string *)((*iter)->getThird());
		if( *temp2 != "")
			*s += temp4 + *temp1 + temp4 + temp5 + temp4 + *temp2 + temp4 + *temp3 + temp6;
		else
			*s += *temp1 + *temp3 + temp6;
		iter++;
	}
	return s;
}
void dot::dotToJpg(string fname)
{
	string dotfname = fname + ".dot";
	string jpgfname = fname + ".jpg";
	ofstream f(dotfname.c_str());
	if (f)
	{ 
		string all = "digraph g{\n\tsize = \"10, 10\";\n\tnode [fontcolor=white,color=lightblue2, style=filled];\n";
		string *strdot = dotToString();
		all += *strdot + "}\n\n";
		f<<all;
		f.close();
		string commond = "dot\\dot -Tjpg -o " + jpgfname +" "+ dotfname;
		system(commond.c_str());
	}
	else	cout<<"file open fault!"<<endl;
	if (!controlKeepDot)
	{
		string s = "del " + dotfname;
		system(s.c_str());
	}
	return;
}

⌨️ 快捷键说明

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