当前位置: 代码迷 >> C语言 >> c语言 strcmp strcpy
  详细解决方案

c语言 strcmp strcpy

热度:548   发布时间:2006-03-16 08:55:00.0
c语言 strcmp strcpy

大姐们大哥们看以下我这个程序怎么了
那里有问题啊?
#include<stdio.h>
#include<string.h>

struct tree
{
char word[20];
int count;
int sign;
struct tree * parent;
struct tree * leftson;
struct tree * rightson;
};


int i=0;
struct tree *treeroot;
char preword[20];
char temp[20];
FILE *fp;

Get_word(FILE *fp)
{
char ch;
int i=0;
ch=fgetc(fp);

strcpy(preword,temp);
while(ch!=' '&&ch!='\n'&&ch!='\r'&&ch!='\t'&&ch!=','&&ch!='.'&&ch!='\"'&&ch!='\'') {
preword[i]= ch;
i++;
ch=fgetc(fp);
}
if(i==0) return 0;
printf("the getword is :%s\n",preword);
return 0;
}

Sis_word(char *preword,struct tree *nangua)
{
struct tree *p;
if(strcmp(preword,nangua->word)>0)
{
if(nangua->rightson!=NULL) Sis_word(preword,nangua->rightson);
else if(nangua->rightson==NULL)
{
p =(struct tree*)malloc(sizeof(struct tree));

strncpy(p->word, preword,20);
p->count = 1;
printf("i want to insert this new word at right:%s\n",preword);
printf("the p.word is:%s\n",p->word);
p->rightson = NULL;
p->leftson = NULL;
p->parent = nangua;
p->sign=0;
nangua->rightson=p;
strcpy(preword,temp);
printf("changed word*****%s\n",preword);
return 0;
}
}
else if(strcmp(preword,nangua->word)==0) nangua->count++;
else if(strcmp(preword,nangua->word)<0)
{
if(nangua->leftson!=NULL) Sis_word(preword,nangua->leftson);
else if(nangua->leftson==NULL)
{
p =(struct tree*)malloc(sizeof(struct tree));

strncpy(p->word,preword,20);
p->count = 1;
printf("i want to insert this new word at left:%s\n",preword);
printf("the p.word is:%s\n",p->word);
p->rightson = NULL;
p->leftson = NULL;
p->parent = nangua;
p->sign=0;
nangua->leftson =p;
strcpy(preword,temp);
printf("changed word*****%s\n",preword);
return 0;
}
}
}

visit(struct tree *treeroot)
{
if(treeroot->leftson!=NULL) visit(treeroot->leftson);

else if(treeroot->leftson==NULL&&treeroot->rightson!=NULL)
{
printf("%s***%d",treeroot->word,treeroot->count);
treeroot->sign=1;
visit(treeroot->rightson);
}

else if(treeroot->leftson==NULL&&treeroot->rightson==NULL)
{
if(treeroot->sign==0)
{
printf("10%s*10%d\n",treeroot->word,treeroot->count);
treeroot->sign=1;
}
if(treeroot->parent!=NULL) return 0;
if(treeroot->parent->sign==0)
treeroot->parent->leftson=NULL;
if(treeroot->parent->sign==1)
treeroot->parent->rightson=NULL;

visit(treeroot->parent);
}
}

main()
{
fp = fopen("A.H","r");
Get_word(fp);

for(i=0;i<20;i++) temp[i]=' ';
i=0;

strcpy(treeroot->word,preword);
treeroot->count = 1;
printf("yeah the first word is %s\n",preword);
treeroot->rightson = NULL;
treeroot->leftson = NULL;

printf("preword:%s\n",preword);
strcpy(preword,temp);

do{
Get_word(fp);
if(!strcmp(preword,temp)) visit(treeroot);

Sis_word(preword,treeroot);
i=strcmp(preword,temp);
}while(strcmp(preword,temp)==0);

}


循环怎么也进不去
用feof(fp);也不行
急救啊!!!!

搜索更多相关的解决方案: c语言  strcmp  strcpy  

----------------解决方案--------------------------------------------------------
你先说说你要做什么 我没细看 我不喜欢没目的的看

这应该是二叉树吧..可是 struct tree * parent 这个定义是什么意思

父母...? 要这个指针干什么?
----------------解决方案--------------------------------------------------------
是想统计一片文章的词频
----------------解决方案--------------------------------------------------------
能具体说说吗?  不过 这个 return 0  应该加()吧 return (0)
----------------解决方案--------------------------------------------------------
我是想先用一个二叉排序树存储单词的数量
如果有相同的词则加一
没有则建立一个接点

后来中序遍历这个树
----------------解决方案--------------------------------------------------------
struct tree * parent
后来遍历的时候用的
----------------解决方案--------------------------------------------------------

# include <stdio.h>
struct stat
{char word[20];
int count;
struct stat *next;}

char get_word(int 1)//这我本想没有形参.可是不行..不知道该怎么办.就加的1//
{char prword[20];
gets(prword);//这我不知道能不能返回去.还没学指针.改改应该没问题//
ruturn(*prword);
}

main()
{struct stat *head,*p,*q,*x;
int y;
head=0;
q=(struct stat *)malloc(sizeof(struct stat)) ;
q->word=get_word(1)//这能给过去吗//;q->count=1;q->next=0;
while(q->word[0]!='*')//这里不知道能不能给过去.找下书.改下应该没问题的//
{ if (head==0) head=p=q;
else{x=head;
while (x!=0)
{if(x->word==q->word)//这里好象不能这样比较.实在不行用FOR也可以.我没时间了.意思一下// {x->count=x->count+1; break;}
else x=x->next ;
if (x==0) y=1;}
if(y==1) {p->next=q; p=p->next;} }
q=(struct stat *)malloc(sizeof(struct stat)) ;
q->word=get_word(1);q->count=1;q->next=0; }

p=head;
while(p->next!=0)
{printf("%s,%d",p->word,p->count);
p=p->next;}
getch();
}


不好意思.我这肯定是有错的.
第一..我没学到文件..就直接用输入了
第二..我没用树..用的是链表.
大概意思就那样..你就改改看吧..然后我再找找书
我中午全做这个了..还没吃饭呢.呵呵..肯定是有错的..然后我再找找书

晚上再改下..意思大概就这样~! 也不知道合不合你的意思
还请大家多改改..最好我晚上来的时候就能运行了.哈哈~思路应该没有问题的吧!

[此贴子已经被作者于2006-3-16 14:23:22编辑过]


----------------解决方案--------------------------------------------------------

谢谢了
我也是弄了一天了
这是我第一次编这样的程序
所以好多不知道


----------------解决方案--------------------------------------------------------
中序遍历..
mid(struct tree *t)
...
if (t!=0)
{mid(t.lchild);
printf("%d\n",t.d);
mid(t.rchild);}

return~~
----------------解决方案--------------------------------------------------------
谢谢,很大收获呀
----------------解决方案--------------------------------------------------------
  相关解决方案