首页 诗词 字典 板报 句子 名言 友答 励志 学校 网站地图
当前位置: 首页 > 教程频道 > 开发语言 > C语言 >

也许是个很简单的有关问题,但自己往往看不出自己哪儿编错了?

2012-03-13 
也许是个很简单的问题,但自己往往看不出自己哪儿编错了?~!这是一个打印一段C语言程序源代码中变量的程序//

也许是个很简单的问题,但自己往往看不出自己哪儿编错了?~!
这是一个打印一段C语言程序源代码中变量的程序
//head.h
#include <stdio.h>
#include <ctype.h>
#include <string.h>
#include <stdlib.h>

#define BUFSIZE 100
char buf[BUFSIZE];
int bufp = 0;
char *keyword[] = {"auto","char","const","double","extern","float","int","long","register","short","signed","static","struct","union","unsigned","void","volatile"};
#define NKEYS (sizeof keyword[] / sizeof keyword[0])
int getch()
{
return (bufp>0) ? buf[--bufp] : getchar();
}

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

int getword(char *word,int lim)
{
int c;
char *w = word;
while(isspace(c = getch()))
;
if(c != EOF)
*w++ = c;
if(!isalpha(c))
{
switch(c)
{
case '"':
for(;--lim>0,(*w=getch())!='"';w++)
{
if(*w == '\n')
{
printf("sytex error,missing quotation mark");
break;
}
}
break;
case '/':
*w = getch();
if(*w == '/')
{
for(;--lim>0,(*w=getch())!='\n';w++)
;
}
else if(*w == '*')
{
for(;--lim>0,(*w=getch())!='\n';w++)
;
if(*(w-1) == '/' && *(w-2) == '*')
break;
else
{
printf("sytex error:missing */");
break;
}
}
else
{
printf("sytex error");
break;
}
default:
break;
}
*w = '\0';
return c;
}
for(;--lim>0;w++)
if(!isalnum(*w = getch()))
{
if(*w != '_')
{
ungetch(*w);
break;
}
else
continue;
}
*w = '\0';
return *word;
}

int binsearch(char *word,char* keyword[],int n)
{
int cond;
int low,high,mid;
low = 0;
high = n - 1;
while(low<=high)
{
mid = (low+high) / 2;
if((cond = strcmp(word,keyword[mid]))<0)
high = mid - 1;
else if (cond>0)
low = mid + 1;
else
return mid;
}
return -1;
}

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

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

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

struct tnode *addtree(struct tnode *p,char *w)
{
int cond;
if(p == NULL)
{
p = talloc();
p->word = strdup(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);
}
}

//VariableShow.cpp
#include "head.h"
#define MAXWORD 100
char *variable[100];
int wordcmp(char *s,char *d,int n);

void main(int argc,char* argv[])
{
int i = 0;
struct tnode *root;
root = NULL;
char word[MAXWORD];
while (getword(word,MAXWORD) != '`')
{
if(isalpha(word[0]))
{
if(binsearch(word,keyword,NKEYS) >0)
{


*variable[i] = getword(word,MAXWORD);
root = addtree(root,variable[i]);
}
}
}
treeprint(root);
}

int wordcmp(char *s,char *d,int n)
{
for(;*s == *d;s++,d++,n--)
if(n == 0)
return 1;
return *s - *d;
}

生成时系统提示错误
错误2error C2059: 语法错误 : “]”c:\documents and settings\yw\my documents\visual studio 2005\projects\sample6_2\sample6_2\variableshow.cpp16
错误3error C2143: 语法错误 : 缺少“;”(在“{”的前面)c:\documents and settings\yw\my documents\visual studio 2005\projects\sample6_2\sample6_2\variableshow.cpp17

这是什么原因,哪儿错了???

[解决办法]
#define NKEYS (sizeof keyword[] / sizeof keyword[0]) 
=================================================================
应该是 ( sizeof(keyword)/sizeof(keyword[0]) )
sizeof keyword[],好像没有这种写法,呵呵

热点排行