1

我正在使用 VS 代码在 C++ 中进行编码。当我尝试调试代码并设置断点时,就会出现问题。局部变量窗格根本不显示任何变量。有时它确实显示了一些变量,但不是全部。

在此处输入图像描述

此外,当我尝试关闭调试器并单击停止按钮时,它不会停止。它需要我多次单击它,我认为这意味着打开了多个调试器或类似的东西。

要重现此问题,请将此文本另存为input_static.txt.

T1    1 2 5
T2    2 4
T3    2 3
T4    1 2 4
T5    1 3
T6    2 3
T7    1 3
T8    1 2 3 5
T9    1 2 3

并通过在第 201 行设置断点来调试以下代码。

#include <bits/stdc++.h>
using namespace std;

ifstream fin;
ofstream fout;

typedef struct fptnode
{
    int item, count;
    fptnode *next;
    map<int, fptnode *> children;
    fptnode *parent;
    fptnode(int item, int count, fptnode *parent)
    {
        this->item = item;
        this->count = count;
        this->parent = parent;
    }

} * FPTPTR;

map<int, int> frequency;

bool isMoreFrequent(int a, int b)
{
    if (frequency[a] == frequency[b])
        return a < b;

    return frequency[a] > frequency[b];
}

class FPTREE
{
public:
    FPTPTR root;
    map<int, list<FPTPTR>> headers;
    // map<int, int> frequency;

    FPTREE()
    {
        this->root = new fptnode(-1, 0, NULL);
    }

    // class isMoreFrequent
    // {
    // public:
    //     FPTREE *T;
    //     isMoreFrequent(FPTREE *T)
    //     {
    //         this->T = T;
    //     }

    //     bool operator()(int item1, int item2)
    //     {
    //         return T->frequency[item1] > T->frequency[item2];
    //     }
    // };

    FPTPTR getNewNode(int item, int count, fptnode *parent)
    {
        FPTPTR T = new fptnode(item, count, parent);
        this->headers[item].push_back(T);

        return T;
    }

    void add(vector<int> &transaction)
    {
        stack<int> S;
        std::sort(transaction.begin(), transaction.end(), isMoreFrequent);

        for (int i = transaction.size() - 1; i >= 0; i--)
        {
            S.push(transaction[i]);
        }
        insert(root, S);
    }

    int insert(FPTPTR T, stack<int> &S)
    {
        T->count++;
        if (S.empty())
            return 0;

        int top = S.top();
        S.pop();
        if (T->children[top] == NULL)
        {
            T->children[top] = getNewNode(top, 0, T);
        }

        insert(T->children[top], S);

        return 0;
    }

    void printPreOrder(ofstream &fout)
    {
        printPreOrder(fout, this->root);
    }
    void printPreOrder(ofstream &fout, FPTPTR T)
    {
        if (T)
        {
            fout << "[" << T->item << ", " << T->count << "]" << ' ';
            for (auto p : T->children)
            {
                printPreOrder(fout, p.second);
            }
        }
    }

    void printTree(ofstream &fout)
    {
        printTree(fout, this->root);
    }
    void printTree(ofstream &fout, FPTPTR T, int level = 0)
    {
        if (T)
        {
            for (int i = 0; i < level; i++)
                fout << "\t";

            fout << "[" << T->item << ", " << T->count << "]" << endl;
            for (auto p : T->children)
            {
                printTree(fout, p.second, level + 1);
            }
        }
    }

    void generatePath(FPTPTR node, vector<int> &path)
    {
        if (node && node->item >= 0)
        {
            path.push_back(node->item);
            generatePath(node->parent, path);
        }
    }

    FPTREE newTree(int item)
    {
        list<FPTPTR> &nodes = this->headers[item];

        vector<int> patternBase;

        FPTREE f;

        for (auto node : nodes)
        {
            patternBase.clear();
            generatePath(node->parent, patternBase);

            for (int j = 0; j < node->count; ++j)
                f.add(patternBase);
        }

        return f;
    }

    int clear()
    {
        return this->clear(this->root);
    }

    int clear(FPTPTR T)
    {
        for (auto p : T->children)
        {
            clear(p.second);
        }
        return 0;
    }

    bool isEmpty()
    {
        // return this->root->count == 0;
        return this->root->children.empty();
    }

} F;

ofstream tempout;

map<set<int>, int> mine(FPTREE f, int r = -1)
{
    map<set<int>, int> M;
    if (!f.isEmpty())
    {
        // if (f.root->children.empty())
        //     M[{}] += f.root->count;

        tempout << "\nOn removing " << r << ":\n";
        f.printTree(tempout);

        for (auto p : frequency)
        {
            FPTREE subF = f.newTree(p.first);

            map<set<int>, int> m = mine(subF, p.first);
            for (auto q : m)
            {
                auto itemset = q.first;
                itemset.insert(p.first);
                M[itemset] += q.second;
            }
            subF.clear();
        }
        return M;
    }
    // tempout << "\nTerminated.\n";
    return {};
}

int main(int argc, char const *argv[])
{
    fin.open("input_static.txt");
    fout.open("output_static.txt");
    tempout.open("temp");
    string str, s;
    while (fin >> s)
    {
        if (s.front() != 'T')
        {
            frequency[stoi(s)]++;
        }
    }

    vector<int> transaction;
    stringstream st;

    fin.clear();
    fin.seekg(0);
    while (std::getline(fin, str))
    {
        st.clear();
        st.str(str);
        transaction.clear();

        while (st >> s)
        {
            if (s.front() != 'T')
            {
                transaction.push_back(stoi(s));
            }
        }

        F.add(transaction);
    }

    fout << "Preorder:\n";
    F.printPreOrder(fout);
    fout << endl
         << endl;

    fout << "Tree in directory form:\n";

    F.printTree(fout);

    // printPrefixes(5);

    map<set<int>, int> frequentItemsets = mine(F);
    fout << endl;

    for (auto p : frequentItemsets)
    {
        fout << "Frequency=" << p.second << "\t";
        for (int item : p.first)
        {
            fout << item << ' ';
        }

        fout << endl;
    }

    // for (int i = 1; i <= 5; ++i)
    // {
    //     fout << i << ":\n";
    //     FPTREE f = F.newTree(i);
    //     f.printTree();
    // }

    // F.newTree(1).newTree(2).printTree(fout);

    return 0;
}

如果有帮助,这是一个生成和挖掘 FP-tree 以在事务数据库中查找频繁项集的程序。

4

0 回答 0