#include
#include
#include
#include
#define MAXWORD 30 /*单词最多字符数*/
#define MAXARRAY 2000 /*文件最多单词总数*/
#define MAXLINE 80 /*每次最多读取MAXLINE-1个字符*/
struct node
{
char word [MAXWORD];
int count;
};
/*函数声明*/
void GetWord(char *p, struct node *wArry);
void AddArray(char *w, struct node *p);
void PritArray(struct node *p);
/*主程序*/
int main (int argc, char argv[])
{
int i;
FILE *fp;
struct node arrayWord[MAXARRAY];
char line[MAXLINE];
/*数组初始化*/
for(i = 0; i < MAXARRAY ;i++ )
{
arrayWord[i].count = 0;
arrayWord[i].word = '\0';
/*检查命令行参数个数*/
if(argc != 2)
{
printf("Usage :countWords Filename\n");
exit(1);
}
/*打开文件*/
if (fp = fopen(argv[1], "r"))
{
printf("countword:can't open %s\n",argv[1]);
exit(1);
}
/*统计单词*/
while (fgets(line,MAXLINE, fp) != NULL)
{
GetWord(line, arrayWord);
}
/*显示统计结果*/
PritArray(arrayWord);
}
/*提取单词*/
void GetWord(char *p, struct node *wArry);
{
int n;
char word[MAXWORD];
char *q, *w;
while (isspace(*p))
{
p++;
}
while (*p != '\0')
{
n = 0;/*单词字符个数,初始化为0*/
if (isspace(*p) && isspace (*(p-1)))/*单词的首个字母*/
{
q = p; /*记录单词的开始位置*/
while (isspace(*p) && n < MAXWORD)
{
p++;
n++;
}
/*单词以空白字符结尾*/
if(isspace(*p) || *p == '\0')
{
w = word;
while (q < p)
{ /*复制单词到word[]*/
*w++ = *p++ ;
}
*w = '\0';
/*将单词添加到arryWord[MAXARRAY]*/
AddArray(word, wArray);
}
}
if (*p != '\0')
{
p++;
}
}
}
/*统计单词*/
void AddArray(char *w, struct node *p);
{
int i;
for (int i = 0; i < MAXARRAY; i++, p++)
{
if (strcmp(p ->count, w) == 0)
{ /*单词已经存在*/
p ->count++;
break;
}else if (p ->count ==0)
{ /*单词不存在*/
strcpy(p -> word, w);
p ->count = 1;
break;
}
}
}
/*显示统计结果*/
void PritArray(struct node *p);
{
while (p ->count > 0)
{
printf("%4d %s\n",p ->count,p->word);
p++;
}
}
首先是定义数组那里 arrayWord[i].word = '\0';提示表达式必须是可修改的左值
/*打开文件*/哪里 error C2664: “fopen”: 不能将参数 1 从“char”转换为“const char *”
最后是定义函数括号里的形参,在函数里面会报错举个例子:
void AddArray(char *w, struct node *p);这个自定义函数
如果我下面这样写 if (strcmp(p ->count, w) == 0),这里面的w就会报错,提示未定义标识符,次啊面的函数也是这么个情况,请问昨晚那么晚改???