From fc747105077c7ebab15963c5e69e334357c675e7 Mon Sep 17 00:00:00 2001 From: Adrian Kummerländer Date: Sat, 5 Jan 2013 23:29:33 +0100 Subject: Made existing Graphviz tree generation available to the libary user by providing a new plain "exportTree" function --- src/parser.cc | 8 ++++++++ src/parser.h | 3 ++- src/tree.cc | 30 ++++++++++++++++++++++++------ 3 files changed, 34 insertions(+), 7 deletions(-) diff --git a/src/parser.cc b/src/parser.cc index 5989027..53eb311 100644 --- a/src/parser.cc +++ b/src/parser.cc @@ -1,6 +1,7 @@ #include "parser.h" #include "exceptions.h" +#include #include namespace SimpleParser { @@ -12,6 +13,13 @@ double calculate(std::string term) { return termTree.solve(); } +std::string exportTree(std::string term) { + Tree termTree; + termTree.setRoot(buildTree(&termTree, term)); + + return termTree.print(term); +} + namespace { int8_t getPriority(char tmp) { diff --git a/src/parser.h b/src/parser.h index 7e594ca..677717a 100644 --- a/src/parser.h +++ b/src/parser.h @@ -1,14 +1,15 @@ #ifndef PARSER_SRC_PARSER_H_ #define PARSER_SRC_PARSER_H_ +#include #include -#include #include "tree.h" namespace SimpleParser { double calculate(std::string); +std::string exportTree(std::string); namespace { int8_t getPriority(char); diff --git a/src/tree.cc b/src/tree.cc index 3755423..0fa41ad 100644 --- a/src/tree.cc +++ b/src/tree.cc @@ -38,22 +38,40 @@ std::string Tree::print(std::string term) { std::stringstream out; out.precision(std::numeric_limits::digits10); - out << "digraph \"" << term << "\"" << std::endl - << "{" << std::endl - << "node [shape = box];" << std::endl; + out << "digraph \"" + << term + << "\"" + << std::endl; + out << "{" << std::endl; + out << "node [shape = box];" << std::endl; int i = 0; for ( auto it = this->node_collection_.begin(); it != this->node_collection_.end(); ++it ) { - out << "node" << i << " [ label = \"" << (*it)->print() << "\"];" << std::endl; + out << "node" + << i + << " [ label = \"" + << (*it)->print() + << "\"];" + << std::endl; if ( (*it)->getType() == OPERATOR_NODE ) { for ( auto iter = this->node_collection_.begin(); iter != this->node_collection_.end(); ++iter ) { if ( (*iter).get() == (*it)->leftChild ) { - out << "\"node" << i << "\" -> \"node" << (iter - this->node_collection_.begin()) << "\";" << std::endl; + out << "\"node" + << i + << "\" -> \"node" + << (iter - this->node_collection_.begin()) + << "\";" + << std::endl; } if ( (*iter).get() == (*it)->rightChild ) { - out << "\"node" << i << "\" -> \"node" << (iter - this->node_collection_.begin()) << "\";" << std::endl; + out << "\"node" + << i + << "\" -> \"node" + << (iter - this->node_collection_.begin()) + << "\";" + << std::endl; } } } -- cgit v1.2.3