All pastes #1503233 Raw Edit

Someone

public c v1 · immutable
#1503233 ·published 2009-07-22 14:00 UTC
rendered paste body
#include <stdio.h>#include <stdlib.h>#include <string.h>#include <ctype.h>#define SIZE 10#define WORD_SIZE 64#define TEXT_SIZE 512typedef struct {  size_t size;  void *ptr;  size_t alloc_size;  size_t use_size;} ARRAY;typedef struct {  ARRAY word;  ARRAY text;} WORD;int word_sort(const void *a, const void *b){  return alphasort(&(((WORD *)a)->word.ptr), &(((WORD *)b)->word.ptr));}void init_array(ARRAY *ap, size_t size){  memset(ap, 0, sizeof(ARRAY));  ap->size = size;}void alloc_array(ARRAY *ap){  void *temp;    temp = ap->ptr;    if (ap->ptr) {    if (ap->use_size % (ap->size * SIZE) == 0) {      ap->alloc_size += ap->size * SIZE;      temp = realloc(temp, ap->alloc_size);      memset(&((char *)temp)[ap->alloc_size - (ap->size * SIZE)], 0, ap->size * SIZE);    }  }else{    ap->alloc_size = ap->size * SIZE;    temp = malloc(ap->alloc_size);    memset(temp, 0, ap->alloc_size);  }    ap->ptr = temp;}void expand_array(ARRAY *ap, size_t expand_size){  size_t old_size;  size_t new_size;  size_t init_size;    if (ap->alloc_size >= expand_size) {    return;  }    if (expand_size % (ap->size * SIZE) == 0) {    new_size = expand_size;  }else{    new_size = ((int)(expand_size / (ap->size * SIZE)) + 1) * (ap->size * SIZE);  }    old_size = ap->alloc_size;  init_size = new_size - old_size;  ap->alloc_size = new_size;  ap->ptr = realloc(ap->ptr, ap->alloc_size);  memset(&((char *)ap->ptr)[old_size], 0, init_size);}void trim_symbol(char *cp, size_t n){  int i;  int c;    i = 0;  c = 0;  while (cp[i] || i == n) {    if (isalpha(cp[i])) {      cp[c] = cp[i];      c++;    }    i++;  }    cp[c] = '\0';}void init_text(char cp[1024], FILE *in){  char temp[1024];    fgets(temp, 1024, in);  strncpy(cp, temp, 1024);}void tokenize_text(ARRAY *word_list, char cp[1024]){  int i;  char *word;  WORD *tword;  char *tcp;    i = (word_list->use_size / word_list->size) - 1;  word = strtok(cp, ".,? \n");  while (word != NULL) {    i++;    alloc_array(word_list);    tword = &(((WORD *)(word_list->ptr))[i]);    init_array(&(tword->word), sizeof(char));    alloc_array(&(tword->word));    expand_array(&(tword->word), strlen(word) + 1);    init_array(&(tword->text), sizeof(char));    alloc_array(&(tword->text));    tcp = tword->word.ptr;    strncpy(tcp, word, tword->word.alloc_size);    tword->word.use_size = strlen(word) + 1;    trim_symbol(tcp, tword->word.alloc_size);    word = strtok(NULL, ".,? \n");    word_list->use_size += word_list->size;  }}void show_words(ARRAY *ap, FILE *out){  int i;    for (i = 0; i < (ap->use_size / ap->size); i++) {    fprintf(out, "[%d] = %s\n", i, (char *)(((WORD *)(ap->ptr))[i].word.ptr));  }}int menu(void){  int temp;    puts("MENU");  puts("--------------------");  puts("1.単語を登録");  puts("2.単語を一括登録");  puts("3.単語の検索");  puts("4.単語の編集");  puts("5.単語の削除");  puts("6.データの保存");  puts("0.終了");  puts("--------------------");  puts("番号を入力してください");    temp = getchar();  getchar();    return temp - '0';}int main(void){  FILE *in = NULL;  FILE *out = NULL;  FILE *ifp = NULL;  FILE *ofp = NULL;  char input[1024];  ARRAY word_list;  size_t use = 0;  size_t size;    /*while (1) {    switch (menu()) {    case 0:      exit(0);      break;    case 1:            break;    case 2:      break;    case 3:      break;    case 4:      break;    case 5:      break;    case 6:      break;    default:      continue;    }    }*/    puts("入力元を指定してください(s:標準入力 f:ファイル)");  switch (getchar()) {  case 's':    in = stdin;    break;  case 'f':    ifp = fopen("input.txt", "r");    in = ifp;    break;  }  getchar();    puts("出力先を指定してください(s:標準出力 f:ファイル)");  switch (getchar()) {  case 's':    out = stdout;    break;  case 'f':    ofp = fopen("output.txt", "w");    out = ofp;    break;  }  getchar();    memset(input, 0, 1024);    init_text(input, in);  init_array(&word_list, sizeof(WORD));  tokenize_text(&word_list, input);  qsort(word_list.ptr, word_list.use_size / word_list.size, word_list.size, word_sort);  show_words(&word_list, out);    if (ifp) {    fclose(ifp);  }  if (ofp) {    fclose(ofp);  }    return 0;}