| 网站首页 | 业界新闻 | 小组 | 威客 | 人才 | 下载频道 | 博客 | 代码贴 | 在线编程 | 编程论坛
欢迎加入我们,一同切磋技术
用户名:   
 
密 码:  
共有 618 人关注过本帖
标题:[求助]这个C程序的问题在哪里?
只看楼主 加入收藏
austavo
Rank: 1
等 级:新手上路
帖 子:4
专家分:0
注 册:2005-11-9
收藏
 问题点数:0 回复次数:1 
[求助]这个C程序的问题在哪里?

这是一个树的查找。
#include <stdio.h>
#include <stdlib.h>
#include <ctype.h>
#include <string.h>

#define MAXWORD 100
#define BUFSIZE 100

struct tnode{
char *word;
int count;
struct tnode *left;
struct tnode *right;
};

struct tnode *addtree(struct tnode *,char *);
struct tnode *talloc(void);
void treeprint(struct tnode *);
int getword(char *,int);
char *strd(char *);
char buf[BUFSIZE];
int bufp=0;

main()
{
struct tnode *root;
char word[MAXWORD];

root=NULL;
while(getword(word,MAXWORD) != EOF)
if (isalpha(word[0]))
root=addtree(root,word);
return 0;
}

struct tnode *addtree(struct tnode *p,char *w)
{
int cond;

if (p==NULL){
p=talloc();
p->word=strd(w);
p->count=1;
p->left=p->right=NULL;
}
else if ((cond=strcmp(w,p->word))==0)
p->count++;
else if (cond<0)
p->left=addtree(p->left,w);
else
p->right=addtree(p->right,w);
return p;
}

void treeprint(struct tnode *p)
{
if (p !=NULL){
treeprint(p->left);
printf("%4d %s\n",p->count,p->word);
treeprint(p->right);
}
}

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];
}

int getch(void)
{
return (bufp>0)?buf[--bufp]:getchar();
}

void ungetch(int c)
{
if (bufp>=BUFSIZE)
printf("ungetch:too many characters\n");
else
buf[bufp++]=c;
}

struct tnode *talloc(void)
{
return (struct tnode *)malloc(sizeof(struct tnode));
}

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

p=(char *)malloc(strlen(s)+1);
if (p !=NULL)
strcpy(p,s);
return p;
}

搜索更多相关主题的帖子: tnode struct char int include 
2005-12-29 13:32
austavo
Rank: 1
等 级:新手上路
帖 子:4
专家分:0
注 册:2005-11-9
收藏
得分:0 

我已经找到错误了,请斑斑删贴好了


曾经的故事风淡云轻
2005-12-29 15:12
快速回复:[求助]这个C程序的问题在哪里?
数据加载中...
 
   



关于我们 | 广告合作 | 编程中国 | 清除Cookies | TOP | 手机版

编程中国 版权所有,并保留所有权利。
Powered by Discuz, Processed in 0.013924 second(s), 8 queries.
Copyright©2004-2024, BCCN.NET, All Rights Reserved