int ascii (struct word *q); // Programmed to calculate the ascii sum of the word.
void createword (struct word **start, char value); // Programmed to create a word.
void word_display(struct word *start); // Programmed to display a single word.
int word_comp(struct word *word1, struct word *word2); // Programmed to compare two words.
void tree_insert (struct filetree **q, struct word *word, int asciisum); // Programmed ot insert the word into the file tree.
void tree_display(struct filetree *start); // Programmed to display the file tree.
//****************************************************************************//
//This class reads a file form the disk.
class file
{
private:
FILE *fp;
public:
file (const char *name, const char *mode)
{
fp = fopen (name, mode);
if (fp==NULL)
{
printf("\nCannot open file: %s", name);
exit(1);
}
}
~file ()
{
fclose(fp);
}
FILE *getfilepointer()
{
return fp;
}
};
/*
*
*/
//static void feed_database (char word[50]);
/*
*
*/
int main(int argc, char** argv)
{
struct word *start = NULL;
struct filetree *tree = NULL;
int a;
char filename[67], ch;
printf("\nEnter the File Name..");
scanf("%s", filename);
file f (filename, "r");
while ((ch = fgetc(f.getfilepointer())) != EOF)
{
a = ((int)ch);
//Checking weather the letter is in desired limit or not.
//ascii value of charcter fecthed from file must be 33 <= letter <=126.
if((a>=97 && a <=122)||(a>=65 && a <= 90))
{
//We will have to create word here
//This letter will be included in the word being created.
createword(&start, ch);
}
else
{
//This will end up this word.
//This word will be stored in the tee.d
//Calculating ascii sum for this word.
//Test Code
int asciisum = 0;
asciisum = ascii(start);
cout<<"\n\n The ASCII sum [";
word_display(start);
cout<<"] is\t"<<asciisum;
tree_insert(&tree, start, asciisum);
start = NULL;
//Test Code Ends
}
}
cout<<"\n\n\n FILE TREE IS BEING DISPLAYED\n";
tree_display(tree);
cout<<"\n\n";
cout<<"Total Number of words present ["<<words<<"]\n\n\n";
return (EXIT_SUCCESS);
}
int ascii (struct word *q)
{
int ascii = 0;
struct word *start;
start = q;
while(start != NULL)
{
ascii += ((int) start->value);
start = start -> link;
}
return ascii;
}
//This function creates a linked list of a word according to the algo.
//CreateWord Fucntion
/*
* This function takes two arguments.
* 1. Address to the pointer containing start adress of word being created.
* 2. Value (New Character) to be inserted at the end of the current word.
*/
void createword (struct word **q, char value)
{
struct word *start;
struct word *temp;
start = *q;
temp = (struct word *) malloc (sizeof(struct word));
if(*q == NULL)
{
cout<<"\n\n\n NEW WORD STARTED\n";
}
cout<<"\n\n New character fetched from file\t"<<value;
cout<<"\n\n[ "<<value<<" ] is being inserted at memory location\t"<<temp;
temp->value = value;
temp->link = NULL;
//This Function is programmed to display a word.
/*
* This function takes only one argument.
* 1. the start pointers value of the word to be displayd.
* */
void word_display(struct word *start)
{
while(start != NULL)
{
cout<<start->value;
start = start->link;
}
}
//This Functoin Compares two words.
/* This function takes two arguments/
* 1. Word 1
* 2. Word 2
*
* This function returns on value
*
* 1. returns 1 if words are same and
* 2. returns 0 of words are not same.
*/
int word_comp(struct word *word1, struct word *word2)
{
while(word1 != NULL || word2 != NULL)
{
if(word1->value != word2->value)
break;
word1 = word1->link;
word2 = word2->link;
}
// Following function inserts a new node into FILE TREE
//this function intakes three arguments
/* 1. It takes the start address of the file tree.
* 2. It takes the start address of the word being insterted.
* 3. It takes the ascii sum of all the characters in the word.
*/
void tree_insert (struct filetree **q, struct word *word, int asciisum)
{
words ++;
cout<<"\n\n\n******************************************************************************\n\n\n";
cout<<"WE ARE NOW INSERTING [";
word_display(word);
cout<<"] INTO THE FILE TREE.\n\n\n";
struct filetree *temp; // Variable to insert new node into.
struct filetree *start; // Start address (variable) of the file tree.
start = *q;
temp = (struct filetree *) malloc (sizeof(struct filetree )); // Memory allocation to the temp.
temp->frequency = 1;
cout<<"\n\n Node being inserted at memory location\t["<<temp<<" ]";