1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169
|
#include <iostream>
#include <cstdlib>
#include <string>
#include <string.h>
#include <stdio.h>
#include <fstream>
#include <sstream>
using namespace std;
struct Index {
string word;
Index *left;
Index *right;
int *line; //will refer to dynamic array. not sure if it's correct
};
void AddNode (Index *&node, string temp_word)
{
if (node == NULL)
{
node=new Index;
node->word = temp_word;
node->left = NULL;
node->right = NULL;
}
else if (temp_word < node->word)
{
AddNode(node->left,temp_word);
}
else if (temp_word > node->word)
{
AddNode(node->right,temp_word);
}
};
void Print(Index *node)
{
if(node!=NULL)
{
Print(node->left);
cout << node->word << endl;
Print(node->right);
}
};
Index* Search(Index* node, string search_word)
{
if(!node)
{
cout << endl << "Word not found" << endl;
return 0;
}
if(search_word == node->word)
{
cout << endl << "Word found.. ";
return node;
}
if(search_word < node->word)
{
//cout << "Lewy" << endl;
return Search(node->left, search_word);
}
else
{
//cout << "Prawy" << endl;
return Search(node->right, search_word);
}
};
void Delete(Index *node)
{
Index *temp;
if (node!=NULL)
{
Delete(node->left);
Delete(node->right);
temp=node;
node=NULL;
delete temp;
}
}
int main(int argc, char* argv[])
{
int n = 0;
int position = 1;
string line;
ifstream text;
text.open("tekst.txt");
//counting number of lines in text. n used later to create
//dynamic array
if(text)
{
stringstream word;
while (getline(text,line))
{
n++;
}
}
int *Position;
Position = new int[n];
Index *root;
root=NULL;
text.close();
text.open("tekst.txt");
//testing if file exist
if(!text)
{
cout << "File not found" << endl;
return 1;
}
//converting text to word and entering them into tree
if(text)
{
stringstream word;
while (getline(text,line))
{
word << line;
while (getline(word,line,' '))
{
cout << "Found in " << position << " - ";
cout << line << endl;
AddNode(root,line);
}
Position[position-1]=position;
position++;
word.clear();
}
}
//assigning line numbers to array table
for (int i=0 ; i<n ; i++)
{
cout << Position[i];
};
//how to add Position[i] to node, for example word "ANYTHING" exists in lines 3,4,7,8
string search_word;
string temp1;
string temp2;
cout << endl << "Enter word: " << endl;
cin >> search_word;
Search(root,search_word);
//deleting tree
Delete(root);
text.close();
system("PAUSE");
return 0;
}
|