struct tag-name{
{
member 1;
…
member N;
};
struct tnode{/*the tree node:*/
char *word;/*points to the next*/
int count;/*number of occurences*/
struct tnode *left;/*left child*/
struct tnode *right;/*right child*/
}
#include<stdio.h>
#include<ctype.h>
#include<string.h>
#include"tNode.h"
#define MAXWORD 100
struct tnode *addtree(struct tnode *,char *);
void treeprint(struct tnode *);
int getword(char *,int);
struct tnode *talloc(void);
char *strdup2(char *);
/*word frequency count*/
main()
{
struct tnode *root;
char word[MAXWORD];
root=NULL;
while(getword(word,MAXWORD)!=EOF)
if(isalpha(word[0]))
root=addtree(root,word);
treeprint(root);
return 0;
}
#define BUFSIZE 100
char buf[BUFSIZE];/*buffer for ungetch*/
int bufp=0;/*next free position in buf*/
int getch(void)/*get a (possibly pushed back) character*/
{
return (bufp>0)? buf[--bufp]:getchar();
}
void ungetch(int c)/*push back character on input*/
{
if(bufp>=BUFSIZE)
printf("ungetch:too many characters\n");
else
buf[bufp++]=c;
}
/*getword:get next word or character from input*/
int getword(char *word,int lim)
{
int c,getch(void);
void ungetch(int);
char *w=word;
while(isspace(c=getch() ));
if(c!=EOF)
*w++=c;
if(!isalpha(c)){
*w='\0';
return c;
}
for(;--lim>0;w++)
if(!isalnum(*w=getch())){
ungetch(*w);
break;
}
*w='\0';
return word[0];
}
/*addtree:add a node with w,at or below p*/
struct tnode *addtree(struct tnode *p,char *w)
{
int cond;
if(p==NULL){/*a new word has arrived*/
p=talloc();/*make a new node*/
p->word=strdup(w);
p->count=1;
p->left=p->right=NULL;
}else if((cond=strcmp(w,p->word))==0)
p->count++;/*repeated word*/
else if(cond<0)/*less than into left subtree*/
p->left=addtree(p->left,w);
else /*greater than into right subtree*/
p->right=addtree(p->right,w);
return p;
}
/*treeprint:in-order print of tree p*/
void treeprint(struct tnode *p)
{
if(p!=NULL){
treeprint(p->left);
printf("%4d %s\n",p->count,p->word);
treeprint(p->right);
}
}
#include<stdlib.h>
/*talloc:make a tnode*/
struct tnode *talloc(void)
{
return (struct tnode *)malloc(sizeof(struct tnode));
}
char *strdup2(char *s)/*make a duplicate of s*/
{
char *p;
p=(char *)malloc(strlen(s)+1);/*+1 for '\0'*/
if(p!=NULL)
strcpy(p,s);
return p;
}
#define KEYWORD 01 /*0001*/ #define EXTERNAL 02 /*0010*/ #define STATIC 04 /*0100*/
enum{KEYWORD =01,EXTERNAL =02,STATIC =04};
struct{
unsigned int is_keyword:1;
unsigned int is_extern:1;
unsigned int is_static:1;
}flags;/*This defines a variable called flags that contains three 1-bit fields*/
flags.is_extern=flags.is_static=1;
flags.is_extern=flags.is_static=0;
机械节能产品生产企业官网模板...
大气智能家居家具装修装饰类企业通用网站模板...
礼品公司网站模板
宽屏简约大气婚纱摄影影楼模板...
蓝白WAP手机综合医院类整站源码(独立后台)...苏ICP备2024110244号-2 苏公网安备32050702011978号 增值电信业务经营许可证编号:苏B2-20251499 | Copyright 2018 - 2025 源码网商城 (www.ymwmall.com) 版权所有