0

我正在此代码中实现向导类,但在查找向导家谱中的关系时遇到了严重问题。我正在使用回溯方法来查找两个节点之间的所有关系,但内存将在任务中间开始混乱,我不知道该怎么办。在这里你可以看到这个类使用 C++ 的实现。

#include "Wizard.h"
#include <iostream>
#include <string>
#include <vector>

using namespace std;

Wizard::Wizard(string first_name, string surname, string occupation, string organization, string wand)
: first_name(first_name),surname(surname), occupation(occupation), organization(organization), wand(wand)
{
    this->married=0;
    this->wand="Dragon Hearstring";
    this->occupation="Auror";
    this->organization="Dumbledore Army";
};

void Wizard::set_first_name(string _name){
    this->first_name = _name;
}

void Wizard::set_surname(string _surname){
    this->surname = _surname;
}

void Wizard::set_occupation(string _occupation){
    this->occupation = _occupation;
}

void Wizard::set_organization(string _organization){
    this->organization = _organization;
}

void Wizard::set_wand(string _wand){
    this->wand = _wand;
}

string Wizard::get_name(){
    string name;
    name = first_name + " " + surname;
    return name;
}

string Wizard::get_occupation(){
    return occupation;
}

string Wizard::get_organization(){
    return organization;
}

string Wizard::get_wand(){
    return wand;
}

void Wizard::print_parents(){
    cerr << "Parents: ";
    for (int i=0; i<parents.size(); i++){
        cout << parents[i]->get_name();
        if ( i!= parents.size() -1){
            cout << " & ";
        }
    }
    cout << endl;
}

void Wizard::print_siblings(){
    cerr <<"Siblings: ";
    for (int i=0; i<siblings.size(); i++){
        cout << siblings[i]->get_name();
        if ( i < siblings.size() -1){
            cout << " & ";
        }
    }
    cout << endl;
}

void Wizard::print_spouse(){
    cerr <<"Spouse: ";
    if (married==1)
        cout << spouse->get_name();
    cout << endl;
}

void Wizard::print_children(){
    cerr <<"Children: ";
    for (int i=0; i<children.size(); i++){
        cout << children[i]->get_name();
        if ( i < children.size() - 1){
            cout << " & ";
        }
    }
    cout << endl;   
}

void Wizard::operator*(Wizard& _spouse){
    this->married=1;
    _spouse.married=1;
    cout << this->get_name() << " and " << _spouse.get_name() << endl;
    this->spouse = &_spouse;
    _spouse.spouse = this;
    _spouse.print_spouse();
    vector< Wizard* > these_two;
    these_two.push_back(this);
    these_two.push_back(&_spouse);
    for (int i=0; i<this->children.size(); i++)
        children[i]->parents = these_two;
    for (int i=0; i<_spouse.children.size(); i++)
        children[i]->parents = these_two;
}

void Wizard::operator+(Wizard& _child){
    if (!search_in_vector(this->children,&_child))
        this->children.push_back(&_child);
    if (!search_in_vector(this->spouse->children,&_child))
        this->spouse->children.push_back(&_child);
    vector< Wizard* > these_two;
    these_two.push_back(this);
    these_two.push_back(this->spouse);
    _child.parents=these_two;
    _child.surname = this->surname;
    for (int i=0; i<this->children.size(); i++){
        if(!search_in_vector(_child.siblings,children[i]) &&     (children[i]!=&_child)){
            _child.siblings.push_back(children[i]);
            children[i]->siblings.push_back(&_child);
        }
    }
}

    void Wizard::print_relation_with(Wizard& john_doe){
    vector<string> path2;
    vector< vector< string > > path1;
    vector<Wizard*> nodes_in_the_way;
    nodes_in_the_way.push_back(this);
    if (this->get_name() == john_doe.get_name()){
        cout << this->get_name() << " is " << john_doe.get_name() << endl;
        return;
    }
    this-   >search_for_relation(*this,john_doe,path1,path2,nodes_in_the_way);
    if (path1.size() == 0){
        cout << "no relation" << endl;
        return;
    }
    path2=path1[0];
    for (int i=1;i<path1.size();i++){
        if (path1[i].size() < path2.size())
            path2=path1[i];
    }
    cout << this->get_name() << " is ";
    for (int i=0; i<path2.size(); i++)
        cout << path2[i] << " of ";
    cout << john_doe.get_name() << endl;
}

void Wizard::search_for_relation(Wizard& a,Wizard& b,vector<       vector<string> > &path1,vector<string> &path2,vector< Wizard* >    &nodes_in_the_way){
    cerr << "////////////" << endl;
    cerr << "IM INSIDE " << a.get_name() << " , SEARCHING FOR " <<     b.get_name() << " - " << path2.size() << endl;
    cout << "PATH: ";
    for (int i=0;i<nodes_in_the_way.size();i++)
        cout << nodes_in_the_way[i]->get_name() << "  ";
    cout << endl;
    a.print_spouse();
    a.print_children();
    a.print_parents();
    if (a.get_name() == b.get_name() && path2.size()==0){
        return;
    }
    if (a.get_name() == b.get_name() ){
        path1.push_back(path2);
        return;
    }   
    if (a.married){
        if (!search_in_vector(nodes_in_the_way,a.spouse)){
            path2.push_back("spouse");
            nodes_in_the_way.push_back(a.spouse);
            search_for_relation(*    (a.spouse),b,path1,path2,nodes_in_the_way);
            path2.pop_back();
            nodes_in_the_way.pop_back();
        }       
    }
    // parents
    for (int i=0;i<a.parents.size();i++){
        if (!search_in_vector(nodes_in_the_way,a.parents[i])){
            path2.push_back("child");
            nodes_in_the_way.push_back(a.parents[i]);
            search_for_relation(*    (a.parents[i]),b,path1,path2,nodes_in_the_way);
            path2.pop_back();
            nodes_in_the_way.pop_back();
        }   
    }
    // children
    for (int i=0;i<a.children.size();i++){
        if (!search_in_vector(nodes_in_the_way,a.children[i])){
            path2.push_back("parent");
            nodes_in_the_way.push_back(a.children[i]);
            search_for_relation(*    (a.children[i]),b,path1,path2,nodes_in_the_way);
            path2.pop_back();
            nodes_in_the_way.pop_back();
        }   
    }
}

///////////// NON-METHOD FUNCITONS ///////////////

bool search_in_vector(vector< Wizard* > a,Wizard* b){
    for (int i=0; i<a.size(); i++)
        if (a[i]->get_name() == b->get_name())
            return true;
    return false;
}

这就是代码。在这里你可以看到一些 back_tracking 之后的输出:

输出

在这里您可以看到,Harry 的信息在上次递归期间被弄乱了,可执行文件无法打印他的父母。

请让我知道我做错了!谢谢!

4

0 回答 0