K&R 练习题6-2至6-4

#include <stdio.h>
#include <ctype.h>
#include <string.h>
#include <stdlib.h>

#define MAX_LINES 100
#define MAX_LEN 1024

typedef struct tree
{
char *word;
int count;
int line[MAX_LINES];
struct tree *left;
struct tree *right;
} TREE;

int state, nmarks, hz_max = 1;

enum
{
STR = 0X1,
EXP1 = 0X2,
EXP2 = 0X4,
OVER = 0X8,

};

int getword(char *s, int max);
void deal_state(int type);
void deal_word(char *s);
TREE *save_word(TREE *p, char *s, int line);
void normal_print_word(TREE *p);
void classify_print(TREE *p, int n);
int strcmp_n(char *s1, char *s2, int n);
void hz_print(TREE *p);

main(int argc, char *argv[])
{
TREE *root;
char s[MAX_LEN];
int type, line;

line = 1;
root = NULL;
while ((type = getword(s, MAX_LEN)) != EOF)
{
	if (type == '\n')
		line++;
	deal_state(type);
	if (isalpha(*s))
	{
		deal_word(s);
		if ((state & (STR | EXP1 | EXP2 | OVER)) == 0)
		{
			root = save_word(root, s, line);
		}
	}
}

/* normal_print_word(root);
if (argc > 1)
classify_print(root, atoi(*++argv));
else
classify_print(root, 6);
return 0;
*/ hz_print(root);
}

int getch(void);
void ungetch(int);

int getword(char *s, int max)
{
int i, c;

i = 0;
while ((c = getch()) == ' ' || c == '\t');

if (!isalpha(c))
{
	s[i] = 0;
	return c;
}

while (isalnum(c))
{
	s[i++] = c;
	c = getch();
}
s[i] = 0;

if (c != EOF)
	ungetch(c);

return *s;

}

#define BUFFMAX 100
char buff[BUFFMAX];
int buffp;

int getch(void)
{
return buffp > 0 ? buff[–buffp] : getchar();
}
void ungetch(int c)
{
if (buffp < BUFFMAX)
buff[buffp++] = c;
else
printf(“The buff is full!\n”);
}

void deal_state(int type)
{
static int temp;

if ((state & (EXP1 | EXP2)) == 0 && temp == '\"')
	nmarks++;

if (nmarks % 2 == 0)
	state &= ~STR;
else
	state |= STR;

if (!(state & STR)  && temp == '/' && type == '/')
	state |= EXP1;
if ((state & EXP1) && type == '\n')
	state &= ~EXP1;

if (!(state & STR) && temp == '/' && type == '*')
	state |= EXP2;
if ((state & EXP2) && temp == '*' && type == '/')
	state &= ~EXP2;

temp = type;

}
void deal_word(char *s)
{
if (!strcmp(s, “the”) || !strcmp(s, “and”))
state |= OVER;
else
state &= ~OVER;
}

TREE *alloc(void);
char *strdup(char *s);
int sit(int *a);

TREE *save_word(TREE *p, char *s, int line)
{
int mond;

if (p == NULL)
{
	int i;

	p = alloc();
	p->word = strdup(s);
	p->line[0] = line;
	for (i = 1; i < MAX_LINES; i++)
		p->line[i] = 0;
	p->count = 1;
	p->left = p->right = NULL;		
}
else if ((mond = strcmp(s, p->word)) == 0)
{
	p->count++;
	p->line[sit(p->line)] = line;
	if (hz_max < p->count)
		hz_max = p->count;
}
else if (mond < 0)
	p->left = save_word(p->left, s, line);
else if (mond > 0)
	p->right = save_word(p->right, s, line);

return p;

}

TREE *alloc(void)
{
return (TREE *)malloc(sizeof(TREE));
}

char *strdup(char *s)
{
char *p;

if ((p = (char *)malloc(strlen(s) + 1)) == NULL)
	printf("alloc: there are not memory!\n");
strcpy(p, s);
return p;

}

int sit(int *a)
{
int i;

for (i = 0; a[i]; i++);
return i;

}

void normal_print_word(TREE *p)
{
int i;
if §
{
normal_print_word(p->left);
printf("%s: “, p->word);
for (i = 0; p->line[i]; i++)
printf(”%d “, p->line[i]);
printf(”\n");
normal_print_word(p->right);
}
}

char temp[MAX_LEN];

void classify_print(TREE *p, int n)
{
if §
{
classify_print(p->left, n);
if (strcmp_n(p->word, temp, n))
printf("\n");
printf("%s ", p->word);
strcpy(temp, p->word);
classify_print(p->right, n);
}
}

int strcmp_n(char *s1, char *s2, int n)
{
int i;

for (i = 0; s1[i] == s2[i]; i++);

return (i < n);

}

void hz_print_active(TREE *p, int n);

void hz_print(TREE *p)
{
int n = hz_max;
while (n)
{
hz_print_active(p, n);
n–;
}
}

void hz_print_active(TREE *p, int n)
{
if§
{
hz_print_active(p->left, n);
if (p->count == n)
printf("%d %s\n", p->count, p->word);
hz_print_active(p->right, n);
}
}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值