-
-
Save senapk/ae3410bff0ee27e4996de16038abdbbf to your computer and use it in GitHub Desktop.
Arvore de decisão gráfica
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#include <iostream> | |
#include <vector> | |
using namespace std; | |
namespace aluno{ | |
struct DcNode{ | |
string key; //resposta | |
string value;//pergunta ou afirmacao | |
vector<DcNode*> filhos; | |
DcNode(string key = "", string value = ""){ | |
this->key = key; | |
this->value = value; | |
} | |
}; | |
struct DnTree{ | |
DcNode * root; | |
DnTree(){ | |
root = new DcNode("", "vazio"); | |
} | |
~DnTree(){ | |
clear(root); | |
root = nullptr; | |
} | |
void clear(DcNode * node){ | |
for(auto filho : node->filhos) | |
clear(filho); | |
delete node; | |
} | |
void _show(DcNode * node, string heranca, int num, DcNode * ref){ | |
char focus = (node == ref)?'>':' '; | |
cout << focus; | |
for(auto c : heranca.substr(0, heranca.size() - 1)) | |
cout << ((c == 'f')?" ":"│") << string(3, focus); | |
if(node != root){ | |
if(heranca.back() == 'f')//no do fim | |
cout << "└───" << num << ": " << node->key << "; " ; | |
else//no do meio | |
cout << "├───" << num << ": " << node->key << "; " ; | |
}cout << node->value << endl; | |
if(node->filhos.empty()) | |
return; | |
int qtd = node->filhos.size(); | |
for(int i = 0; i < qtd - 1; i++){ | |
_show(node->filhos[i], heranca + "m", i, ref); | |
} | |
_show(node->filhos.back(), heranca + "f", qtd - 1, ref); | |
} | |
void alterar(DcNode * node){ | |
while(true){ | |
_show(root, "", 0, node); | |
cout << "add _texto; back; go _num; key _k; value _v;" << endl; | |
string op; | |
cin >> op; | |
if(op == "add"){ | |
string texto; | |
getline(cin, texto); | |
node->filhos.push_back(new DcNode(texto.substr(1), "vazio")); | |
} | |
if(op == "go"){ | |
int num; | |
cin >> num; | |
alterar(node->filhos[num]); | |
} | |
if(op == "back"){ | |
return; | |
} | |
if(op == "key"){ | |
string texto; | |
getline(cin, texto); | |
node->key = texto.substr(1); | |
} | |
if(op == "value"){ | |
string texto; | |
getline(cin, texto); | |
node->value = texto.substr(1); | |
} | |
} | |
} | |
}; | |
} //namespace aluno | |
int main(){ | |
aluno::DnTree tree; | |
tree.alterar(tree.root); | |
return 0; | |
} | |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#include <iostream> | |
#include <fstream> | |
using namespace std; | |
void show(ostream& os = std::cout){ | |
os << "coisa importante vou salvar" << endl; | |
} | |
int main() | |
{ | |
string path = "../data.txt"; | |
ifstream iarq(path); | |
if(!iarq){ | |
cerr << "nao achei o arquivo"; | |
return 0; | |
} | |
string line; | |
while(getline(iarq, line)){ | |
cout << line << endl; | |
} | |
iarq.close(); | |
// ofstream oarq(path); | |
// arq.close(); | |
//show(arq); | |
return 0; | |
} | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment