| 网站首页 | 业界新闻 | 小组 | 威客 | 人才 | 下载频道 | 博客 | 代码贴 | 在线编程 | 编程论坛
欢迎加入我们,一同切磋技术
用户名:   
 
密 码:  
共有 4044 人关注过本帖
标题:虚心请教各位大牛,帮我解决一下问题cannot convert parameter 1 from 'str ...
只看楼主 加入收藏
tzr573796771
Rank: 1
等 级:新手上路
帖 子:6
专家分:0
注 册:2009-9-24
结帖率:100%
收藏
已结贴  问题点数:20 回复次数:5 
虚心请教各位大牛,帮我解决一下问题cannot convert parameter 1 from 'struct main::node *' to 'struc
题目是统计输入中所有单词的出现次数,采用二叉树的结构存储各个单词
#include <stdio.h>
#include <ctype.h>
#include <string.h>
#include <stdlib.h>
#define MAX 100

struct tnode
{
    char * word;
    int count;
    struct tnode *left;
    struct tnode *right;
};
/*在输入中得到单词*/
int getWord(char *word,int lim)
{
    int c,getch();
    void ungetch(int);//ungetch(int)是将多余的字符放回到输入中
    char *w=word;
    while(isspace(c=getch()))//isspace()函数跳过空白字符
    {};
    if(c!=EOF)
    {
        *w++=c;
    }
    if(!isalpha(c))//isalpha()函数识别字母
    {
        *w='\0';
        return c;
    }
    for(;--lim>0;w++)
    {
        if(!isalnum(*w=getch()))//isalnum()函数识别字母和数字
        {
            ungetch(*w);
            break;
        }
    }
    *w='\0';
    return word[0];
}
/*用二叉树的方法查找新单词的次数*/
struct tnode *binAddTree(struct tnode *p,char *w)
{
    int cond;
    if(p==NULL)
    {
        p=(struct tnode *)malloc(sizeof(struct tnode));
    //    p=talloc();
        p->word=w;
        p->left=NULL;
        p->right=NULL;
        p->count=1;
    }
    else if((cond=strcmp(w,p->word))==0)
    {
        p->count++;
    }
    else if(cond<0)
    {
        p->left=binAddTree(p->left,w);
    }
    else
    {
        p->right=binAddTree(p->right,w);
    }
    return p;
}
/*把节点打印出来*/
void printfTree(struct tnode *root)
{
    if(root!=NULL)
    {
        printfTree(root->left);
        printf("%s",root->word);
        printfTree(root->right);
    }
}

void main()
{
    struct node * root;
    char word[MAX];
    root=NULL;
    while(getWord(word,MAX)!=EOF)
    {
        if(isalpha(word[0]))
        {
            root=binAddTree(root,word);
        }
    }
    printfTree(root);
}


在这里root=binAddTree(root,word)出现错误:error C2664: 'binAddTree' : cannot convert parameter 1 from 'struct main::node *' to 'struct tnode *'
求教各位大牛,怎样解决啊!!!
搜索更多相关主题的帖子: convert main parameter node cannot 
2009-09-24 22:33
jig
Rank: 16Rank: 16Rank: 16Rank: 16
等 级:版主
帖 子:530
专家分:242
注 册:2005-12-27
收藏
得分:10 
说实在,看了会。不太想去跟你的逻辑。但换我来写,些许我会换种方式,更好理解一些,而其你的结构中左右指针没必要有两个

直接一个 next指针就可以了。虽然没跟下去,但一看至少你对每个word指针的赋值是不明智的。都是些空指针,或指向已经释放的空间

或就算指向有实际数据的对应空间,但每个字符串未必有'\0'结束符。从这点来看,你这段代码就有很大问题

[ 本帖最后由 jig 于 2009-9-25 08:46 编辑 ]

个人网站 -  http://.h001.
2009-09-25 07:06
jig
Rank: 16Rank: 16Rank: 16Rank: 16
等 级:版主
帖 子:530
专家分:242
注 册:2005-12-27
收藏
得分:0 
试着写了下


#include <stdio.h>
#include <ctype.h>
#include <string.h>
#include <stdlib.h>
#define MAX 100
 
struct tnode
{
    char            *word;
    int             count;
    struct tnode    *next;
};
 
void getWord(char *word,int lim)
{
    int i=0;
 
    while (1)
    {
        if ((*(word+i) = getchar()) == '\n')
        {
            break;
        }
 
        i++;
        if (i+1 == lim)
        {
            break;
        }
    }
 
    word[i] = '\0';
}
 
int ChekNull(char *p)
{
    int j = 0, i = 0;
    while (*(p+i) != '\0')
    {
        if (*(p+i) == ' ')
        {
            j++;
        }
        i++;
    }
 
    if (j == strlen(p))
    {
        return 1;
    }
 
    return 0;
}
 
 
int ChekStrCount(struct tnode *p, char *w)
{
 
    while (p != NULL)
    {
        if (strcmp(p->word, w) == 0)
        {
            p->count++;
            return 1;
        }
 
        p = p->next;
    }
 
    return 0;
}
 
 
void printfTree(struct tnode *p)
{
 
    while (p != NULL)
    {
        printf("%s  %d\n", p->word, p->count);
 
        p = p->next;
    }
}
 
 
struct tnode *binAddTree(struct tnode *p,char *w)
{
    char temp[MAX];
    struct tnode *ls;
    int j = 0, i = 0;
    while (1)
    {
        if (*(w+i) == ' ' || *(w+i) == '\0')   /*以空格作为字符串判断标志*/
        {
            memcpy(temp, w+j, i-j+1); /*获取单词*/
            temp[i-j] = '\0';
 
            if (!ChekNull(temp))    /*判断单词不为空*/
            {
                /*printf("\n%s", temp);*/  /*可以注释掉这句,查看是否将每个单词都准确无误的截取下来*/
 
                if (p == NULL)
                {
                    p = (struct tnode *)malloc(sizeof(struct tnode));
                    p->word = (char *)malloc(sizeof(strlen(temp)+1));
                    memcpy(p->word, temp, strlen(temp)+1);
                    p->count = 1;
                    p->next = NULL;
                    ls = p;
                }
                else
                {
                    if (!ChekStrCount(p, temp)) /*说明此字符串是第一此出现*/
                    {
                        ls->next = (struct tnode *)malloc(sizeof(struct tnode));
                        ls->next->word = (char *)malloc(sizeof(strlen(temp)+1));
                        memcpy(ls->next->word, temp, strlen(temp)+1);
                        ls->next->count = 1;
                        ls->next->next = NULL;
                        ls = ls->next;
                    }   
                }
            }
            j = i+1;
        }
 
        if (*(w+i) == '\0') break;
        i++;
    }
 
    return p;
}
 
 
int main()
{
     
    struct tnode     *root = NULL;
    char word[MAX];
 
    getWord(word, MAX);                 /*获得输入的字符串*/
    root = binAddTree(root, word);      /*建立单向链表*/
    printfTree(root);                   /*打印结果*/
 
 
    return 0;
}

你输入
nihao nihao shide shide shide 123 kkk

结果
nihao 2
shide 3
123 1
kkk 1

个人网站 -  http://.h001.
2009-09-25 08:45
zhddragon
Rank: 5Rank: 5
等 级:职业侠客
帖 子:208
专家分:346
注 册:2009-5-14
收藏
得分:10 
struct tnode *binAddTree(struct tnode *p,char *w)
struct node * root;
root=binAddTree(root,word);

身体是玩命的本钱
2009-09-25 12:12
tzr573796771
Rank: 1
等 级:新手上路
帖 子:6
专家分:0
注 册:2009-9-24
收藏
得分:0 
回复 2楼 jig

感谢2楼的思路点拨,让我学会了用另外一种方法去解决问题!
再次感谢~~~
2009-09-26 17:29
tzr573796771
Rank: 1
等 级:新手上路
帖 子:6
专家分:0
注 册:2009-9-24
收藏
得分:0 
回复 3楼 jig
我太粗心了!!
呵呵,谢谢啦~~~
2009-09-26 17:30
快速回复:虚心请教各位大牛,帮我解决一下问题cannot convert parameter 1 from ...
数据加载中...
 
   



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

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