私はデータバッグ構造を作成しました。私はファイルのテキストを読み、各単語をノードに挿入し、同じ文字列があればカウントをインクリメントします。しかし、私の問題は、同じ文字列のうちの1つの文字列とそれが使用された回数だけを出力したいということです。しかし、私がremove関数を使用するときはいつでも、ファイル内のすべてを削除します。もし私がそれを使用しなければ、私は以下の出力を得ます。私は間違って何をしているのかわかりません、重複した文字列を出力しない方法はありますか?特定のノードの削除
ofstream output;
struct BagNode
{
string dataValue;
string dataCopy;
int dataCountCopy;
int dataCount;
BagNode * next;
};
class Bag{
private:
BagNode * head;
public:
Bag()
{
head = NULL;
}
void insert(string v)
{
if(head == NULL){ //empty list
head = new BagNode;
removePunct(v);
head->dataValue = v;
transform(v.begin(), v.end(), v.begin(), ::tolower);
head->dataCopy = v;
head->next = NULL;
}
else
{
BagNode * n = new BagNode; // new node
removePunct(v);
n->dataValue = v;
transform(v.begin(), v.end(), v.begin(), ::tolower);
n->dataCopy = v;
BagNode * current = head; //for traversal
//current = head;
n->dataCount = 0;
if(current->dataCopy > v)
{
n->next = head;
head = n;
}
else{ //mid and tail insert
while(current->next && current->next->dataCopy < v)
{
current = current->next;
}
n->next = current->next;
current->next = n;
}
}
BagNode * check = new BagNode;
for(check = head; check->next != NULL; check = check->next)
{
if(check->dataCopy == v)//isSame(check->dataValue, v))
{
check->dataCount++;
}
}
}
bool remove(string v) //bool
{
bool status;
if(head == NULL){
status = false;
}
else if(head->dataCopy > v)
{//(head->dataValue > v){
status = false;
}
else if(head->dataCopy == v)
{
BagNode * t = head;
head = head->next;
delete t;
status = true;
}
else//general case
{
BagNode * current = head;
while(current->next && current->next->dataCopy < v){
current = current->next;
}
if(current->next == NULL)
{
status = false;
}
else if(current->next->dataCopy == v) //found it
{
BagNode *t = current->next;
current->next = current->next->next;
delete t;
status = true;
}
else
{
status = false;
}
}
return status;
}
void traverse()
{
BagNode * current;
current = head;
while(current)
{
output << current->dataValue << " (" << current->dataCount << ")" << " ";
current = current->next;
}
cout << endl;
}
出力:10Annette(1)1805(1)7(1)(1)(2)(3)(4)(5)(6)全て(1 )全て(2)(1)及び(1)及び(2)及び(3)及び(4)及び(5)及び(6)、(10)及び(7)
if(!inputFile)
{
cout << "Could Not Open " << fileName << " File" << endl;
exit(EXIT_FAILURE);
}
else
{
while(inputFile >> text)
{
theBag.insert(text);
}
cout << "Processing File Complete" << endl;
cout << "Please Enter An Output File Name: ";
getline(cin,outputFilename);
output.open(outputFilename);
theBag.traverse();
theBag.remove(text);
inputFile.close();
output.close();
}
@RawN Ok、その上にあります。ありがとう – Thompson
あなたのインストラクターの邪魔になるかもしれない愚かな単純な方法は 'std :: map strings;'これで 'strings [stringIJustParsed] ++;'と事実上全て作業の完了です。マップは 'stringIJustParsed'の新しい' int'を作成し、 'stringIJustParsed'が既に分かっている場合はゼロを初期化するか、既存の' int'を取得します。新規または古い 'int'、' int'がインクリメントされ、カウンタが提供されます。 –
user4581301
@ user4581301私のdataCountのために、私は本当にそれを必要としませんか? – Thompson