我在C中有这3个文件,一个是头文件,另一个是dictionary.C,最后一个是main.C
dictionary.c
#include <stdlib.h>
#include <stdio.h>
#include <string.h>
#include "dictionary.h"
/* Calculate the edition distance between two strings */
int getDistance(char* ref,char* word) {
char* w1=NULL;
char* w2=NULL;
int dist=0;
int i=0;
bool different=false;
/* Put the shortest word on w1 and the longest on w2. */
if(strlen(ref)<=strlen(word)) {
w1=ref;
w2=word;
} else {
w1=word;
w2=ref;
}
/* Copare char to char */
for(i=0;i<strlen(w2);i++) {
if(different) {
/* If the words are different, increase the distance */
dist++;
} else {
/* Compare the new char and word lenghts */
if(i<strlen(w1)) {
different=w1[i]!=w2[i];
} else {
/* w1 is shorter than w2 */
different=true;
}
if(different) {
/* If the words are different, increase the distance */
dist++;
}
}
}
return dist;
}
/* Get word information */
bool getWordInfo(char* str,char* word,int* languageID,int* wordID){
*languageID=-1;
*wordID=-1;
sscanf(str,"%s ; %d ; %d",word,languageID,wordID);
return *languageID>0 && *wordID>0;
}
WordList create(){
WordList l;
l.act = 1;
l.nelem = 0;
return l;
};
WordList insert(WordList l, tWordInfo e){
int i;
if (l.nelem == [250]){
printf("error");
}else{
for (i=l.nelem; l.act; i--){
l.WordInfo[i+1]= l.WordInfo[i];
}
l.WordInfo[l.act]= e;
l.nelem = l.nelem + 1;
l.act = l.act + 1;
}
return l;
}
dictionary.h
/* Declare the boolean type */
typedef enum {false, true} bool;
/* Calculate the edition distance between two strings */
int getDistance(char* ref,char* word);
/* Get word information */
bool getWordInfo(char* str,char* word,int* languageID,int* wordID);
char tWordInfo(int* languageID, int* wordID, char* word[128]);
typedef struct {
char tWordInfo[250];
int act;
int nelem;
}tWordList, WordList;
WordList create();
WordList insert(WordList l, tWordInfo e);
和最后一个主要的。c
#include <stdio.h>
#include "dictionary.h"
int main(int argc, char **argv)
{
char word1[128];
char word2[128];
int langId1,wordId1;
int langId2,wordId2;
int d1,d2;
WordList l;
tWordInfo e;
create();
printf("%dn", l.act);
printf("%sn", l.tWordInfo[l.act]);
printf("%dn", l.nelem);
getWordInfo("hospital ; 1 ; 1",word1,&langId1,&wordId1);
getWordInfo("hola ; 1 ; 2",word2,&langId2,&wordId2);
d1=getDistance(word1,word2);
d2=getDistance(word2,word1);
printf("[%d,%d] => %sn",langId1,wordId1,word1);
printf("[%d,%d] => %sn",langId2,wordId2,word2);
printf("d(%s,%s)=%dn",word1,word2,d1);
printf("d(%s,%s)=%dn",word2,word1,d2);
return 0;
}
程序尚未结束,但我无法定义函数insert,在字典文件(.c和.h)中,会出现以下错误:未预期的声明说明符或"…"在"tWordInfo"之前,那么这里的问题是什么?我该如何解决?
另一个问题是,l.act和l.nelem的printf没有给我1和0的值,给l.act-2,给l.nelem一个大数字。为什么?
感谢
TWordInfo被声明为一个函数,然后在插入函数中用作参数类型。该参数需要不同的类型。