Добавил:
alex20000
Опубликованный материал нарушает ваши авторские права? Сообщите нам.
Вуз:
Предмет:
Файл:sem10_1alex
.c#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);
void print_head(wordptr head);
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);
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("word: %s count: %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("word: %s count: %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)) && (a[pos] == b[pos]))
pos++;
return a[pos] - b[pos];
}
void print_head(wordptr head)
{
if(head == NULL)
return;
printf("%s\n", head->word);
printf("left:");
print_head(head->left);
printf("right:");
print_head(head->right);
}