c语言实strcpy和strcmp,c语言 strcmp strcpy

博主在C语言中遇到程序问题,涉及到strcmp和strcpy函数的使用,以及在构建二叉树过程中插入新节点的逻辑。代码中字符串比较和插入过程的困惑导致循环无法正常进行,求助于如何解决feof判断问题。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

c语言 strcmp strcpy

大姐们大哥们看以下我这个程序怎么了

那里有问题啊?

#include

#include

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);也不行

急救啊!!!!

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值