Автор: Aluminium
Дата сообщения: 12.08.2011 14:55
Добрый день.
есть вод такой код:
[more]
Код: #include <stdio.h>
#include <string.h>
#include <ctype.h>
#define MAXLINE 1000
struct cWord {
int count;
char *word;
struct cWord *left;
struct cWord *right;
struct cWord *nleft;
struct cWord *nright;
} *pWord;
struct cWord *add_pWord(struct cWord *, struct cWord *, char *);
struct nWord *add_nWord(struct cWord *, struct cWord *, int);
struct cWord *printSort_pWord(struct cWord *);
struct nWord *printSort_nWord(struct nWord *);
void main()
{
int i = 0;
char c, line[1000];
while ((c = getchar()) != EOF)
{
while (!isspace(c))
{
*(line+i++) = c;
continue;
}
*(line+i) = '\0';
i = 0;
if (isalpha(*(line+i)))
{
pWord = add_pWord(NULL,pWord,line);
add_nWord(NULL,pWord,pWord->count);
}
}
}
struct cWord *add_pWord(struct cWord *parentWord, struct cWord *stcWord, char *inLine)
{
int n;
if (stcWord == NULL)
{
stcWord = (struct cWord *)malloc(sizeof(struct cWord));
stcWord->word = malloc(strlen(inLine));
stcWord->word = memcpy(stcWord->word,inLine,strlen(inLine));
stcWord->count = 1;
if (parentWord == NULL)
stcWord->left = stcWord->right = NULL;
else
{
if ((n = strcmp(inLine,parentWord->word)) > 0)
stcWord->left = parentWord;
else if (n < 0)
stcWord->right = parentWord;
}
}
else if ((n = strcmp(inLine,stcWord->word)) > 0)
{
if (strcmp(inLine,parentWord->word) < 0)
{
struct cWord *newWord = (struct cWord *) malloc(sizeof(struct cWord));
newWord->word = memcpy(newWord->word,inLine,strlen(inLine));
newWord->count = 1;
newWord->left = parentWord->left;
newWord->right = parentWord;
parentWord->left->right = newWord;
return newWord;
}
stcWord->right = add_pWord(stcWord,stcWord->right,inLine);
}
else if (n < 0)
{
if (strcmp(inLine,parentWord->word) > 0)
{
struct cWord *newWord = (struct cWord *) malloc(sizeof(struct cWord));
newWord->word = memcpy(newWord->word,inLine,strlen(inLine));
newWord->count = 1;
newWord->left = parentWord;
newWord->right = parentWord->right;
parentWord->right->left = newWord;
return newWord;
}
stcWord->left = add_pWord(stcWord,stcWord->left,inLine);
}
else
stcWord->count++;
return stcWord;
}
struct nWord *add_nWord(struct cWord *parentWord, struct cWord *curWord, int count)
{
if (curWord->left != NULL && count < curWord->left->count)
add_nWord(curWord,curWord->left,count);
else if (curWord->right != NULL && count > curWord->right->count)
add_nWord(curWord,curWord->right,count);
else
{
curWord->nleft = curWord->left;
curWord->nright = curWord->right;
}
}