我正在編程一個雙向鏈表,其中我從給定文件中讀取字符串。 因此,我編寫了一個名爲Node的類,其中存儲了一個字符串(讀取字)以及字長和其他參數的一些整數。計數列表元素
當我讀取所有字符串形成文件後,我打開第二個文件,並再次讀出每個單詞,並將該單詞與鏈接列表中的字符串進行比較。之後,我將每個找到的單詞存儲在結果文件中。
現在我想顯示找到的詞有它的文本位置的用戶,例如:
「上找到在thext文件中的200處字」
因此我創建了一個計數器,每創建一個新節點就增加一個計數器。我現在的問題是,我的計數器只是統計了整個創建了多少個節點。所以我只看到創建了大約56000個節點,但我無法存儲節點的編號。
我在做什麼錯?
編輯:我沒有嘗試減少計數器,因爲我從不刪除一個節點。 這裏是我完整的代碼
#include <iostream>
#include <string>
#include <stdio.h>
#include <fstream>
#include <cstring>
using namespace std;
class Word
{
public:
Word (string inputstring = 0, int b = 0, int c = 0, int l = 0, Word *n = 0, Word *p = 0) : word (inputstring), book (b), chapter (c), length (l), next (n), prev (p)
{
++counter;
}
int book;
int chapter;
int length;
string word;
Word *next;
Word *prev;
static size_t howMany()
{
return counter;
}
private:
static size_t counter;
};
size_t Word::counter;
int main()
{
string inputstring = "empty";
string compare = "empty";
int l1 = 0;
int book = 0;
int chapter = 0;
int count = 0;
Word *p = 0;
Word *x = 0;
Word *start = 0;
ifstream file;
file.open("Beispieltext.txt");
ofstream outfile;
if (!file) cout << ("can't open input file");
else cout << "File: Beispieltext.txt open\n";
// create nodes
while (file >> inputstring)
{
l1 = (int)inputstring.length();
if ((!(inputstring[0] >= 'A' && inputstring[0] <= 'Z')) && (!(inputstring[0] >= 'a' && inputstring[0] <= 'z'))) inputstring = inputstring.substr(1,l1--); // l1-- reduce length
while ((!(inputstring[l1-1] >= 'A' && inputstring[l1-1] <= 'Z')) && (!(inputstring[l1-1] >= 'a' && inputstring[l1-1] <= 'z'))) inputstring = inputstring.substr(0,--l1); // --l1 go till n-1
// book?
if (std::strncmp(inputstring.data(), "BOOK", 4) == 0) ++book, chapter = 0/*, cout << "\nBook Nr.: " << book << "\n"*/;
// Chapter?
if (std::strncmp(inputstring.data(), "CHAPTER", 7) == 0) ++chapter/*, cout << "chapter: " << chapter << "\n"*/;
if (p == NULL)
{
p = new Word (inputstring);
} else
{
x = new Word (inputstring, book, chapter, l1, 0, p);
p->next = x;
p = x;
}
}
file.close();
cout << "File: Beispieltext.txt closed!\n";
// n...0
for (; p; p = p->prev) start = p; // go to start
// Open compare file 1
file.open("Suchbegriffe_1.txt");
if (!file) cout << "Can't open compare file!\n";
else cout << "File: Suchbegriffe.txt open!\n";
// Open result file 1
outfile.open("Result_1.txt");
if(!outfile) cout << "Can't open Result_1.txt file!\n";
else cout << "File: Result_1.txt open!\n";
while (file >> compare)
{
l1 = (int)compare.length();
// Search
x = start;
// 0...n go to end
for (; x; x = x->next)
{
if (l1 == x->length)
{
if (compare == x->word)
{
outfile << "Word: " << compare << " found in book Nr.: " << x->book << ", chapter: " << x->chapter << "!\n";
outfile << "Word: " << compare << " is the " << x->howMany() << " Word in the book.\n";
count++;
}
}
}
outfile << "Word: " << compare << ", " << count << "x found!\n";
count = 0;
}
file.close();
cout << "File: Suchbegriffe_1.txt closed!\n";
outfile.close();
cout << "File: Result.txt closed!\n";
// Open compare file 2
file.open("Suchbegriffe_2.txt");
if (!file) cout << "Can't open compare file!\n";
else cout << "File: Suchbegriffe.txt open!\n";
// Open result file 2
outfile.open("Result_2.txt");
if (!outfile) cout << "Can't open Result_2 file!\n";
else cout << "File: Result_2.txt open!\n";
while (file >> compare)
{
l1 = (int)compare.length();
// Search
x = start;
// 0...n go to end
for (; x; x = x->next)
{
if (l1 == x->length)
{
if (compare == x->word)
{
outfile << "Word: " << compare << " found in book Nr.: " << x->book << ", chapter: " << x->chapter << "!\n";
count++;
}
}
}
outfile << "Word: " << compare << ", " << count << "x found!\n";
count = 0;
}
file.close();
cout << "File: Suchbegriffe_2.txt closed!\n";
outfile.close();
cout << "File: Result_2.txt closed!\n";
}
請提供其餘的代碼(並正確格式化),而不是指望人們填寫空格。 – 2014-10-03 10:49:16
你是否試圖在析構函數中減少計數器? – Jepessen 2014-10-03 10:49:16
@Jepessen,這不會改變每個節點共享單個全局計數器的事實,因此不可能爲每個節點返回不同的ID – 2014-10-03 10:51:42