Добавил:
Опубликованный материал нарушает ваши авторские права? Сообщите нам.
Вуз: Предмет: Файл:

sem10_1

.c
Скачиваний:
0
Добавлен:
29.05.2019
Размер:
3.17 Кб
Скачать
#include <stdio.h>
#include <stdlib.h>
#include <string.h>

typedef struct word* wordptr;
struct word
{
 int count;
 char* word;
 wordptr left;
 wordptr right;
};

wordptr new_word(char* word);
void insert_word(char* word, wordptr* first_element);
void print_ordered(wordptr head, int order_asc);
int is_letter(char a);
char* get_word(FILE* f, int* is_eof);
int str_cmp(char* a, char* b);
char to_lower(char a);

int main(int argc, char** argv)
{
 if((argc != 4) && (argc != 2))
 {
  puts("usage: sem10_1 <filename> -o <DESC/ASC>");
  return 0;
 }
 int order_asc = 1;
 if(argc == 4)
 {
  if(!strcmp(argv[3], "DESC"))
   order_asc = 0;
 }
 FILE* file = fopen(argv[1], "r");
 int is_eof = 0;
 wordptr tree = NULL;
 char* word;
 while(!is_eof)
 {
  word = get_word(file, &is_eof);
  if(!is_eof)
  {
   insert_word(word, &tree);
   free(word);
  }
 }
 print_ordered(tree, order_asc);
 return 0;
}
wordptr new_word(char* word_)
{
 wordptr result = (wordptr)malloc(sizeof(struct word));
 result->count = 1;
 result->left = NULL;
 result->right = NULL;
 result->word = (char*)malloc(strlen(word_));
 strcpy(result->word, word_);
 return result;
}
void insert_word(char* word, wordptr* first_element)
{
 if(*first_element == NULL)
 {
  *first_element = new_word(word);
  return;
 }
 wordptr curr = *first_element;
 int inserted = 0;
 while(!inserted)
 {
  if(str_cmp(word, curr->word) < 0)
  {
   if(curr->left == NULL)
   {
    curr->left = new_word(word);
    inserted = 1;
   }
   else
    curr = curr->left;
  }
  else if(str_cmp(word, curr->word) > 0)
  {
   if(curr->right == NULL)
   {
    curr->right = new_word(word);
    inserted = 1;
   }
   else
    curr = curr->right;
  }
  else
  {
   curr->count++;
   inserted = 1;
  }
 }

}
void print_ordered(wordptr head, int order_asc)
{

 if(order_asc)
 {
  if(head->left != NULL)
   print_ordered(head->left, order_asc);
  printf("%s -- %d\n", head->word, head->count);
  if(head->right != NULL)
   print_ordered(head->right, order_asc);
 }

if(!order_asc)
{
  if(head->right != NULL)
   print_ordered(head->right, order_asc);
  printf("%s -- %d\n", head->word, head->count);
  if(head->left != NULL)
   print_ordered(head->left, order_asc);
}
}
char* get_word(FILE* f, int* is_eof)
{
 int len = 0;
 int real_len = 0;
 int c;
 char* result = NULL;
 c = fgetc(f);
 while(!is_letter(c) && (c != EOF))
  c = fgetc(f);
 while(is_letter(c) && (c != EOF))
 {

  if(++len > real_len)
  {
   real_len += 32;
   result = (char*)realloc(result, sizeof(char) * real_len);
  }
  result[len - 1] = c;
  c = fgetc(f);
 }
 result = (char*)realloc(result, sizeof(char) * ++len);
 result[len - 1] = '\0';
 if(c == EOF)
  *is_eof = 1;
 else
  *is_eof = 0;
 return result;
}
int is_letter(char a)
{
 return ((a >= 'a') && (a <= 'z')) || ((a >= 'A') && (a <= 'Z'));
}
int min(int a, int b)
{
 return a < b ? a : b;
}
int str_cmp(char* a, char* b)
{
 int lena = strlen(a);
 int lenb = strlen(b);
 int len = min(lena, lenb);
 int pos = 0;
 while((pos < (len-1)) && (to_lower(a[pos]) == to_lower(b[pos])))
  pos++;
 return to_lower(a[pos]) - to_lower(b[pos]);
}

char to_lower(char a)
{
 if((a >= 'A') && (a <= 'Z'))
  a += 'a' - 'A';
 return a;
}
Соседние файлы в предмете Информатика