Question

In: Computer Science

WRITE A C++ PROGRAM TO IMPLEMENT THE CONCEPT OF INDEX (Create index in text file)

WRITE A C++ PROGRAM TO IMPLEMENT THE CONCEPT OF INDEX (Create index in text file)

Solutions

Expert Solution

#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <stdbool.h>
#include <ctype.h>

#include "index.h"

/* Text functions area */

FILE *getFile (int argc, char const *argv[]) { /* A function to get the file inclouding a input check */
    FILE *theinput;

    if (argc < 2) { /* no argument */
        fprintf(stderr, "Must pass an argument!\n");
        exit(1);
    }

    theinput = fopen(argv[1], "r");

    if (!theinput) { /* Argument is non-existing file */
        fprintf(stderr, "Can't read %s\n", argv[1]);
        exit(1);
    }

    return theinput;
}

char *getText (FILE *file) { /* Gets the text from file, Return a string with the file content */
    char *text, *q;
    char token;
    int i = 0;
    text = malloc(sizeof(char));
    while ((token = getc(file)) != EOF) {
        text[i] = token;
        i++;
        q = realloc(text, (i + 2) * sizeof(char));
        if (!q) {
            fprintf(stderr, "Out of memory\n");
            exit(1);
        }
        text = q;
    }
    text[i] = '\0';
    return text;
}

bool isLegalChar (char a) { /* Checks if a char is A - Z or a - z or 0 - 9 using ascii */
    if (a <= 90 && a >= 65) { /* A = 65, Z = 90 */
        return true;
    }
    else if (a <= 122 && a >= 97) { /* a = 97, z = 122 */
        return true;
    }
    else if (isdigit(a)) {
        return true;
    }
    else {
        return false;
    }
}

char *getWord (char *text, int start, bool *isEnterFound) { /* returns a single word from text starts searching from start */
    char *word,*q; /* q = a helper string to make sure mallocing well */
    char token; /* token is the right now input char */
    int i = 0;
    int j = start;
    word = malloc(sizeof(char));
    while (isLegalChar(token = text[j++]) || token == '\n') { 
        if (token == '\n')
        {
            (*isEnterFound) = true;
            break;
        }
        word[i++] = token;
        q = realloc(word, (strlen(word) + 2) * sizeof(char));
        if (!q) {
            fprintf(stderr, "Out of memory\n");
            exit(1);
        }
        word = q;
    }
    word[i] = '\0';
    return word;
}

void tolowerString (char *input) { /* Changes a string so all the letters in it will by lower */
    int i;
    for (i = 0; i < strlen(input); ++i)
    {
        input[i] = tolower(input[i]);
    }
}

char* stradd(const char* a, const char* b) { /* Return a string which is the two given strings combined */
    size_t len = strlen(a) + strlen(b);
    char *ret = (char*)malloc(len * sizeof(char) + 1);
    *ret = '\0';
    return strcat(strcat(ret, a) ,b);
}

/* Tree functions area */ 

Node* newNode(char* word, int line) { /* Return a new node with the vars given to the funcion */
    Node *p = (Node*) malloc (sizeof(Node)); 
    p->word = word;
    p->lines = malloc(sizeof(int));
    p->lines[0] = line;
    p->linesLength = 1;
    p->ls = p->rs = NULL; 
    return p; 
}

void insert(Node **root, char *word, int line) { /* Insert node to tree using recursion */
    int *q;
    int i;
    if(!*root) {
        *root = newNode(word, line); 
        return; 
    }
    if(strcmp(word, (*root)->word) < 0)  /* In case word is smaller than the nodes word */
    { 
        insert(&((*root)->ls),word,line); 
    }
    else if (strcmp(word, (*root)->word) == 0) /* In case word already exist */
    {
        for (i = 0; i < (*root)->linesLength; ++i)
        {
            if ((*root)->lines[i] == line)
            {
                return;
            }
        }
        q = realloc((*root)->lines, (*root)->linesLength + 2);
        if (!q)
        {
            fprintf(stderr, "Out of memory!\n");
            exit(1);
        }
        (*root)->lines = q;
        (*root)->lines[(*root)->linesLength] = line;
        (*root)->linesLength++;
        return;
    }
    else /* In case word is bigger than the nodes word */
    {
        insert(&((*root)->rs),word,line); 
    }
}

void printTreeRec(Node *root, FILE *outputFile) { /* A recursive function which prints the tree by given root */
    int i;

    if(!root) {
        return; 
    }
    printTreeRec(root->ls, outputFile);

    fprintf(outputFile, "The word: \"%s\" ",root->word); 
    if (strlen(root->word) < 15) {
        for (i = 0; i < 15 - strlen(root->word); ++i) {
            fprintf(outputFile, " ");
        }
    }   
    fprintf(outputFile, "appears in ");
    if (root->linesLength == 1) {
        fprintf(outputFile, "line: ");
    }
    else {
        fprintf(outputFile, "lines: ");
    }
    for (i = 0;i < root->linesLength - 1; i++) {
        fprintf(outputFile, "%d, ", root->lines[i]);
    }
    fprintf(outputFile, "%d", root->lines[i]);
    fprintf(outputFile, "\n");

    printTreeRec(root->rs,outputFile);
}

void freeTree(Node *root) { /* A function to free the tree recursively */
    if(!root)
        return; 
    freeTree(root->ls);
    freeTree(root->rs); 
    free(root->lines);
    free(root); 
}

/* Main */

int main(int argc, char const *argv[]) {
    FILE *inputFile = getFile(argc,argv); /* Opening input file */
    char *ouputAddress = stradd(argv[1],".index"); /* Creating output file address */
    FILE *outputFile = fopen(ouputAddress,"w"); /* Creating output file */
    char *text = getText(inputFile);
    char *word;
    int line = 1; /* Line counter */
    bool foundEnter = false;
    bool *isEnterFound = &foundEnter;
    Node *root = NULL; /* First node of the tree */
    int start = 0; /* Pointer to what was the last thing readed in text string */

    while (start < strlen(text)) {
        word = getWord(text, start, isEnterFound); /* If getWord detected enter foundEnter will become true */
        tolowerString(word);
        start += strlen(word) + 1;

        if (strcmp(word, "") != 0) /* If word is not NULL */
        {
            insert(&root,word,line);
        }

        if (foundEnter)
        {
            foundEnter = false;
            line++;
        }
    }

    /* Ptinting the tree */
    printTreeRec(root, outputFile);

    /* Freeing the tree */
    freeTree(root);
    fclose(inputFile);
    fclose(outputFile);
    free(ouputAddress);
    free(text);
    free(word);

    return 0;
}

Related Solutions

WRITE A JAVA PROGRAM TO IMPLEMENT THE CONCEPT OF INDEX (Create index in text file) full...
WRITE A JAVA PROGRAM TO IMPLEMENT THE CONCEPT OF INDEX (Create index in text file) full code
Write a C++ program to create a text file. Your file should contain the following text:...
Write a C++ program to create a text file. Your file should contain the following text: Batch files are text files created by programmer. The file is written in notepad. Creating a text file and writing to it by using fstream: to write to a file, you need to open thew file as write mode. To do so, include a header filr to your program. Create an object of type fsrteam. Open the file as write mode. Reading from a...
Create this C++ program using classes 1. Create a file text file with a string on...
Create this C++ program using classes 1. Create a file text file with a string on it 2. Check the frecuency of every letter, number and symbol (including caps) 3. Use heapsort to sort the frecuencys found 4. Use huffman code on the letters, symbols or numbers that have frecuencys I created the file, and the frecuency part but i'm having trouble with the huffman and heapsort implementation.
Write a C program that Reads a text file(any file)  and writes it to a binary file....
Write a C program that Reads a text file(any file)  and writes it to a binary file. Reads the binary file and converts it to a text file.
Write a java program: Write a program that creates a text file. Write to the file...
Write a java program: Write a program that creates a text file. Write to the file three lines each line having a person's name. In the same program Append to the file one line of  'Kean University'.  In the same program then Read the file and print the four lines without lines between.
In C++, write a program that reads data from a text file. Include in this program...
In C++, write a program that reads data from a text file. Include in this program functions that calculate the mean and the standard deviation. Make sure that the only global variables are the actual data points, the mean, the standard deviation, and the number of data entered. All other variables must be local to the function. At the top of the program make sure you use functional prototypes instead of writing each function before the main function... ALL LINES...
Using C++, write a code that this program always stores text file output into a text...
Using C++, write a code that this program always stores text file output into a text file named "clean.txt". -The program should read one character at a time from "someNumbers.txt", and do the following. -If it is a letter, print that letter to the screen, AND also store it in the text file. All letters should be converted to lowercase beforehand. -If it is a number, print that number to screen, but do NOT store it in the text file....
Write a C++ program that reads a string from a text file and determines if the...
Write a C++ program that reads a string from a text file and determines if the string is a palindrome or not using stacks and queue
For C++ Write a program that opens a specified text file then displays a list of...
For C++ Write a program that opens a specified text file then displays a list of all the unique words found in the file. Hint: Store each word as an element of a set.
Write a C program to run on unix to read a text file and print it...
Write a C program to run on unix to read a text file and print it to the display. It should count of the number of words in the file, find the number of occurrences of a substring, and take all the words in the string and sort them (ASCII order). You must use getopt to parse the command line. There is no user input while this program is running. Usage: mywords [-cs] [-f substring] filename • The -c flag...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT