-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcounter.cpp
90 lines (83 loc) · 1.83 KB
/
counter.cpp
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
/*
*
* @author: Valeri Vladimirov 40399682
* Last Modified: 06/04/2019
* Purpose: To insert words from text files into a BinarySearchTree and output their count.
*
*/
#include <iostream>
#include <string>
#include <vector>
#include <cstdlib>
#include <fstream>
#include "BinarySearchTree.h"
#include "Node.h"
#define LINE_LENGTH 1000
using namespace std;
int main(int argc, char **argv)
{
// Declaring all the variables needed for the program.
string line;
string word;
string name = argv[1];
char *token;
ifstream myfile (name);
BinarySearchTree *tree = new BinarySearchTree();
// Looping through the file, checking if the word exists if not, then add to the BST if yes then increment its count.
if(myfile.is_open())
{
// If the file is with sentences check for punctuation.
if(name == "sentences_test.txt")
{
cout << "This is for the sentences_test.txt." << endl;
while(getline(myfile, line))
{
int lenght = line.size();
for(int i = 0; i < lenght; i++)
{
if(!(ispunct(line[i])) && line[i] != ' ' && line[i] != '\n')
{
word += line[i];
}
else if(word != "")
{
if(tree->exists(word))
{
tree->increment(word);
}
else
{
tree->insert(word);
}
word = "";
}
}
}
myfile.close();
}
else if(name == "single_words_test.txt")
{
cout << "This is for the single_words_test file." << endl;
while(getline(myfile, line))
{
if(tree->exists(line))
{
tree->increment(line);
}
else
{
tree->insert(line);
}
}
myfile.close();
}
}
else
{
cout << "Unable to open file." << endl;
}
// Outputing all the words and their count and deleting the tree.
tree->output();
delete(tree);
return 0;
}