You are on page 1of 5

Strip each token from a C source file and save them in another

file in a (line,word) format for future use

#include<iostream>
#include<cstdlib>
#include<cstring>
#include<fstream>
using namespace std;

ofstream of; // the output file handler

class BinarySearchTree
{
private:
struct where
{
int line, word;
struct where *next;
}*first;
struct node
{
char token[80];
struct where *first;
struct node *left, *right;
}*root;

public:
BinarySearchTree()
{
root = NULL;first = NULL;
}
bool isEmpty(){ return (root == NULL);}
void insert(char*,int,int);
void display(node*);
void print();
};

void BinarySearchTree :: insert(char *newToken,int newLine,int newWord)


{
node *n = new node;

strcpy(n->token,newToken);
n->right = NULL;
n->left = NULL;
if(isEmpty())
{
cout<<"The tree is empty\n";
root = n;
where *w = new where;
w->line = newLine;
w->word = newWord;
w->next = NULL;
root->first = w;
}
else
{
node *parent; //will be the parent of the newly added node
node *temp;
temp = root;

while(temp != NULL)
{
parent = temp;
if(strcmp(newToken,temp->token) == 0)
break;
else
{
if(strcmp(newToken,temp->token) < 0)
{
temp = temp->left;
}
else
/* if(strcmp(newToken,temp->token) > 0) */
{
temp = temp->right;
}
}
}
/* new occurance added in a new node of type where */
where *w = new where;
w->line = newLine;
w->word = newWord;
w->next = NULL;
//----------------

if(strcmp(newToken,parent->token) == 0)
/* i.e. newToken already there in the bst */
{
where *temp;
temp = parent->first;
while(temp->next !=NULL)
{
temp = temp->next;
}

temp->next = w; //placed in the right position


}

else
{
if(strcmp(newToken,parent->token) < 0)
{
n->first = w;
parent->left = n;
}
else
/* i.e. strcmp(newToken,parent->token) > 0 */
{
n->first = w;
parent->right = n;
}
}
}
}
void BinarySearchTree :: print(){
of.open("Output.txt");
display(root);
}
void BinarySearchTree :: display(node *n)
{
if(n != NULL)
{
display(n->left);
of<<"\nData: "<<n->token;
cout<<"\nData: "<<n->token;
where *w = n->first;
do
{
of<<"\t("<<w->line<<","<<w->word<<")"; // (line,word)
w = w->next;
}while(w != NULL);
display(n->right);
}
else
return;
}

int main(int argc,char *argv[])


{
int NEWLINE = 0;
int word = 0, line = 0;
int length = 0;
char str[80];
char ch;
BinarySearchTree bst;

ifstream infile("Input.c");

if(!infile){
cout<<"Can't open Input.c file"<<endl;
return 1;
}

while(infile)
{
infile.get(ch);
cout<<ch;
if(ch == '\t' || ch == '\n' || ch == ' ')
{
if(ch == ' ')
if(length !=0)
word++;

if(ch == '\n')
{
NEWLINE = 1;
}
if(length != 0)
{
str[length] = '\0';
bst.insert(str,line,word);
length = 0;
}
length = 0;
if(NEWLINE)
{
line++;
word=0;
NEWLINE = 0;
}
continue;
}

if(ch == 47) // 47 = '/'


{
infile.get(ch);
if(ch == 42) // 42 = '*' i.e. a multi line comment
{
do
{
infile.get(ch);
if(ch=='\n')
line++;
}while(ch != 47);
}
else
{
if(ch == 47) // i.e. // a single line comment
{
do
{
infile.get(ch);
}while(ch!='\n');
line++;
length = 0;
continue;
}
}
}

if((ch==33&&ch==34)||(ch>=36&&ch<=39)||
(ch>=42&&ch<=45)||(ch>=58&&ch<=64)||(ch==92)||
(ch>=94&&ch<=96)||(ch>=123&&ch<=126)||ch==47)
// that means its a punctuation marks
{
if(length != 0)
{
word++;
str[length] = '\0';
bst.insert(str,line,word);
if(ch == 59) //i.e. ch == ;
word=0;
length = 0;
}
}
else
{
if(infile)
str[length++] = ch;
}
}
cout<<"\nTotal words: "<<word<<endl;
bst.print();
infile.close();
outfile.close();
}
:::Output:::  

:::Input.txt:::

0. #include<stdio.h>
1. #include<stdlib.h>
2.
3. //Input file
4. main()
5. {
6. char buf[80];
7. /*multi
8. line
9. comment*/
10. int i;
11. int j;
12. char c;
13. }

:::Output.txt:::

Data: #include (0,1) (1,1)


Data: buf[80] (6,2)
Data: c (12,2)
Data: char (6,1) (12,1)
Data: i (10,2)
Data: int (10,1) (11,1)
Data: j (11,2)
Data: main() (4,0)
Data: stdio.h (0,2)
Data: stdlib.h (1,2)