コード例 #1
0
ファイル: htable-main.c プロジェクト: lchish/cosc
/**
 * Spell check a file using a hash-table dictionary.
 * Prints all words not in the dictionary to stdout and statistics to stderr.
 * @param ht hash-table to use as a dictionary.
 * @param filename name of file to be spell-checked.
 * @param fill_time time taken to fill the dictionary.
 */
static void spell_check(htable ht, char *filename, double fill_time){
  clock_t start,end;
  char word[256];
  int unknown_words = 0;
  FILE *file = efopen(filename,"r");
  start = clock();
  while ((getword(word, sizeof word, file)) != EOF) {
    if(htable_search(ht,word) == 0){/* word not found */
      printf("%s\n",word);
      unknown_words++;
    }
  }
  end = clock();
  fclose(file);
  fprintf(stderr,
	  "Fill time\t:%f\nSearch time\t:%f\n"
	  "Unknown words = %d\n",fill_time,
	  (end-start)/(double)CLOCKS_PER_SEC,unknown_words);
}
コード例 #2
0
ファイル: htable-main.c プロジェクト: zwpdbh/cosc242_code
/**
 * main programme will be excuted.
 * By default, words are read from stdin and
 * added to hashtable before being printed out
 * alongside their frequencies to stdout.
 * @param argc an integer saying how many arguments
 *  there are argc for “argument count”
 * @param argv an array of strings in which the arguments
 *  are stored (argv for “argument vector ”).
 * @return integer 1 to indicate if the programme excuted successfully or not.
 */
int main(int argc, char *argv[]) {
    const char *optstring = "ht:c:deps:";
    char option;
    char word[256];
    int capacity = 113;

    FILE *infile = NULL;
    clock_t start, end;

    hashing_t method = LINEAR_P;

    double fillTime;
    double searchTime;

    int unknownWords = 0;
    int numOfSnapshot = 0;

    htable  h;
    char *fileToBeChecked = NULL;
    int withC = 0;
    int withE = 0;
    int withP = 0;
    int withS = 0;
    /*
     * Begin processing the argument from the command line
     */
    while ((option = getopt(argc, argv, optstring)) != EOF) {
        switch (option) {
            case 't':
                if (atoi(optarg) <= 0){
                    capacity = 113;
                }else {
                    capacity = primegt(atoi(optarg));
                }
                break;
            case 'd':
                method = DOUBLE_H;
                break;
            case 'c':
                withC = 1;
                fileToBeChecked = optarg;
                break;
            case 'e':
                withE = 1;
                break;
            case 's':
                numOfSnapshot = atoi(optarg);
                if (numOfSnapshot <= 0) {
                    numOfSnapshot = 10;
                }
                withS = 1;
                break;
            case 'p':
                withP = 1;
                break;
            case 'h':
                printHelpInfo();
                return EXIT_SUCCESS;
            default:
                printHelpInfo();
                return EXIT_FAILURE;
        }

    }

    h = htable_new(capacity, method);
    start = clock();

    while (getword(word, sizeof word, stdin) != EOF) {
        htable_insert(h, word);
    }

    end = clock();
    fillTime = (end-start)/(double)CLOCKS_PER_SEC;

    /* prints all the details of the table (-e argument)*/
    if (withE == 1) {
        htable_print_entire_table(h, stderr);
    }

    /* Checks the input file against a dictionary (-c <filename> argument)*/
    if (withC == 1) {
        /*open file and check if it is valid*/
        if (NULL == (infile = fopen(fileToBeChecked, "r"))) {
            fprintf(stderr, "Can't open file '%s' using mode r\n",
                    fileToBeChecked);
            htable_free(h);
            return EXIT_FAILURE;
        }
        start = clock();
        /*Get words from input file, and search for them in the dictionary*/
        while (getword(word, sizeof word, infile) != EOF) {
            /*If the word isn't in the dictionary*/
            if (htable_search(h, word) == 0) {
                printf("%s\n", word);
                unknownWords += 1;
            }
        }
        end = clock();
        fclose(infile);
        searchTime = (end-start)/(double)CLOCKS_PER_SEC;
        fprintf(stderr, "Fill time\t:%f\n", fillTime);
        fprintf(stderr, "Search time\t:%f\n", searchTime);
        fprintf(stderr, "Unknown words = %d\n", unknownWords);

        htable_free(h);
        return EXIT_SUCCESS;
    }


    /*Prints table stats (-p -s arguments)*/
    if (withP == 1 && withS == 0) {
        htable_print_stats(h, stdout, 10);
    } else if (withP == 1 && withS == 1) {
        htable_print_stats(h, stdout, numOfSnapshot);
    } else {
        htable_print(h, print_info);
    }

    htable_free(h);

    return EXIT_SUCCESS;
}