示例#1
0
int main(void)
{
    t_table *table = table_init(128);
    if (table == NULL) {
        fprintf(stderr, "Table could not be initialized!\n");
        return EXIT_FAILURE;
    }
    
    print_table_info(table);
    print_table(table);

    int data_pole[NUM] = {0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 0, 1, 2, 3, 4, 5, 6, 7, 8, 9};
    t_string key_pole[NUM];

    int i;
    for (i = 0; i < NUM; i++) {
        stringInit(&key_pole[i]);
    }

    for (i = 0; i < NUM; i++) {
        stringAddChar(&key_pole[i], 'a' + i);
    }

    int ret = 0;
    t_tab_data * data_ptr;

    for (i = 0; i < NUM; i++) {
        ret = table_insert(table, &key_pole[i], &data_ptr);
        switch (ret) {
        case TAB_INSERT_OK:
            data_ptr->a = data_pole[i];
            break;
        case TAB_INSERT_FOUND:
            fprintf(stderr, "Item already inserted\n");
            break;
        case TAB_INSERT_ALL_ERR:
            fprintf(stderr, "Allocation error\n");
            break;
        }
    }

    print_table_info(table);
    print_table(table);
    
    /*
    //1. raz
    printf("INSERT\n\n");
    for (i = 0; i < NUM; i++) {
        ret = table_insert(table, key_pole[i], 7, (t_tab_data **) &data);
        switch (ret) {
        case TAB_INSERT_FAIL:
            fprintf(stderr, "Item already inserted.\n");
            break;
        case TAB_INSERT_FULL:
            while (ret == TAB_INSERT_FULL) {
                if (table_resize(table) == TAB_RESIZE_FAIL) {
                    fprintf(stderr, "RESIZE FAIL\n");
                    table_destroy(table);
                    return EXIT_FAILURE;
                }
                ret = table_insert(table, key_pole[i], 7, (t_tab_data **)&data);
            }
            if (ret == TAB_INSERT_FAIL) {
                fprintf(stderr, "Item already inserted.\n");
                break;
            }
            if (ret == TAB_INSERT_ALL_ERR) {
                fprintf(stderr, "ALLOC ERR\n");
                table_destroy(table);
                return EXIT_FAILURE;
            }
            *(int *)data = data_pole[i];
            break;
        case TAB_INSERT_OK:
            *(int *)data = data_pole[i];
            break;
        case TAB_INSERT_ALL_ERR:
            fprintf(stderr, "ALLOC ERROR\n");
            table_destroy(table);
            return EXIT_FAILURE;
        }
    }

    print_table(table);
    */
    
    printf("-----------------------------------------------------------------------------------------\n\n");

    printf("GET DATA\n\n");
    for (i = 0; i < NUM; i++) {
        ret = table_search(table, &key_pole[i], (t_tab_data **) &data_ptr);
        if (ret == TAB_SEARCH_OK) {
            printf("Key:    %s\n", key_pole[i].string);
            printf("Data:  %d\n\n", data_ptr == NULL ? -1 : ((t_tab_data *)data_ptr)->a);
        }
    }

    /*
    print_table(table);
    */

    for (i = 0; i < NUM; i++) {
        stringFree(&key_pole[i]);
    }
    table_destroy(table);
    return EXIT_SUCCESS;
}
/****************************************************************************
* Menu option #6: Delete Stop Words
* Removes common words from the index supplied from an external file.
****************************************************************************/
void deleteStopWords(IndexType* index)
{
  FILE *fp;
  unsigned int injunk,in,notin;
  char word[MAXLENGTH],c;
  char buffer[BUFFSIZE];
  WordTypePtr node,prev,temp;
  String line;

  printf("\nDelete Stop Words\n");
  printf("-----------------\n");


  getString(word,MAXLENGTH,0,"Enter name of stop word file:");

  if(strlen(word)==0)
  {
    printf("Delete Stop Words cancelled. Returning to main menu.\n");
    return;
  }

  if((fp=fopen(word,"rb"))==NULL)
  {
    printf("Failed to read %s !\n",word);
    return;
  }

  setvbuf(fp,buffer,_IOFBF,BUFFSIZE);

  stringInit(&line);
  injunk=FALSE;
  in=FALSE;
  notin=FALSE;

  while((c=fgetc(fp))!=EOF)
  {
    if((c>=a && c<=z) || (c>=A && c<=Z))
    {
      injunk=FALSE;
      stringAddChar(&line,c);
    }
    if(!injunk)
    {
      if(c==SPACE || c=='\n')
      {
        /*found the word so search index and remove the node if its found*/
        node=index->headWord;
        prev=node;
        while(node!=NULL)
        {
          /*found the word to delete so delete it*/
          if(strcmp(node->word,line.contents)==0)
          {
            temp=deletebetween(prev,node);

            /*temp=node;*/


            if(node==index->headWord)
                index->headWord=temp;
            else
                node=temp;


            stringClear(&line);
            injunk=TRUE;
            in++;
            break;
          }
          prev=node;
          node=node->nextWord;

        }
        /*the word wasnt found in the list*/
        if(node==NULL)
            notin++;

      }
    }
  }
  printf("\n\n%d words removed from the index. %d words not found."
         ,in,notin);

  stringFree(&line);
  fclose(fp);


}
示例#3
0
int main(int argc, char *argv[])
{
	char ch, *str, *strStart;
	int i;
	double number;
	struct String input = {NULL, 0, 0, 5};

	/* Kick off scope  */
	struct Scope *currentScope = NULL;
	addScope(currentScope, &currentScope);

	/* Read input from... */
	if (argc == 1) {
		/* stdin */
		while ((ch = getchar()) != '\n' && ch != EOF) {
			stringAddChar(&input, ch);
		}
	} else {
		/* args */
		for (i = 1; i < argc; i++) {
			stringAddStr(&input, argv[i]);
			stringAddChar(&input, ' ');
		}
	}

	/* Tokenise */
	str = input.str;
	while (*str != '\0') {
		strStart = str;
		if (isspace(*str)) {
			/* Space */
			str++;
		} else if (*str == '(') {
			addScope(currentScope, &currentScope);
			str++;
		} else if (*str == ')') {
			insertToken(currentScope, NUM, '\0', evaluateScope(currentScope, &currentScope));
			str++;
		} else if ((number = strtold(str, &str)) == 0.0L && (number = strtoconst(str, &str)) == 0.0L) {
			/* NaN */
			insertToken(currentScope, OP, normalise(*str), 0);
			str++;
		} else {
			/* Number */
			if ((*strStart == '+' || *strStart == '-')
				&& currentScope->last != NULL
				&& strcmp(currentScope->last->type, OP)) {
				//Assume this was supposed to be an operation
				insertToken(currentScope, OP, *strStart, 0);
			}
			insertToken(currentScope, NUM, '\0', number);
		}
	}

	/*
	 * TODO: Increase precision
	 * TODO: Allow precision to be specified
	 */
	printf("%.15g\n", evaluateScope(currentScope, &currentScope));

	free(input.str);

	return 0;
}
/****************************************************************************
* Menu option #4: Stemming Report
* Creates an external file with statistics of all words in the index that
* have common suffixes.
****************************************************************************/
void stemmingReport(IndexType* index)
{
  FILE *fp;
  WordTypePtr node;
  char filename[FILENAME_MAX];
  String stem;
  char c;
  char buffer[BUFFSIZE];
  char* longestword;

  unsigned int nodelength;



  printf("\nStemming Report\n");
  printf("---------------\n");


  getString(filename,FILENAME_MAX,0,"Enter name of a suffixes file:");
  /*if they enter no file name cancel to the main menu*/
  if(strlen(filename)==0)
  {
    printf("\nStemm report cancelled. Returning to main menu.\n");
    return;
  }

  /*open the file*/
  fp=fopen(filename,"r");
  if(fp==NULL)
  {
    printf("\n Failed to read %s file!\n",filename);
    return;
  }

   stringInit(&stem);

  /*reserve space for longest word*/
  longestword=malloc(strlen(index->longestWord->word)*sizeof(char));


  /*speed up*/
  setvbuf(fp,buffer,_IOFBF,BUFFSIZE);

  printf("\nResults:\n\n");

  /*loop through all nodes*/
  node=index->headWord;
  while(node!=NULL)
  {
    /*printf("node is %s\n",node->word);*/
    nodelength=strlen(node->word);

    /*loop through all stem words*/
    fseek(fp,0,0);
    while((c=fgetc(fp))!=EOF)
    {
      /*next stem words is ready*/
      if(c=='\n')
      {
        /*if the stem word is larger than the node word skip the node*/
        if(stem.offset>nodelength)
        {
          stringClear(&stem);
          continue;
        }
        /*check if the node word containts the stem word*/
        if(strcmp(stem.contents,node->word+nodelength-stem.offset)==0)
        {
          /*get the base word and null terminate*/
          strncpy(longestword,node->word,nodelength-stem.offset);
          *(longestword+nodelength-stem.offset)='\0';
          /*display the node and the stem prefix*/
          printf("%s %s\n",node->word,longestword);
        }
        stringClear(&stem);
      }
      /*keep building the stem word*/
      else
        stringAddChar(&stem,c);
    }
    node=node->nextWord;
  }
  fclose(fp);
  free(longestword);
  stringFree(&stem);
}