C语言结构体函数

bcai_yuan 2017-05-09 05:32:25
这是书上的练习题,统计关键字出现的次数,在binsearch函数中,结构体总是出错,哪位大神可以帮个忙看一下啊
#include<stdio.h>
#include<ctype.h>
#include<string.h>

struct key{
char *word;
int count;
};
struct key keytab[] = { //关键字列表必须按升序存储在keytab中
"auto", 0,
"break", 0,
"case", 0,
"char", 0,
"const", 0
};

#define NKEYS (sizeof keytab / sizeof(struct key))
#define MAXWORD 100

int getword(char *, int);
int binsearch(char *, struct key *, int);

//统计输入中C语言关键字的个数
main()
{
int n;
char word[MAXWORD];

while(getword(word, MAXWORD) != EOF)
if(isalpha(word[0])) //isalpha判断字符是否为英文字母,yes,return 非0;No,return 0;
if((n = binsearch(word, keytab, NKEYS)) >= 0)
keytab[n].count++;
for(n=0; n<NKEYS; n++)
if(keytab[n].count > 0)
printf("%4d %s\n", keytab[n].count, keytab[n].word);
return 0;
}

#include<stdio.h>
#include<ctype.h>
#include<string.h>

//binsearch:在tab[0]...tab[n-1]中查找单词 折半查找函数
int binsearch(char *word, struct key tab[ ], int n)
{
int cond;
int low, high, mid;

low = 0;
high = n - 1;
while(low <= high) {
mid = (low+high) / 2;
if((cond = strcmp(word, tab[mid].word)) < 0) //strcmp(s1,s2):判断两个字符串大小,s1 < s2 -;
high = mid - 1; //s1 > s2 +;
else if(cond > 0) //s1 = s2 0
low = mid + 1;
else
return mid;
}
return -1;
}



这是总会显示的错误,好捉急啊,哪位大神帮帮我吧
...全文
322 7 打赏 收藏 转发到动态 举报
写回复
用AI写文章
7 条回复
切换为时间正序
请发表友善的回复…
发表回复
bcai_yuan 2017-05-09
  • 打赏
  • 举报
回复
哦哦哦,好的,已经解决了 谢谢
自信男孩 2017-05-09
  • 打赏
  • 举报
回复
引用 5 楼 xiaohuh421 的回复:
上面错了 修改成 int binsearch(char *word, struct key *tab, int n)
这两种方法编译器都能正常识别([], *),主要是bsearch所在的源文件里找不到struct key的声明吧
xiaohuh421 2017-05-09
  • 打赏
  • 举报
回复
上面错了 修改成 int binsearch(char *word, struct key *tab, int n)
xiaohuh421 2017-05-09
  • 打赏
  • 举报
回复
int binsearch(char *word, struct key tab[ ], int n) 修改成 int binsearch(char *word, struct key tab*, int n)
自信男孩 2017-05-09
  • 打赏
  • 举报
回复
#include<stdio.h>
#include<ctype.h>
#include<string.h>

struct key{
    char *word;
    int count;
};
struct key keytab[] = {
    {"auto", 0},
    {"break", 0},
    {"case", 0},
    {"char", 0},
    {"const", 0}
};

#define NKEYS (sizeof keytab / sizeof(struct key))
#define MAXWORD 100

int getword(char *, int);
int binsearch(char *, struct key *, int);

int main()
{
    int i;
    char word[MAXWORD];

    //while(getword(word, MAXWORD) != EOF)
    while(fgets(word, MAXWORD, stdin) != NULL) {
        word[strlen(word) - 1] = 0;
        if(isalpha(word[0]))
            if((i = binsearch(word, keytab, NKEYS)) >= 0)
                keytab[i].count++;
    }
    for(i = 0; i < NKEYS; i++)
        if(keytab[i].count > 0)
            printf("%4d %s\n", keytab[i].count, keytab[i].word);
    return 0;
}

int binsearch(char *word, struct key tab[ ], int n)
{
    int cond;
    int low, high, mid;

    low = 0;
    high = n - 1;
    while(low <= high) {
        mid = (low+high) / 2;
        if((cond = strcmp(word, tab[mid].word)) < 0)
            high = mid - 1;
        else if(cond > 0)
            low = mid + 1;
        else
            return mid;
    }
    return -1;
}
这个方法是都写到一个源文件里,如果是多个源文件同时存在,建议添加一个头文件,将结构体的声明放在头文件里,然后多个源文件include该头文件
自信男孩 2017-05-09
  • 打赏
  • 举报
回复
bsearch是在另一个文件里,这个文件找不到struct key数据结构,所以会提示这个错误。解决办法是:两个方法。 1. 把bsearch放在main同源文件里。 2. 将struct key单独放在一个头文件里,在两个源文件里都引用该头文件。 两种方法都可以。
ck2333 2017-05-09
  • 打赏
  • 举报
回复
#include<stdio.h>
#include<ctype.h>
#include<string.h>
#define NKEYS (sizeof keytab / sizeof(struct key))
#define MAXWORD 100
 
struct key{
    char word[10];
    int count;
};
key keytab[] = {     //关键字列表必须按升序存储在keytab中
    {"auto", 0},
    {"break", 0},
    {"case", 0},
    {"char", 0},
    {"const", 0} 
};
 
int getword(char *, int);
int binsearch(char *, struct key *, int);
 
//统计输入中C语言关键字的个数
int main()
{
    int n;
    char word[MAXWORD];
 
    while(getword(word, MAXWORD) != EOF)
        if(isalpha(word[0]))    //isalpha判断字符是否为英文字母,yes,return 非0;No,return 0;
            if((n = binsearch(word, keytab, NKEYS)) >= 0)
                keytab[n].count++;
    for(n=0; n<NKEYS; n++)
        if(keytab[n].count > 0)
            printf("%4d %s\n", keytab[n].count, keytab[n].word);
    return 0;
} 

69,335

社区成员

发帖
与我相关
我的任务
社区描述
C语言相关问题讨论
社区管理员
  • C语言
  • 花神庙码农
  • 架构师李肯
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧